diff options
| author | Markus Heiser <markus.heiser@darmarit.de> | 2025-08-22 17:17:51 +0200 |
|---|---|---|
| committer | Markus Heiser <markus.heiser@darmarIT.de> | 2025-09-03 13:37:36 +0200 |
| commit | 57b9673efb1b4fd18a3ac15e26da642201e2cd33 (patch) | |
| tree | 79d3ecd365a1669a1109aa7e5dd3636bc1041d96 /searx/openmetrics.py | |
| parent | 09500459feffa414dc7a0601bdb164464a8b0454 (diff) | |
[mod] addition of various type hints / tbc
- pyright configuration [1]_
- stub files: types-lxml [2]_
- addition of various type hints
- enable use of new type system features on older Python versions [3]_
- ``.tool-versions`` - set python to lowest version we support (3.10.18) [4]_:
Older versions typically lack some typing features found in newer Python
versions. Therefore, for local type checking (before commit), it is necessary
to use the older Python interpreter.
.. [1] https://docs.basedpyright.com/v1.20.0/configuration/config-files/
.. [2] https://pypi.org/project/types-lxml/
.. [3] https://typing-extensions.readthedocs.io/en/latest/#
.. [4] https://mise.jdx.dev/configuration.html#tool-versions
Signed-off-by: Markus Heiser <markus.heiser@darmarit.de>
Format: reST
Diffstat (limited to 'searx/openmetrics.py')
| -rw-r--r-- | searx/openmetrics.py | 48 |
1 files changed, 33 insertions, 15 deletions
diff --git a/searx/openmetrics.py b/searx/openmetrics.py index a9ea01e08..340840320 100644 --- a/searx/openmetrics.py +++ b/searx/openmetrics.py @@ -1,27 +1,45 @@ # SPDX-License-Identifier: AGPL-3.0-or-later """Module providing support for displaying data in OpenMetrics format""" +import typing as t + +OMFTypeHintType = t.Literal["counter", "gauge", "histogram", "summary"] +OMFDataInfoType = list[dict[str, str]] +OMFDataType = list[t.Any] + 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 primitive type. - See https://github.com/OpenObservability/OpenMetrics/blob/main/specification/OpenMetrics.md for more information. + - 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 + primitive type. + + See `OpenMetrics specification`_ for more information. + + .. _OpenMetrics specification: + https://github.com/prometheus/OpenMetrics/blob/main/specification/OpenMetrics.txt + """ - 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 __init__( + self, key: str, type_hint: OMFTypeHintType, help_hint: str, data_info: OMFDataInfoType, data: list[t.Any] + ): + self.key: str = key + self.type_hint: OMFTypeHintType = type_hint + self.help_hint: str = help_hint + self.data_info: OMFDataInfoType = data_info + self.data: OMFDataType = data def __str__(self): - text_representation = f"""# HELP {self.key} {self.help_hint} + text_representation = f"""\ +# HELP {self.key} {self.help_hint} # TYPE {self.key} {self.type_hint} """ @@ -29,7 +47,7 @@ class OpenMetricsFamily: # pylint: disable=too-few-public-methods if not data_info_dict or not self.data[i]: 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" + 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 |