Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions prometheus_client/exposition.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@
)
from wsgiref.simple_server import make_server, WSGIRequestHandler, WSGIServer

from packaging.version import Version

from .openmetrics import exposition as openmetrics
from .registry import CollectorRegistry, REGISTRY
from .utils import floatToGoString
from .utils import floatToGoString, parse_version

__all__ = (
'CONTENT_TYPE_LATEST',
Expand Down Expand Up @@ -346,7 +344,7 @@ def choose_encoder(accept_header: str) -> Tuple[Callable[[CollectorRegistry], by
# mimetype.
if not version:
return (partial(openmetrics.generate_latest, escaping=openmetrics.UNDERSCORES, version="1.0.0"), openmetrics.CONTENT_TYPE_LATEST)
if version and Version(version) >= Version('1.0.0'):
if version and parse_version(version) >= (1, 0, 0):
return (partial(openmetrics.generate_latest, escaping=escaping, version=version),
f'application/openmetrics-text; version={version}; charset=utf-8; escaping=' + str(escaping))
elif accepted.split(';')[0].strip() == 'text/plain':
Expand All @@ -355,7 +353,7 @@ def choose_encoder(accept_header: str) -> Tuple[Callable[[CollectorRegistry], by
escaping = _get_escaping(toks)
# Only return an escaping header if we have a good version and
# mimetype.
if version and Version(version) >= Version('1.0.0'):
if version and parse_version(version) >= (1, 0, 0):
return (partial(generate_latest, escaping=escaping),
CONTENT_TYPE_LATEST + '; escaping=' + str(escaping))
return generate_latest, CONTENT_TYPE_PLAIN_0_0_4
Expand Down
6 changes: 2 additions & 4 deletions prometheus_client/openmetrics/exposition.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
from sys import maxunicode
from typing import Callable

from packaging.version import Version

from ..utils import floatToGoString
from ..utils import floatToGoString, parse_version
from ..validation import (
_is_valid_legacy_labelname, _is_valid_legacy_metric_name,
)
Expand Down Expand Up @@ -94,7 +92,7 @@ def generate_latest(registry, escaping=UNDERSCORES, version="1.0.0"):
timestamp = f' {s.timestamp}'

# Skip native histogram samples entirely if version < 2.0.0
if s.native_histogram and Version(version) < Version('2.0.0'):
if s.native_histogram and parse_version(version) < (2, 0, 0):
continue

native_histogram = ''
Expand Down
12 changes: 12 additions & 0 deletions prometheus_client/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import math
from typing import Union

INF = float("inf")
MINUS_INF = float("-inf")
Expand All @@ -22,3 +23,14 @@ def floatToGoString(d):
mantissa = f'{s[0]}.{s[1:dot]}{s[dot + 1:]}'.rstrip('0.')
return f'{mantissa}e+0{dot - 1}'
return s


def parse_version(version_str: str) -> tuple[Union[int, str], ...]:
version: list[Union[int, str]] = []
for part in version_str.split('.'):
try:
version.append(int(part))
except ValueError:
version.append(part)

return tuple(version)