Skip to content

Commit ff09e4a

Browse files
committed
Fix spaces in grouping key values for push_to_gateway
Use base64 encoding for grouping key values containing spaces, similar to how values with slashes are handled. This prevents spaces from being converted to '+' signs by quote_plus(). Fixes #1064 Signed-off-by: Varun Chawla <varun_6april@hotmail.com>
1 parent a854135 commit ff09e4a

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

prometheus_client/exposition.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -783,8 +783,9 @@ def _escape_grouping_key(k, v):
783783
if v == "":
784784
# Per https://github.com/prometheus/pushgateway/pull/346.
785785
return k + "@base64", "="
786-
elif '/' in v:
786+
elif '/' in v or ' ' in v:
787787
# Added in Pushgateway 0.9.0.
788+
# Use base64 encoding for values containing slashes or spaces
788789
return k + "@base64", base64.urlsafe_b64encode(v.encode("utf-8")).decode("utf-8")
789790
else:
790791
return k, quote_plus(v)

tests/test_exposition.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,13 @@ def test_push_with_groupingkey_empty_label(self):
301301
self.assertEqual(self.requests[0][0].headers.get('content-type'), CONTENT_TYPE_PLAIN_0_0_4)
302302
self.assertEqual(self.requests[0][1], b'# HELP g help\n# TYPE g gauge\ng 0.0\n')
303303

304+
def test_push_with_groupingkey_with_spaces(self):
305+
push_to_gateway(self.address, "my_job", self.registry, {'label': 'value with spaces'})
306+
self.assertEqual(self.requests[0][0].command, 'PUT')
307+
self.assertEqual(self.requests[0][0].path, '/metrics/job/my_job/label@base64/dmFsdWUgd2l0aCBzcGFjZXM=')
308+
self.assertEqual(self.requests[0][0].headers.get('content-type'), CONTENT_TYPE_PLAIN_0_0_4)
309+
self.assertEqual(self.requests[0][1], b'# HELP g help\n# TYPE g gauge\ng 0.0\n')
310+
304311
def test_push_with_complex_groupingkey(self):
305312
push_to_gateway(self.address, "my_job", self.registry, {'a': 9, 'b': 'a/ z'})
306313
self.assertEqual(self.requests[0][0].command, 'PUT')

0 commit comments

Comments
 (0)