From 0c5b6117c2cb9a8ca453615d749b9e6a90a407c5 Mon Sep 17 00:00:00 2001 From: George Mossessian Date: Sun, 22 Dec 2019 16:26:01 -0500 Subject: [PATCH 1/2] set http prefix in python geq 3.7.6 Signed-off-by: George Mossessian --- prometheus_client/exposition.py | 8 ++++++-- tests/test_exposition.py | 7 +++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/prometheus_client/exposition.py b/prometheus_client/exposition.py index 221cbedf..4cac223f 100644 --- a/prometheus_client/exposition.py +++ b/prometheus_client/exposition.py @@ -29,7 +29,8 @@ """Content type of the latest text format""" PYTHON26_OR_OLDER = sys.version_info < (2, 7) - +# Due to https://bugs.python.org/issue27657: +PYTHON376_OR_NEWER = sys.version_info > (3, 7, 5) def make_wsgi_app(registry=REGISTRY): """Create a WSGI app which serves the metrics from a registry.""" @@ -341,7 +342,10 @@ def delete_from_gateway( def _use_gateway(method, gateway, job, registry, grouping_key, timeout, handler): gateway_url = urlparse(gateway) - if not gateway_url.scheme or (PYTHON26_OR_OLDER and gateway_url.scheme not in ['http', 'https']): + if not gateway_url.scheme or ( + (PYTHON376_OR_NEWER or PYTHON26_OR_OLDER) + and gateway_url.scheme not in ['http', 'https'] + ): gateway = 'http://{0}'.format(gateway) url = '{0}/metrics/{1}/{2}'.format(gateway, *_escape_grouping_key("job", job)) diff --git a/tests/test_exposition.py b/tests/test_exposition.py index db73e237..47c200f3 100644 --- a/tests/test_exposition.py +++ b/tests/test_exposition.py @@ -235,6 +235,13 @@ def test_push(self): self.assertEqual(self.requests[0][0].headers.get('content-type'), CONTENT_TYPE_LATEST) self.assertEqual(self.requests[0][1], b'# HELP g help\n# TYPE g gauge\ng 0.0\n') + def test_push_schemeless_url(self): + push_to_gateway(self.address.replace('http://', ''), "my_job", self.registry) + self.assertEqual(self.requests[0][0].command, 'PUT') + self.assertEqual(self.requests[0][0].path, '/metrics/job/my_job') + self.assertEqual(self.requests[0][0].headers.get('content-type'), CONTENT_TYPE_LATEST) + self.assertEqual(self.requests[0][1], b'# HELP g help\n# TYPE g gauge\ng 0.0\n') + def test_push_with_groupingkey(self): push_to_gateway(self.address, "my_job", self.registry, {'a': 9}) self.assertEqual(self.requests[0][0].command, 'PUT') From 41a183154c8f0f85c33169ef0b76d0d80b46cf1b Mon Sep 17 00:00:00 2001 From: George Mossessian Date: Mon, 23 Dec 2019 10:45:19 -0500 Subject: [PATCH 2/2] move comment on py 3.7.6 to where it is used Signed-off-by: George Mossessian --- prometheus_client/exposition.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/prometheus_client/exposition.py b/prometheus_client/exposition.py index 4cac223f..6911ba75 100644 --- a/prometheus_client/exposition.py +++ b/prometheus_client/exposition.py @@ -29,7 +29,6 @@ """Content type of the latest text format""" PYTHON26_OR_OLDER = sys.version_info < (2, 7) -# Due to https://bugs.python.org/issue27657: PYTHON376_OR_NEWER = sys.version_info > (3, 7, 5) def make_wsgi_app(registry=REGISTRY): @@ -342,6 +341,7 @@ def delete_from_gateway( def _use_gateway(method, gateway, job, registry, grouping_key, timeout, handler): gateway_url = urlparse(gateway) + # See https://bugs.python.org/issue27657 for details on urlparse in py>=3.7.6. if not gateway_url.scheme or ( (PYTHON376_OR_NEWER or PYTHON26_OR_OLDER) and gateway_url.scheme not in ['http', 'https']