MCPcopy Create free account
hub / github.com/prometheus/client_python / escape_metric_name

Function escape_metric_name

prometheus_client/openmetrics/exposition.py:173–193  ·  view source on GitHub ↗

Escapes the metric name and puts it in quotes iff the name does not conform to the legacy Prometheus character set.

(s: str, escaping: str = UNDERSCORES)

Source from the content-addressed store, hash-verified

171
172
173def escape_metric_name(s: str, escaping: str = UNDERSCORES) -> str:
174 """Escapes the metric name and puts it in quotes iff the name does not
175 conform to the legacy Prometheus character set.
176 """
177 if len(s) == 0:
178 return s
179 if escaping == ALLOWUTF8:
180 if not _is_valid_legacy_metric_name(s):
181 return '"{}"'.format(_escape(s, escaping, _is_legacy_metric_rune))
182 return _escape(s, escaping, _is_legacy_metric_rune)
183 elif escaping == UNDERSCORES:
184 if _is_valid_legacy_metric_name(s):
185 return s
186 return _escape(s, escaping, _is_legacy_metric_rune)
187 elif escaping == DOTS:
188 return _escape(s, escaping, _is_legacy_metric_rune)
189 elif escaping == VALUES:
190 if _is_valid_legacy_metric_name(s):
191 return s
192 return _escape(s, escaping, _is_legacy_metric_rune)
193 return s
194
195
196def escape_label_name(s: str, escaping: str = UNDERSCORES) -> str:

Callers 2

test_escape_metric_nameFunction · 0.90
generate_latestFunction · 0.85

Calls 2

_escapeFunction · 0.85

Tested by 1

test_escape_metric_nameFunction · 0.72