openwebrx-clone/owrx/controllers/metrics.py

33 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
import re
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=re.sub('[^a-zA-Z0-9:_]', '_', key), value=value)
2021-02-01 17:43:14 +00:00
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")