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

@@ -6,9 +6,11 @@ from threading import Thread
import bottle
import pytz
from bottle import run, request, response, template
from prometheus_client import CONTENT_TYPE_LATEST, generate_latest
from core.config import MAX_SPOT_AGE, ALLOW_SPOTTING
from core.constants import BANDS, ALL_MODES, MODE_TYPES, SIGS, CONTINENTS, SOFTWARE_VERSION
from core.prometheus_metrics_handler import page_requests_counter, registry, get_metrics, api_requests_counter
from data.spot import Spot
@@ -19,6 +21,8 @@ class WebServer:
def __init__(self, spots, alerts, status_data, port):
self.last_page_access_time = None
self.last_api_access_time = None
self.page_access_counter = 0
self.api_access_counter = 0
self.spots = spots
self.alerts = alerts
self.status_data = status_data
@@ -44,6 +48,8 @@ class WebServer:
bottle.get("/status")(lambda: self.serve_template('webpage_status'))
bottle.get("/about")(lambda: self.serve_template('webpage_about'))
bottle.get("/apidocs")(lambda: self.serve_template('webpage_apidocs'))
# Route for Prometheus metrics
bottle.get("/metrics")(lambda: self.serve_prometheus_metrics())
# Default route to serve from "webassets"
bottle.get("/<filepath:path>")(self.serve_static_file)
@@ -92,6 +98,8 @@ class WebServer:
# Serve a JSON API endpoint
def serve_api(self, data):
self.last_api_access_time = datetime.now(pytz.UTC)
self.api_access_counter += 1
api_requests_counter.inc()
self.status = "OK"
response.content_type = 'application/json'
response.set_header('Cache-Control', 'no-store')
@@ -100,6 +108,8 @@ class WebServer:
# Accept a spot
def accept_spot(self):
self.last_api_access_time = datetime.now(pytz.UTC)
self.api_access_counter += 1
api_requests_counter.inc()
self.status = "OK"
try:
@@ -153,6 +163,8 @@ class WebServer:
# Serve a templated page
def serve_template(self, template_name):
self.last_page_access_time = datetime.now(pytz.UTC)
self.page_access_counter += 1
page_requests_counter.inc()
self.status = "OK"
return template(template_name)
@@ -160,6 +172,10 @@ class WebServer:
def serve_static_file(self, filepath):
return bottle.static_file(filepath, root="webassets")
# Serve Prometheus metrics
def serve_prometheus_metrics(self):
return get_metrics()
# Utility method to apply filters to the overall spot list and return only a subset. Enables query parameters in
# the main "spots" GET call.
def get_spot_list_with_filters(self):