diff --git a/prometheus_client/exposition.py b/prometheus_client/exposition.py index 221cbedf..6911ba75 100644 --- a/prometheus_client/exposition.py +++ b/prometheus_client/exposition.py @@ -29,7 +29,7 @@ """Content type of the latest text format""" PYTHON26_OR_OLDER = sys.version_info < (2, 7) - +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 +341,11 @@ 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']): + # 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'] + ): 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')