Skip to content

Commit

Permalink
Handle empty label values for the pushgateway. (#547)
Browse files Browse the repository at this point in the history
Fixes #546

Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
  • Loading branch information
brian-brazil authored May 15, 2020
1 parent 18d9371 commit 3be4faf
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
5 changes: 4 additions & 1 deletion prometheus_client/exposition.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,10 @@ def _use_gateway(method, gateway, job, registry, grouping_key, timeout, handler)


def _escape_grouping_key(k, v):
if '/' in v:
if v == "" :
# Per https://github.com/prometheus/pushgateway/pull/346.
return k + "@base64", "="
elif '/' in v:
# Added in Pushgateway 0.9.0.
return k + "@base64", base64.urlsafe_b64encode(v.encode("utf-8")).decode("utf-8")
else:
Expand Down
7 changes: 7 additions & 0 deletions tests/test_exposition.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,13 @@ def test_push_with_groupingkey(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_with_groupingkey_empty_label(self):
push_to_gateway(self.address, "my_job", self.registry, {'a': ''})
self.assertEqual(self.requests[0][0].command, 'PUT')
self.assertEqual(self.requests[0][0].path, '/metrics/job/my_job/a@base64/=')
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_complex_groupingkey(self):
push_to_gateway(self.address, "my_job", self.registry, {'a': 9, 'b': 'a/ z'})
self.assertEqual(self.requests[0][0].command, 'PUT')
Expand Down

0 comments on commit 3be4faf

Please sign in to comment.