MCPcopy Index your code
hub / github.com/prometheus/client_python / escape_label_name

Function escape_label_name

prometheus_client/openmetrics/exposition.py:196–216  ·  view source on GitHub ↗

Escapes the label 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

194
195
196def escape_label_name(s: str, escaping: str = UNDERSCORES) -> str:
197 """Escapes the label name and puts it in quotes iff the name does not
198 conform to the legacy Prometheus character set.
199 """
200 if len(s) == 0:
201 return s
202 if escaping == ALLOWUTF8:
203 if not _is_valid_legacy_labelname(s):
204 return '"{}"'.format(_escape(s, escaping, _is_legacy_labelname_rune))
205 return _escape(s, escaping, _is_legacy_labelname_rune)
206 elif escaping == UNDERSCORES:
207 if _is_valid_legacy_labelname(s):
208 return s
209 return _escape(s, escaping, _is_legacy_labelname_rune)
210 elif escaping == DOTS:
211 return _escape(s, escaping, _is_legacy_labelname_rune)
212 elif escaping == VALUES:
213 if _is_valid_legacy_labelname(s):
214 return s
215 return _escape(s, escaping, _is_legacy_labelname_rune)
216 return s
217
218
219def _escape(s: str, escaping: str, valid_rune_fn: Callable[[str, int], bool]) -> str:

Callers 2

test_escape_label_nameFunction · 0.90
generate_latestFunction · 0.85

Calls 2

_escapeFunction · 0.85

Tested by 1

test_escape_label_nameFunction · 0.72