8000 Remove trailing slash from pushgateway URLs (#722) · christopherime/client_python@a2042ab · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit a2042ab

Browse files
authored
Remove trailing slash from pushgateway URLs (prometheus#722)
When using the push gateway, URLs may be submitted as host or URL. When using an URL scheme, in case user submits URL with a trailing slash (eg: `http://localhost:9091/`), the pushgateway server will reply with a 301 (or 307 depending on the version) redirect that is not handled by the default handler. Leading to a traceback Simply remove any trailing slash from the base URL Signed-off-by: Romuald Brunet <romuald@gandi.net>
1 parent b3541ac commit a2042ab

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

prometheus_client/exposition.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,8 @@ def _use_gateway(method, gateway, job, registry, grouping_key, timeout, handler)
448448
and gateway_url.scheme not in ['http', 'https']
449449
):
450450
gateway = 'http://{0}'.format(gateway)
451+
452+
gateway = gateway.rstrip('/')
451453
url = '{0}/metrics/{1}/{2}'.format(gateway, *_escape_grouping_key("job", job))
452454

453455
data = b''

tests/test_exposition.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,12 @@ def my_redirect_handler(url, method, timeout, headers, data):
369369
# ensure the redirect took place at the expected redirect location.
370370
self.assertEqual(self.requests[1][0].path, "/" + self.redirect_flag)
371371

372+
def test_push_with_trailing_slash(self):
373+
address = self.address + '/'
374+
push_to_gateway(address, "my_job_with_trailing_slash", self.registry)
375+
376+
self.assertNotIn('//', self.requests[0][0].path)
377+
372378
def test_instance_ip_grouping_key(self):
373379
self.assertTrue('' != instance_ip_grouping_key()['instance'])
374380

0 commit comments

Comments
 (0)
0