summaryrefslogtreecommitdiff
path: root/searx/openmetrics.py
diff options
context:
space:
mode:
authorBnyro <bnyro@tutanota.com>2024-09-17 16:43:48 +0200
committerMarkus Heiser <markus.heiser@darmarIT.de>2024-11-24 14:25:49 +0100
commit8744dd3c71bcf29e8df7fdaf18448c3ff65c1035 (patch)
treee4c1b50f1aa53b8b2023ca8b5c1d94551b1b4cdb /searx/openmetrics.py
parent7927baf545013b829611834c20042c7b17e18ca0 (diff)
downloadsearxng-8744dd3c71bcf29e8df7fdaf18448c3ff65c1035.tar.gz
searxng-8744dd3c71bcf29e8df7fdaf18448c3ff65c1035.zip
[feat] metrics: support for open metrics
Diffstat (limited to 'searx/openmetrics.py')
-rw-r--r--searx/openmetrics.py35
1 files changed, 35 insertions, 0 deletions
diff --git a/searx/openmetrics.py b/searx/openmetrics.py
new file mode 100644
index 000000000..5232e05ca
--- /dev/null
+++ b/searx/openmetrics.py
@@ -0,0 +1,35 @@
+# SPDX-License-Identifier: AGPL-3.0-or-later
+"""Module providing support for displaying data in OpenMetrics format"""
+
+
+class OpenMetricsFamily: # pylint: disable=too-few-public-methods
+ """A family of metrics.
+ The key parameter is the metric name that should be used (snake case).
+ The type_hint parameter must be one of 'counter', 'gauge', 'histogram', 'summary'.
+ The help_hint parameter is a short string explaining the metric.
+ The data_info parameter is a dictionary of descriptionary parameters for the data point (e.g. request method/path).
+ The data parameter is a flat list of the actual data in shape of a primive type.
+
+ See https://github.com/OpenObservability/OpenMetrics/blob/main/specification/OpenMetrics.md for more information.
+ """
+
+ def __init__(self, key: str, type_hint: str, help_hint: str, data_info: list, data: list):
+ self.key = key
+ self.type_hint = type_hint
+ self.help_hint = help_hint
+ self.data_info = data_info
+ self.data = data
+
+ def __str__(self):
+ text_representation = f"""# HELP {self.key} {self.help_hint}
+# TYPE {self.key} {self.type_hint}
+"""
+
+ for i, data_info_dict in enumerate(self.data_info):
+ if not data_info_dict and data_info_dict != 0:
+ continue
+
+ info_representation = ','.join([f"{key}=\"{value}\"" for (key, value) in data_info_dict.items()])
+ text_representation += f"{self.key}{{{info_representation}}} {self.data[i]}\n"
+
+ return text_representation