.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
4
5class OpenMetricsFamily: # pylint: disable=too-few-public-methods
6 """A family of metrics.
7 The key parameter is the metric name that should be used (snake case).
8 The type_hint parameter must be one of 'counter', 'gauge', 'histogram', 'summary'.
9 The help_hint parameter is a short string explaining the metric.
10 The data_info parameter is a dictionary of descriptionary parameters for the data point (e.g. request method/path).
11 The data parameter is a flat list of the actual data in shape of a primive type.
12
13 See https://github.com/OpenObservability/OpenMetrics/blob/main/specification/OpenMetrics.md for more information.
14 """
15
16 def __init__(self, key: str, type_hint: str, help_hint: str, data_info: list, data: list):
17 self.key = key
18 self.type_hint = type_hint
19 self.help_hint = help_hint
20 self.data_info = data_info
21 self.data = data
22
23 def __str__(self):
24 text_representation = f"""# HELP {self.key} {self.help_hint}
25# TYPE {self.key} {self.type_hint}
26"""
27
28 for i, data_info_dict in enumerate(self.data_info):
29 if not data_info_dict or not self.data[i]:
30 continue
31
32 info_representation = ','.join([f"{key}=\"{value}\"" for (key, value) in data_info_dict.items()])
33 text_representation += f"{self.key}{{{info_representation}}} {self.data[i]}\n"
34
35 return text_representation
__init__(self, str key, str type_hint, str help_hint, list data_info, list data)