Files
session_redis_public/monitoring_prometheus/controllers/prometheus_metrics.py
T
David Gallay 95f6a48b9d [REF] monitoring_prometheus: gather metrics on the fly in /metrics
The cron and the prometheus.metric table only existed to pass values from
the cron process to the HTTP workers. Querying the database directly when
/metrics is scraped removes the cron, the table and the staleness.
2026-10-01 11:55:24 +02:00

37 lines
1.1 KiB
Python

# Copyright 2016 Camptocamp SA
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html)
import logging
from prometheus_client import REGISTRY, Gauge, generate_latest
from odoo.http import Controller, request, route
_logger = logging.getLogger(__name__)
# Gauges must be instantiated only once per process, registering the same
# metric name twice raises a "Duplicated timeseries" error.
_GAUGES = {}
def _get_gauge(name, documentation, label_names, registry=REGISTRY):
gauge = _GAUGES.get(name)
if gauge is None:
gauge = Gauge(name, documentation, label_names, registry=registry)
_GAUGES[name] = gauge
return gauge
class PrometheusController(Controller):
@route("/metrics", auth="public")
def metrics(self):
# REGISTRY holds the in-process metrics (request latency, longpolling)
output = generate_latest(REGISTRY)
try:
gatherer = request.env["prometheus.gatherer"].sudo()
output += generate_latest(gatherer._build_prometheus_registry())
except Exception:
_logger.exception("Could not publish the gathered Prometheus metrics")
return output