From a369050b042a672efe1b5925057ab96c2fc1c7f7 Mon Sep 17 00:00:00 2001 From: Alexandre Fayolle Date: Fri, 18 Sep 2026 12:29:34 +0200 Subject: [PATCH] fixup! fixup! [ADD] monitoring_prometheus_queue_job --- monitoring_prometheus/__init__.py | 2 +- monitoring_prometheus/models/__init__.py | 2 +- .../models/prometheus_metric.py | 49 +++++++++++++++++++ .../security/ir.model.access.csv | 2 + 4 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 monitoring_prometheus/models/prometheus_metric.py create mode 100644 monitoring_prometheus/security/ir.model.access.csv diff --git a/monitoring_prometheus/__init__.py b/monitoring_prometheus/__init__.py index 91c5580..f7209b1 100644 --- a/monitoring_prometheus/__init__.py +++ b/monitoring_prometheus/__init__.py @@ -1,2 +1,2 @@ -from . import controllers from . import models +from . import controllers diff --git a/monitoring_prometheus/models/__init__.py b/monitoring_prometheus/models/__init__.py index 3dd0e8c..472b8a2 100644 --- a/monitoring_prometheus/models/__init__.py +++ b/monitoring_prometheus/models/__init__.py @@ -1,3 +1,3 @@ from . import ir_http -from . import prometheus_metric from . import prometheus_gatherer +from . import prometheus_metric diff --git a/monitoring_prometheus/models/prometheus_metric.py b/monitoring_prometheus/models/prometheus_metric.py new file mode 100644 index 0000000..3544c23 --- /dev/null +++ b/monitoring_prometheus/models/prometheus_metric.py @@ -0,0 +1,49 @@ +# Copyright 2016-2021 Camptocamp SA +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html) + +import json + +from odoo import api, fields, models + + +class PrometheusMetric(models.Model): + """Store the metrics gathered by ``prometheus.gatherer``. + + A record is stored per metric name/labels combination, because a single + metric name can be reported several times with different labels (e.g. + one gauge value per queue job state and channel). + """ + + _name = "prometheus.metric" + _description = "Prometheus Metric" + + name = fields.Char(required=True, index=True) + documentation = fields.Char() + # JSON-serialized dict, used both to publish the metric and as part of + # the uniqueness key alongside ``name``. + labels = fields.Char(default="{}") + value = fields.Float(required=True) + + @staticmethod + def _labels_key(labels): + return json.dumps(labels or {}, sort_keys=True) + + @api.model + def _update_metric(self, metric): + """Create or update the record matching ``metric``, return it.""" + labels_key = self._labels_key(metric.get("labels")) + record = self.search( + [("name", "=", metric["name"]), ("labels", "=", labels_key)], + limit=1, + ) + values = { + "name": metric["name"], + "documentation": metric.get("documentation", ""), + "labels": labels_key, + "value": metric["value"], + } + if record: + record.write(values) + else: + record = self.create(values) + return record diff --git a/monitoring_prometheus/security/ir.model.access.csv b/monitoring_prometheus/security/ir.model.access.csv new file mode 100644 index 0000000..3da8765 --- /dev/null +++ b/monitoring_prometheus/security/ir.model.access.csv @@ -0,0 +1,2 @@ +id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink +access_prometheus_metric_system,prometheus.metric system,model_prometheus_metric,base.group_system,1,1,1,1