mirror of
https://github.com/camptocamp/odoo-cloud-platform.git
synced 2026-10-23 14:09:31 +00:00
This commit features a refactoring of monitoring_prometheus to get a cron gathering some metrics from the database, and then publishing the result on the /metrics endpoint The new module monitoring_prometheus_queue_job extends the cron to get information about the queue job states.
53 lines
1.6 KiB
Python
53 lines
1.6 KiB
Python
# Copyright 2016 Camptocamp SA
|
|
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html)
|
|
|
|
import json
|
|
import logging
|
|
|
|
from prometheus_client import 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):
|
|
gauge = _GAUGES.get(name)
|
|
if gauge is None:
|
|
gauge = Gauge(name, documentation, label_names)
|
|
_GAUGES[name] = gauge
|
|
return gauge
|
|
|
|
|
|
class PrometheusController(Controller):
|
|
def _publish_gathered_metrics(self):
|
|
records = request.env["prometheus.metric"].sudo().search([])
|
|
if not records:
|
|
return
|
|
|
|
cleared = set()
|
|
for record in records:
|
|
name = record.name
|
|
labels = json.loads(record.labels or "{}")
|
|
gauge = _get_gauge(name, record.documentation or "", sorted(labels))
|
|
if name not in cleared:
|
|
# drop the series that disappeared since the last collection
|
|
gauge.clear()
|
|
cleared.add(name)
|
|
if labels:
|
|
gauge.labels(**labels).set(record.value)
|
|
else:
|
|
gauge.set(record.value)
|
|
|
|
@route("/metrics", auth="public")
|
|
def metrics(self):
|
|
try:
|
|
self._publish_gathered_metrics()
|
|
except Exception:
|
|
_logger.exception("Could not publish the gathered Prometheus metrics")
|
|
return generate_latest()
|