|
| 1 | +from __future__ import annotations |
| 2 | + |
1 | 3 | from logging import getLogger |
| 4 | +from typing import Optional |
2 | 5 |
|
3 | 6 | from fastapi import APIRouter, Depends |
| 7 | +from prometheus_client import Counter, Gauge |
4 | 8 | from sqlmodel import select |
5 | 9 |
|
6 | 10 | import murfey.server.prometheus as prom |
@@ -90,3 +94,30 @@ def increment_rsync_transferred_files_prometheus( |
90 | 94 | def change_monitoring_status(visit_name: str, on: int): |
91 | 95 | prom.monitoring_switch.labels(visit=visit_name) |
92 | 96 | prom.monitoring_switch.labels(visit=visit_name).set(on) |
| 97 | + |
| 98 | + |
| 99 | +@router.get("metrics/{metric_name}") |
| 100 | +def inspect_prometheus_metrics( |
| 101 | + metric_name: str, |
| 102 | +): |
| 103 | + """ |
| 104 | + A debugging endpoint that returns the current contents of any Prometheus |
| 105 | + gauges and counters that have been set up thus far. |
| 106 | + """ |
| 107 | + |
| 108 | + # Extract the Prometheus metric defined in the Prometheus module |
| 109 | + metric: Optional[Counter | Gauge] = getattr(prom, metric_name, None) |
| 110 | + if metric is None or not isinstance(metric, (Counter, Gauge)): |
| 111 | + raise LookupError("No matching metric was found") |
| 112 | + |
| 113 | + # Package contents into dict and return |
| 114 | + results = {} |
| 115 | + if hasattr(metric, "_metrics"): |
| 116 | + for i, (label_tuple, sub_metric) in enumerate(metric._metrics.items()): |
| 117 | + labels = dict(zip(metric._labelnames, label_tuple)) |
| 118 | + labels["value"] = sub_metric._value.get() |
| 119 | + results[i] = labels |
| 120 | + return results |
| 121 | + else: |
| 122 | + value = metric._value.get() |
| 123 | + return {"value": value} |
0 commit comments