8000 Support slashes in pgw grouping keys. (#442) · danarwix/client_python@a8f5c80 · GitHub
[go: up one dir, main page]

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit a8f5c80

Browse files
authored
Support slashes in pgw grouping keys. (prometheus#442)
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
1 parent 4712878 commit a8f5c80

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

prometheus_client/exposition.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ def _use_gateway(method, gateway, job, registry, grouping_key, timeout, handler)
343343
gateway_url = urlparse(gateway)
344344
if not gateway_url.scheme or (PYTHON26_OR_OLDER and gateway_url.scheme not in ['http', 'https']):
345345
gateway = 'http://{0}'.format(gateway)
346-
url = '{0}/metrics/job/{1}'.format(gateway, quote_plus(job))
346+
url = '{0}/metrics/{1}/{2}'.format(gateway, *_escape_grouping_key("job", job))
347347

348348
data = b''
349349
if method != 'DELETE':
@@ -352,7 +352,7 @@ def _use_gateway(method, gateway, job, registry, grouping_key, timeout, handler)
352352
if grouping_key is None:
353353
grouping_key = {}
354354
url += ''.join(
355-
'/{0}/{1}'.format(quote_plus(str(k)), quote_plus(str(v)))
355+
'/{0}/{1}'.format(*_escape_grouping_key(str(k), str(v)))
356356
for k, v in sorted(grouping_key.items()))
357357

358358
handler(
@@ -361,6 +361,14 @@ def _use_gateway(method, gateway, job, registry, grouping_key, timeout, handler)
361361
)()
362362

363363

364+
def _escape_grouping_key(k, v):
365+
if '/' in v:
366+
# Added in Pushgateway 0.9.0.
367+
return k + "@base64", base64.urlsafe_b64encode(v.encode("utf-8")).decode("utf-8")
368+
else:
369+
return k, quote_plus(v)
370+
371+
364372
def instance_ip_grouping_key():
365373
"""Grouping key with instance set to the IP Address of this host."""
366374
with closing(socket.socket(socket.AF_INET, socket.SOCK_DGRAM)) as s:

tests/test_exposition.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,14 @@ def test_push_with_groupingkey(self):
239239
def test_push_with_complex_groupingkey(self):
240240
push_to_gateway(self.address, "my_job", self.registry, {'a': 9, 'b': 'a/ z'})
241241
self.assertEqual(self.requests[0][0].command, 'PUT')
242-
self.assertEqual(self.requests[0][0].path, '/metrics/job/my_job/a/9/b/a%2F+z')
242+
self.assertEqual(self.requests[0][0].path, '/metrics/job/my_job/a/9/b@base64/YS8geg==')
243+
self.assertEqual(self.requests[0][0].headers.get('content-type'), CONTENT_TYPE_LATEST)
244+
self.assertEqual(self.requests[0][1], b'# HELP g help\n# TYPE g gauge\ng 0.0\n')
245+
246+
def test_push_with_complex_job(self):
247+
push_to_gateway(self.address, "my/job", self.registry)
248+
self.assertEqual(self.requests[0][0].command, 'PUT')
249+
self.assertEqual(self.requests[0][0].path, '/metrics/job@base64/bXkvam9i')
243250
self.assertEqual(self.requests[0][0].headers.get('content-type'), CONTENT_TYPE_LATEST)
244251
self.assertEqual(self.requests[0][1], b'# HELP g help\n# TYPE g gauge\ng 0.0\n')
245252

0 commit comments

Comments
 (0)
0