Add prometheus metrics endpoint. Closes #67

This commit is contained in:
Ian Renton
2025-10-30 13:32:10 +00:00
parent a9003162cc
commit 9768f976c5
4 changed files with 67 additions and 2 deletions

View File

@@ -0,0 +1,40 @@
from bottle import response
from prometheus_client import CollectorRegistry, generate_latest, CONTENT_TYPE_LATEST, Counter, disable_created_metrics, \
Gauge
disable_created_metrics()
# Prometheus metrics registry
registry = CollectorRegistry()
page_requests_counter = Counter(
"page_requests",
"Total number of page requests received",
registry=registry,
)
api_requests_counter = Counter(
"api_requests",
"Total number of API requests received",
registry=registry
)
spots_gauge = Gauge(
"spots",
"Number of spots currently in the software",
registry=registry
)
alerts_gauge = Gauge(
"alerts",
"Number of alerts currently in the software",
registry=registry
)
memory_use_gauge = Gauge(
"memory_usage_bytes",
"Current memory usage of the software in bytes",
registry=registry
)
# Get a Prometheus metrics response for Bottle
def get_metrics():
response.content_type = CONTENT_TYPE_LATEST
response.status = 200
return generate_latest(registry)

View File

@@ -7,6 +7,7 @@ import pytz
from core.config import SERVER_OWNER_CALLSIGN
from core.constants import SOFTWARE_VERSION
from core.prometheus_metrics_handler import memory_use_gauge, spots_gauge, alerts_gauge
# Provides a timed update of the application's status data.
@@ -60,8 +61,15 @@ class StatusReporter:
self.status_data["webserver"] = {"status": self.web_server.status,
"last_api_access": self.web_server.last_api_access_time.replace(
tzinfo=pytz.UTC).timestamp() if self.web_server.last_api_access_time else 0,
"api_access_count": self.web_server.api_access_counter,
"last_page_access": self.web_server.last_page_access_time.replace(
tzinfo=pytz.UTC).timestamp() if self.web_server.last_page_access_time else 0}
tzinfo=pytz.UTC).timestamp() if self.web_server.last_page_access_time else 0,
"page_access_count": self.web_server.page_access_counter}
# Update Prometheus metrics
memory_use_gauge.set(psutil.Process(os.getpid()).memory_info().rss * 1024)
spots_gauge.set(len(self.spots))
alerts_gauge.set(len(self.alerts))
self.run_timer = Timer(self.run_interval, self.run)
self.run_timer.start()