@@ -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+
364372def 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 :
0 commit comments