8000 Allow a handler to be passed in to carry out a custom request. · pythonAI/client_python@8736939 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8736939

Browse files
committed
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 6e1e16a commit 8736939

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
@@ -111,7 +111,7 @@ def write_to_textfile(path, registry):
111111
os.rename(tmppath, path)
112112

113113

114-
def push_to_gateway(gateway, job, registry, grouping_key=None, timeout=None):
114+
def push_to_gateway(gateway, job, registry, grouping_key=None, timeout=None, handler=None):
115115
'''Push metrics to the given pushgateway.
116116
117117
`gateway` the url for your push gateway. Either of the form
@@ -126,10 +126,10 @@ def push_to_gateway(gateway, job, registry, grouping_key=None, timeout=None):
126126
127127
This overwrites all metrics with the same job and grouping_key.
128128
This uses the PUT HTTP method.'''
129-
_use_gateway('PUT', gateway, job, registry, grouping_key, timeout)
129+
_use_gateway('PUT', gateway, job, registry, grouping_key, timeout, handler)
130130

131131

132-
def pushadd_to_gateway(gateway, job, registry, grouping_key=None, timeout=None):
132+
def pushadd_to_gateway(gateway, job, registry, grouping_key=None, timeout=None, handler=None):
133133
'''PushAdd metrics to the given pushgateway.
134134
135135
`gateway` the url for your push gateway. Either of the form
@@ -144,10 +144,10 @@ def pushadd_to_gateway(gateway, job, registry, grouping_key=None, timeout=None):
144144
145145
This replaces metrics with the same name, job and grouping_key.
146146
This uses the POST HTTP method.'''
147-
_use_gateway('POST', gateway, job, registry, grouping_key, timeout)
147+
_use_gateway('POST', gateway, job, registry, grouping_key, timeout, handler)
148148

149149

150-
def delete_from_gateway(gateway, job, grouping_key=None, timeout=None):
150+
def delete_from_gateway(gateway, job, grouping_key=None, timeout=None, handler=None):
151151
'''Delete metrics from the given pushgateway.
152152
153153
`gateway` the url for your push gateway. Either of the form
@@ -161,10 +161,10 @@ def delete_from_gateway(gateway, job, grouping_key=None, timeout=None):
161161
162162
This deletes metrics with the given job and grouping_key.
163163
This uses the DELETE HTTP method.'''
164-
_use_gateway('DELETE', gateway, job, None, grouping_key, timeout)
164+
_use_gateway('DELETE', gateway, job, None, grouping_key, timeout, handler)
165165

166166

167-
def _use_gateway(method, gateway, job, registry, grouping_key, timeout):
167+
def _use_gateway(method, gateway, job, registry, grouping_key, timeout, handler):
168168
gateway_url = urlparse(gateway)
169169
if not gateway_url.scheme:
170170
gateway = 'http://{0}'.format(gateway)
@@ -182,10 +182,14 @@ def _use_gateway(method, gateway, job, registry, grouping_key, timeout):
182182
request = Request(url, data=data)
183183
request.add_header('Content-Type', CONTENT_TYPE_LATEST)
184184
request.get_method = lambda: method
185-
resp = build_opener(HTTPHandler).open(request, timeout=timeout)
186-
if resp.code >= 400:
187-
raise IOError("error talking to pushgateway: {0} {1}".format(
188-
resp.code, resp.msg))
185+
if handler is None:
186+
resp = build_opener(handler).open(request, timeout=timeout)
187+
if resp.code >= 400:
188+
raise IOError("error talking to pushgateway: {0} {1}".format(
189+
resp.code, resp.msg))
190+
else:
191+
handler(url=url, method=lambda: method, timeout=timeout,
192+
headers=[('Content-Type', CONTENT_TYPE_LATEST)], content=data)
189193

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

0 commit comments

Comments
 (0)
0