2022-10-24 21:22:48 +02:00
|
|
|
import datetime
|
|
|
|
|
2023-01-19 16:47:47 +01:00
|
|
|
import mongo_tooling_metrics.client as metrics_client
|
2024-10-10 19:59:18 +02:00
|
|
|
import pkg_resources
|
|
|
|
import pymongo
|
2024-05-17 00:00:17 +02:00
|
|
|
from mongo_tooling_metrics.lib.top_level_metrics import (
|
|
|
|
NinjaToolingMetrics,
|
|
|
|
ResmokeToolingMetrics,
|
|
|
|
SConsToolingMetrics,
|
|
|
|
)
|
2024-10-10 19:59:18 +02:00
|
|
|
from pydantic import ValidationError
|
2023-01-19 16:47:47 +01:00
|
|
|
|
2022-10-24 21:22:48 +02:00
|
|
|
# Check cluster connectivity
|
|
|
|
try:
|
2023-03-17 12:41:53 +01:00
|
|
|
client = pymongo.MongoClient(
|
|
|
|
host=metrics_client.INTERNAL_TOOLING_METRICS_HOSTNAME,
|
|
|
|
username=metrics_client.INTERNAL_TOOLING_METRICS_USERNAME,
|
|
|
|
password=metrics_client.INTERNAL_TOOLING_METRICS_PASSWORD,
|
|
|
|
)
|
|
|
|
client.server_info()
|
2022-10-24 21:22:48 +02:00
|
|
|
except Exception as exc:
|
|
|
|
print("Could not connect to Atlas cluster")
|
|
|
|
raise exc
|
|
|
|
|
2023-03-17 12:41:53 +01:00
|
|
|
metrics_classes = {
|
2024-05-17 00:00:17 +02:00
|
|
|
"ninja": NinjaToolingMetrics,
|
|
|
|
"scons": SConsToolingMetrics,
|
|
|
|
"resmoke": ResmokeToolingMetrics,
|
2023-03-17 12:41:53 +01:00
|
|
|
}
|
|
|
|
|
2022-10-24 21:22:48 +02:00
|
|
|
|
2023-03-17 12:41:53 +01:00
|
|
|
def get_metrics_data(source, lookback=30):
|
2022-11-28 18:15:48 +01:00
|
|
|
try:
|
|
|
|
# Get SCons metrics for the lookback period
|
2024-05-17 00:00:17 +02:00
|
|
|
tooling_metrics_version = pkg_resources.get_distribution("mongo-tooling-metrics").version
|
2022-11-28 18:15:48 +01:00
|
|
|
lookback_datetime = datetime.datetime.utcnow() - datetime.timedelta(days=lookback)
|
2024-05-17 00:00:17 +02:00
|
|
|
last_week_metrics = client.metrics.tooling_metrics.find(
|
|
|
|
{
|
|
|
|
"source": source,
|
|
|
|
"utc_starttime": {"$gt": lookback_datetime},
|
|
|
|
"tooling_metrics_version": tooling_metrics_version,
|
|
|
|
}
|
|
|
|
)
|
2022-11-28 18:15:48 +01:00
|
|
|
|
|
|
|
malformed_metrics = []
|
|
|
|
invalid_metrics = []
|
|
|
|
total_docs = 0
|
|
|
|
|
|
|
|
# Find any malformed/invalid documents in the cluster
|
|
|
|
for doc in last_week_metrics:
|
|
|
|
total_docs += 1
|
|
|
|
try:
|
2023-03-17 12:41:53 +01:00
|
|
|
metrics = metrics_classes[source](**doc)
|
2022-11-28 18:15:48 +01:00
|
|
|
if metrics.is_malformed():
|
2024-05-17 00:00:17 +02:00
|
|
|
malformed_metrics.append(doc["_id"])
|
2022-11-28 18:15:48 +01:00
|
|
|
except ValidationError:
|
2024-05-17 00:00:17 +02:00
|
|
|
invalid_metrics.append(doc["_id"])
|
2022-11-28 18:15:48 +01:00
|
|
|
|
2024-05-17 00:00:17 +02:00
|
|
|
metrics_detailed = (
|
|
|
|
f"METRICS DETAILED ({source}):\n"
|
|
|
|
f"malformed_metrics_last_week: {malformed_metrics}\n"
|
|
|
|
f"invalid_metrics_last_week: {invalid_metrics}\n"
|
|
|
|
f"total_docs_last_week: {total_docs}\n"
|
|
|
|
f"tooling_metrics_version: {tooling_metrics_version}\n"
|
|
|
|
)
|
2022-11-28 18:15:48 +01:00
|
|
|
metrics_overview = (
|
|
|
|
f"METRICS OVERVIEW ({source}):\n"
|
|
|
|
f"malformed_metrics_last_week: {len(malformed_metrics)} ({len(malformed_metrics)/total_docs*100:.2f}%)\n"
|
|
|
|
f"invalid_metrics_last_week: {len(invalid_metrics)} ({len(invalid_metrics)/total_docs*100:.2f}%)\n"
|
2023-01-18 22:25:44 +01:00
|
|
|
f"total_docs_last_week: {total_docs}\n"
|
2024-05-17 00:00:17 +02:00
|
|
|
f"tooling_metrics_version: {tooling_metrics_version}\n"
|
|
|
|
)
|
2022-11-28 18:15:48 +01:00
|
|
|
|
|
|
|
print(metrics_overview)
|
|
|
|
print(metrics_detailed)
|
|
|
|
|
|
|
|
return metrics_overview
|
|
|
|
|
|
|
|
except Exception as exc:
|
|
|
|
print("Unexpected failure while getting metrics")
|
|
|
|
raise exc
|
|
|
|
|
|
|
|
|
2023-03-17 12:41:53 +01:00
|
|
|
ninja_metrics_overview = get_metrics_data("ninja")
|
|
|
|
scons_metrics_overview = get_metrics_data("scons")
|
|
|
|
resmoke_metrics_overview = get_metrics_data("resmoke")
|