.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
openmetrics.py
Go to the documentation of this file.
1# SPDX-License-Identifier: AGPL-3.0-or-later
2"""Module providing support for displaying data in OpenMetrics format"""
3
4import typing as t
5
6OMFTypeHintType = t.Literal["counter", "gauge", "histogram", "summary"]
7OMFDataInfoType = list[dict[str, str]]
8OMFDataType = list[t.Any]
9
10
11class OpenMetricsFamily: # pylint: disable=too-few-public-methods
12 """A family of metrics.
13
14 - The ``key`` parameter is the metric name that should be used (snake case).
15 - The ``type_hint`` parameter must be one of ``counter``, ``gauge``,
16 ``histogram``, ``summary``.
17 - The ``help_hint`` parameter is a short string explaining the metric.
18 - The data_info parameter is a dictionary of descriptionary parameters for
19 the data point (e.g. request method/path).
20
21 - The data parameter is a flat list of the actual data in shape of a
22 primitive type.
23
24 See `OpenMetrics specification`_ for more information.
25
26 .. _OpenMetrics specification:
27 https://github.com/prometheus/OpenMetrics/blob/main/specification/OpenMetrics.txt
28
29 """
30
32 self, key: str, type_hint: OMFTypeHintType, help_hint: str, data_info: OMFDataInfoType, data: list[t.Any]
33 ):
34 self.key: str = key
35 self.type_hint: OMFTypeHintType = type_hint
36 self.help_hint: str = help_hint
37 self.data_info: OMFDataInfoType = data_info
38 self.data: OMFDataType = data
39
40 def __str__(self):
41 text_representation = f"""\
42# HELP {self.key} {self.help_hint}
43# TYPE {self.key} {self.type_hint}
44"""
45
46 for i, data_info_dict in enumerate(self.data_info):
47 if not data_info_dict or not self.data[i]:
48 continue
49
50 info_representation = ','.join([f'{key}="{value}"' for (key, value) in data_info_dict.items()])
51 text_representation += f'{self.key}{{{info_representation}}} {self.data[i]}\n'
52
53 return text_representation
__init__(self, str key, OMFTypeHintType type_hint, str help_hint, OMFDataInfoType data_info, list[t.Any] data)