Skip to content

Commit c3de82b

Browse files
tim-seossrossigee
authored andcommitted
Allow a handler to be passed in to carry out a custom request.
Allow a custom handler to be provided, so that the caller can provide code which carried out basic auth, https client certificate validation or other arbitrary schemes and access methods such as using different types of proxy.
1 parent e3071c4 commit c3de82b

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

prometheus_client/exposition.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ def write_to_textfile(path, registry):
118118
os.rename(tmppath, path)
119119

120120

121-
def push_to_gateway(gateway, job, registry, grouping_key=None, timeout=None):
121+
def push_to_gateway(gateway, job, registry, grouping_key=None, timeout=None, handler=None):
122122
'''Push metrics to the given pushgateway.
123123
124124
`gateway` the url for your push gateway. Either of the form
@@ -133,10 +133,10 @@ def push_to_gateway(gateway, job, registry, grouping_key=None, timeout=None):
133133
134134
This overwrites all metrics with the same job and grouping_key.
135135
This uses the PUT HTTP method.'''
136-
_use_gateway('PUT', gateway, job, registry, grouping_key, timeout)
136+
_use_gateway('PUT', gateway, job, registry, grouping_key, timeout, handler)
137137

138138

139-
def pushadd_to_gateway(gateway, job, registry, grouping_key=None, timeout=None):
139+
def pushadd_to_gateway(gateway, job, registry, grouping_key=None, timeout=None, handler=None):
140140
'''PushAdd metrics to the given pushgateway.
141141
142142
`gateway` the url for your push gateway. Either of the form
@@ -151,10 +151,10 @@ def pushadd_to_gateway(gateway, job, registry, grouping_key=None, timeout=None):
151151
152152
This replaces metrics with the same name, job and grouping_key.
153153
This uses the POST HTTP method.'''
154-
_use_gateway('POST', gateway, job, registry, grouping_key, timeout)
154+
_use_gateway('POST', gateway, job, registry, grouping_key, timeout, handler)
155155

156156

157-
def delete_from_gateway(gateway, job, grouping_key=None, timeout=None):
157+
def delete_from_gateway(gateway, job, grouping_key=None, timeout=None, handler=None):
158158
'''Delete metrics from the given pushgateway.
159159
160160
`gateway` the url for your push gateway. Either of the form
@@ -168,10 +168,10 @@ def delete_from_gateway(gateway, job, grouping_key=None, timeout=None):
168168
169169
This deletes metrics with the given job and grouping_key.
170170
This uses the DELETE HTTP method.'''
171-
_use_gateway('DELETE', gateway, job, None, grouping_key, timeout)
171+
_use_gateway('DELETE', gateway, job, None, grouping_key, timeout, handler)
172172

173173

174-
def _use_gateway(method, gateway, job, registry, grouping_key, timeout):
174+
def _use_gateway(method, gateway, job, registry, grouping_key, timeout, handler):
175175
gateway_url = urlparse(gateway)
176176
if not gateway_url.scheme:
177177
gateway = 'http://{0}'.format(gateway)
@@ -189,10 +189,14 @@ def _use_gateway(method, gateway, job, registry, grouping_key, timeout):
189189
request = Request(url, data=data)
190190
request.add_header('Content-Type', CONTENT_TYPE_LATEST)
191191
request.get_method = lambda: method
192-
resp = build_opener(HTTPHandler).open(request, timeout=timeout)
193-
if resp.code >= 400:
194-
raise IOError("error talking to pushgateway: {0} {1}".format(
195-
resp.code, resp.msg))
192+
if handler is None:
193+
resp = build_opener(handler).open(request, timeout=timeout)
194+
if resp.code >= 400:
195+
raise IOError("error talking to pushgateway: {0} {1}".format(
196+
resp.code, resp.msg))
197+
else:
198+
handler(url=url, method=lambda: method, timeout=timeout,
199+
headers=[('Content-Type', CONTENT_TYPE_LATEST)], content=data)
196200

197201
def instance_ip_grouping_key():
198202
'''Grouping key with instance set to the IP Address of this host.'''

0 commit comments

Comments
 (0)