Skip to content

Commit

Permalink
set http prefix in python geq 3.7.6
Browse files Browse the repository at this point in the history
Signed-off-by: George Mossessian <gmossessian@gmail.com>
  • Loading branch information
gmossessian committed Dec 22, 2019
1 parent 87d08de commit 0c5b611
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
8 changes: 6 additions & 2 deletions prometheus_client/exposition.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down Expand Up @@ -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))

Expand Down
7 changes: 7 additions & 0 deletions tests/test_exposition.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit 0c5b611

Please sign in to comment.