Skip to content

Commit 592738c

Browse files
committed
fix: use tuples instead of packaging Version
Signed-off-by: Ruslan Kuprieiev <kupruser@gmail.com>
1 parent b9e78a3 commit 592738c

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
lines changed

prometheus_client/exposition.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,10 @@
1818
)
1919
from wsgiref.simple_server import make_server, WSGIRequestHandler, WSGIServer
2020

21-
from packaging.version import Version
2221

2322
from .openmetrics import exposition as openmetrics
2423
from .registry import CollectorRegistry, REGISTRY
25-
from .utils import floatToGoString
24+
from .utils import floatToGoString, parse_version
2625

2726
__all__ = (
2827
'CONTENT_TYPE_LATEST',
@@ -346,7 +345,7 @@ def choose_encoder(accept_header: str) -> Tuple[Callable[[CollectorRegistry], by
346345
# mimetype.
347346
if not version:
348347
return (partial(openmetrics.generate_latest, escaping=openmetrics.UNDERSCORES, version="1.0.0"), openmetrics.CONTENT_TYPE_LATEST)
349-
if version and Version(version) >= Version('1.0.0'):
348+
if version and parse_version(version) >= (1, 0, 0):
350349
return (partial(openmetrics.generate_latest, escaping=escaping, version=version),
351350
f'application/openmetrics-text; version={version}; charset=utf-8; escaping=' + str(escaping))
352351
elif accepted.split(';')[0].strip() == 'text/plain':
@@ -355,7 +354,7 @@ def choose_encoder(accept_header: str) -> Tuple[Callable[[CollectorRegistry], by
355354
escaping = _get_escaping(toks)
356355
# Only return an escaping header if we have a good version and
357356
# mimetype.
358-
if version and Version(version) >= Version('1.0.0'):
357+
if version and parse_version(version) >= (1, 0, 0):
359358
return (partial(generate_latest, escaping=escaping),
360359
CONTENT_TYPE_LATEST + '; escaping=' + str(escaping))
361360
return generate_latest, CONTENT_TYPE_PLAIN_0_0_4

prometheus_client/openmetrics/exposition.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
from sys import maxunicode
55
from typing import Callable
66

7-
from packaging.version import Version
87

9-
from ..utils import floatToGoString
8+
from ..utils import floatToGoString, parse_version
109
from ..validation import (
1110
_is_valid_legacy_labelname, _is_valid_legacy_metric_name,
1211
)
1312

13+
1414
CONTENT_TYPE_LATEST = 'application/openmetrics-text; version=1.0.0; charset=utf-8'
1515
"""Content type of the latest OpenMetrics 1.0 text format"""
1616
CONTENT_TYPE_LATEST_2_0 = 'application/openmetrics-text; version=2.0.0; charset=utf-8'
@@ -94,7 +94,7 @@ def generate_latest(registry, escaping=UNDERSCORES, version="1.0.0"):
9494
timestamp = f' {s.timestamp}'
9595

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

100100
native_histogram = ''

prometheus_client/utils.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,12 @@ def floatToGoString(d):
2222
mantissa = f'{s[0]}.{s[1:dot]}{s[dot + 1:]}'.rstrip('0.')
2323
return f'{mantissa}e+0{dot - 1}'
2424
return s
25+
26+
27+
def parse_version(version_str: str) -> tuple:
28+
if not version_str:
29+
return (0, 0, 0)
30+
try:
31+
return tuple(int(x) for x in version_str.split('.'))
32+
except ValueError:
33+
return (0, 0, 0)

0 commit comments

Comments
 (0)