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

Function _escape

prometheus_client/openmetrics/exposition.py:219–260  ·  view source on GitHub ↗

Performs backslash escaping on backslash, newline, and double-quote characters. valid_rune_fn takes the input character and its index in the containing string.

(s: str, escaping: str, valid_rune_fn: Callable[[str, int], bool])

Source from the content-addressed store, hash-verified

217
218
219def _escape(s: str, escaping: str, valid_rune_fn: Callable[[str, int], bool]) -> str:
220 """Performs backslash escaping on backslash, newline, and double-quote characters.
221
222 valid_rune_fn takes the input character and its index in the containing string."""
223 if escaping == ALLOWUTF8:
224 return s.replace('\\', r'\\').replace('\n', r'\n').replace('"', r'\"')
225 elif escaping == UNDERSCORES:
226 escaped = StringIO()
227 for i, b in enumerate(s):
228 if valid_rune_fn(b, i):
229 escaped.write(b)
230 else:
231 escaped.write('_')
232 return escaped.getvalue()
233 elif escaping == DOTS:
234 escaped = StringIO()
235 for i, b in enumerate(s):
236 if b == '_':
237 escaped.write('__')
238 elif b == '.':
239 escaped.write('_dot_')
240 elif valid_rune_fn(b, i):
241 escaped.write(b)
242 else:
243 escaped.write('__')
244 return escaped.getvalue()
245 elif escaping == VALUES:
246 escaped = StringIO()
247 escaped.write("U__")
248 for i, b in enumerate(s):
249 if b == '_':
250 escaped.write("__")
251 elif valid_rune_fn(b, i):
252 escaped.write(b)
253 elif not _is_valid_utf8(b):
254 escaped.write("_FFFD_")
255 else:
256 escaped.write('_')
257 escaped.write(format(ord(b), 'x'))
258 escaped.write('_')
259 return escaped.getvalue()
260 return s
261
262
263def _is_legacy_metric_rune(b: str, i: int) -> bool:

Callers 3

generate_latestFunction · 0.85
escape_metric_nameFunction · 0.85
escape_label_nameFunction · 0.85

Calls 2

_is_valid_utf8Function · 0.85
writeMethod · 0.80

Tested by

no test coverage detected