Skip to content
Snippets Groups Projects
Commit 9f1a97b3 authored by Jan David Mol's avatar Jan David Mol
Browse files

Moved metrics to PyCommon

parent b5860885
No related branches found
Tags v0.0.23
1 merge request!1458Add custom monitoring points to LTA ingest, TMSS backend, and scheduling service
......@@ -17,18 +17,36 @@
import time
from prometheus_client import start_http_server, disable_created_metrics, Gauge
from prometheus_client import start_http_server, disable_created_metrics, Gauge, Histogram
def add_default_metrics():
def _add_default_metrics():
"""Create generic metrics that we expose by default."""
uptime = Gauge("uptime", "Uptime of the service")
start_time = time.time()
uptime.set_function(lambda: time.time() - start_time)
def start_metrics_server(port: int = 8000):
"""Start a Prometheus endpoint to scrape."""
disable_created_metrics()
start_http_server(port)
add_default_metrics()
_add_default_metrics()
def metric_track_duration():
"""Decorator for functions, to expose their call durations as a
Histogram in Prometheus."""
def inner(func):
func_name = func.__qualname__.replace(".","_")
metric = Histogram(f"{func_name}_duration")
@wraps(func)
def metric_wrapper(*args, **kwargs):
with metric.time():
return func(*args, **kwargs)
return metric_wrapper
return inner
......@@ -5,7 +5,6 @@ include(PythonInstall)
set(_py_files
qa_statistics_service.py
metrics.py
)
python_install(${_py_files}
......
......@@ -28,7 +28,7 @@ from prometheus_client import Counter, Gauge
from lofar.stationmodel.antennafields import (
antennafields_for_antennaset_and_station,
)
from lofar.sas.tmss.services.metrics import (
from lofar.common.metrics import (
start_metrics_server,
)
from lofar.sas.tmss.services.tuna.job_dispatcher import (
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment