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

Function choose_encoder

prometheus_client/exposition.py:343–369  ·  view source on GitHub ↗
(accept_header: str)

Source from the content-addressed store, hash-verified

341
342
343def choose_encoder(accept_header: str) -> Tuple[Callable[[Collector], bytes], str]:
344 # Python client library accepts a narrower range of content-types than
345 # Prometheus does.
346 accept_header = accept_header or ''
347 escaping = openmetrics.UNDERSCORES
348 for accepted in accept_header.split(','):
349 if accepted.split(';')[0].strip() == 'application/openmetrics-text':
350 toks = accepted.split(';')
351 version = _get_version(toks)
352 escaping = _get_escaping(toks)
353 # Only return an escaping header if we have a good version and
354 # mimetype.
355 if not version:
356 return (partial(openmetrics.generate_latest, escaping=openmetrics.UNDERSCORES, version="1.0.0"), openmetrics.CONTENT_TYPE_LATEST)
357 if version and parse_version(version) >= (1, 0, 0):
358 return (partial(openmetrics.generate_latest, escaping=escaping, version=version),
359 f'application/openmetrics-text; version={version}; charset=utf-8; escaping=' + str(escaping))
360 elif accepted.split(';')[0].strip() == 'text/plain':
361 toks = accepted.split(';')
362 version = _get_version(toks)
363 escaping = _get_escaping(toks)
364 # Only return an escaping header if we have a good version and
365 # mimetype.
366 if version and parse_version(version) >= (1, 0, 0):
367 return (partial(generate_latest, escaping=escaping),
368 CONTENT_TYPE_LATEST + '; escaping=' + str(escaping))
369 return generate_latest, CONTENT_TYPE_PLAIN_0_0_4
370
371
372def _get_version(accept_header: List[str]) -> str:

Callers 12

test_default_encoderMethod · 0.90
test_plain_encoderMethod · 0.90
test_openmetrics_utf8Method · 0.90
test_prom_latestMethod · 0.90
test_prom_plain_1_0_0Method · 0.90
test_prom_utf8Method · 0.90
test_prom_no_versionMethod · 0.90
_bake_outputFunction · 0.85

Calls 3

_get_versionFunction · 0.85
_get_escapingFunction · 0.85
parse_versionFunction · 0.85

Tested by 11

test_default_encoderMethod · 0.72
test_plain_encoderMethod · 0.72
test_openmetrics_utf8Method · 0.72
test_prom_latestMethod · 0.72
test_prom_plain_1_0_0Method · 0.72
test_prom_utf8Method · 0.72
test_prom_no_versionMethod · 0.72