Files
openwebrx-clone/owrx/controllers/metrics.py
Kristian Glass b01d08c3e8 Expose Prometheus-compatible metrics at /metrics
This is a rough first-draft change, with multiple flaws, intended to
just start discussion around the feature request.

Issues include:

* It breaks compatibility, replacing the previous JSON /metrics
* It crudely hand-generates Prometheus-formatted data
* It would probably be better if it used
https://pypi.org/project/prometheus-client/

But it works locally and I have some nice graphs!
2020-12-22 22:34:14 +00:00

25 lines
830 B
Python

from . import Controller
from owrx.metrics import CounterMetric, DirectMetric, Metrics
class MetricsController(Controller):
def indexAction(self):
metrics = Metrics.getSharedInstance().metrics
data = "# https://prometheus.io/docs/instrumenting/exposition_formats/\n"
for key,metric in metrics.items():
value = -1
if isinstance(metric, CounterMetric):
key += "_total"
value = metric.getValue()["count"]
elif isinstance(metric, DirectMetric):
value = metric.getValue()
else:
raise ValueError("Unexpected metric type for metric %s" % repr(metric))
data += "%s %s\n" % (key.replace(".", "_"), value)
self.send_response(data, content_type="text/plain; version=0.0.4")