@@ -343,7 +343,7 @@ def _use_gateway(method, gateway, job, registry, grouping_key, timeout, handler)
343
343
gateway_url = urlparse (gateway )
344
344
if not gateway_url .scheme or (PYTHON26_OR_OLDER and gateway_url .scheme not in ['http' , 'https' ]):
345
345
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 ))
347
347
348
348
data = b''
349
349
if method != 'DELETE' :
@@ -352,7 +352,7 @@ def _use_gateway(method, gateway, job, registry, grouping_key, timeout, handler)
352
352
if grouping_key is None :
353
353
grouping_key = {}
354
354
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 )))
356
356
for k , v in sorted (grouping_key .items ()))
357
357
358
358
handler (
@@ -361,6 +361,14 @@ def _use_gateway(method, gateway, job, registry, grouping_key, timeout, handler)
361
361
)()
362
362
363
363
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
+
364
372
def instance_ip_grouping_key ():
365
373
"""Grouping key with instance set to the IP Address of this host."""
366
374
with closing (socket .socket (socket .AF_INET , socket .SOCK_DGRAM )) as s :
0 commit comments