openwebrx-clone/owrx/controllers/metrics.py

31 lines
1.1 KiB
Python
Raw Permalink Normal View History

from . import Controller
2021-02-01 17:43:14 +00:00
from owrx.metrics import CounterMetric, DirectMetric, Metrics
import json
class MetricsController(Controller):
def indexAction(self):
2021-02-01 17:43:14 +00:00
data = json.dumps(Metrics.getSharedInstance().getHierarchicalMetrics())
self.send_response(data, content_type="application/json")
2021-02-01 17:43:14 +00:00
def prometheusAction(self):
metrics = Metrics.getSharedInstance().getFlatMetrics()
def prometheusFormat(key, metric):
value = metric.getValue()
if isinstance(metric, CounterMetric):
key += "_total"
value = value["count"]
elif isinstance(metric, DirectMetric):
pass
else:
raise ValueError("Unexpected metric type for metric {}".format(repr(metric)))
return "{key} {value}".format(key=key.replace(".", "_"), value=value)
data = ["# https://prometheus.io/docs/instrumenting/exposition_formats/"] + [
prometheusFormat(k, v) for k, v in metrics.items()
]
self.send_response("\n".join(data), content_type="text/plain; version=0.0.4")