63 lines
2.5 KiB
Python
63 lines
2.5 KiB
Python
#!/usr/bin/env python3
|
|
"""Проверяет свежие telemetry signals из сети Compose, не печатая их payload."""
|
|
import json
|
|
import ssl
|
|
import time
|
|
from urllib.parse import urlencode
|
|
from urllib.error import HTTPError
|
|
from urllib.request import urlopen
|
|
|
|
|
|
TLS = ssl.create_default_context(cafile="/run/pki/ca.crt")
|
|
|
|
|
|
def get_json(url):
|
|
with urlopen(url, context=TLS, timeout=5) as response:
|
|
return json.loads(response.read())
|
|
|
|
|
|
def require(condition, message):
|
|
if not condition:
|
|
raise SystemExit("FAIL: " + message)
|
|
|
|
|
|
now = time.time()
|
|
names = get_json("https://lb:9543/api/v1/label/__name__/values")["data"]
|
|
require("messenger_messenger_http_requests_total" in names, "API request counter missing")
|
|
require("messenger_messenger_http_request_duration_seconds_bucket" in names, "API duration histogram missing")
|
|
require("messenger_messenger_jobs_total" in names, "worker job counter missing")
|
|
require("messenger_messenger_job_duration_seconds_bucket" in names, "worker duration histogram missing")
|
|
|
|
series_query = urlencode({"match[]": "messenger_messenger_http_requests_total"})
|
|
series = get_json("https://lb:9543/api/v1/series?" + series_query)["data"]
|
|
for labels in series:
|
|
require(not {"user_id", "chat_id", "message_id", "attachment_id", "request_id"}.intersection(labels),
|
|
"high-cardinality identifier found in metric labels")
|
|
|
|
|
|
def find_correlated_log(service, event):
|
|
selector = '{service_name="' + service + '"} |= "' + event + '"'
|
|
query = urlencode({"query": selector, "limit": "20",
|
|
"start": str(int((now - 1800) * 1_000_000_000)),
|
|
"end": str(int(now * 1_000_000_000))})
|
|
result = get_json("https://lb:9643/loki/api/v1/query_range?" + query)["data"]["result"]
|
|
for stream in result:
|
|
trace_id = stream["stream"].get("trace_id")
|
|
span_id = stream["stream"].get("span_id")
|
|
if trace_id and span_id:
|
|
try:
|
|
trace = get_json("https://lb:9843/api/traces/" + trace_id)
|
|
if trace:
|
|
return True
|
|
except HTTPError as error:
|
|
if error.code != 404:
|
|
raise
|
|
return False
|
|
|
|
|
|
require(find_correlated_log("messenger-api", "http_request_completed"),
|
|
"API log with matching trace/span IDs and stored trace missing")
|
|
require(find_correlated_log("messenger-worker", "attachment_job_completed"),
|
|
"worker log with matching trace/span IDs and stored trace missing")
|
|
print("PASS: API/job metrics, bounded metric labels, Loki logs and matching Tempo traces")
|