40 lines
1.7 KiB
Python
40 lines
1.7 KiB
Python
from __future__ import annotations
|
|
|
|
from sanic import response
|
|
|
|
|
|
async def s_api_metrics(request):
|
|
try:
|
|
from prometheus_client import generate_latest, CONTENT_TYPE_LATEST # type: ignore
|
|
data = generate_latest()
|
|
return response.raw(data, content_type=CONTENT_TYPE_LATEST)
|
|
except Exception:
|
|
# Fallback: export minimal in-process counters from DHT module, if available
|
|
try:
|
|
from app.core.network.dht import prometheus as dprom
|
|
|
|
def dump(metric_obj, metric_name):
|
|
lines = []
|
|
values = getattr(metric_obj, "_values", {})
|
|
for labels, value in values.items():
|
|
label_str = ",".join(f'{k}="{v}"' for k, v in labels)
|
|
if label_str:
|
|
lines.append(f"{metric_name}{{{label_str}}} {value}")
|
|
else:
|
|
lines.append(f"{metric_name} {value}")
|
|
return lines
|
|
|
|
parts = []
|
|
parts += dump(dprom.replication_under, "dht_replication_under_total")
|
|
parts += dump(dprom.replication_over, "dht_replication_over_total")
|
|
parts += dump(dprom.leader_changes, "dht_leader_changes_total")
|
|
parts += dump(dprom.merge_conflicts, "dht_merge_conflicts_total")
|
|
parts += dump(dprom.view_count_total, "dht_view_count_total")
|
|
parts += dump(dprom.unique_estimate, "dht_unique_view_estimate")
|
|
parts += dump(dprom.watch_time_seconds, "dht_watch_time_seconds")
|
|
body = "\n".join(parts) + ("\n" if parts else "")
|
|
return response.text(body, content_type="text/plain; version=0.0.4")
|
|
except Exception:
|
|
return response.text("")
|
|
|