8000 Add 'handler_args' to pass extra informatoin to handler. · pythonAI/client_python@0ac7442 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0ac7442

Browse files
committed
Add 'handler_args' to pass extra informatoin to handler.
1 parent ea1e3e7 commit 0ac7442

File tree

3 files changed

+28
-9
lines changed

3 files changed

+28
-9
lines changed

prometheus_client/exposition.py< 10000 /h3>

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ def write_to_textfile(path, registry):
121121
os.rename(tmppath, path)
122122

123123

124-
def push_to_gateway(gateway, job, registry, grouping_key=None, timeout=None, handler=None):
124+
def push_to_gateway(gateway, job, registry, grouping_key=None, timeout=None, handler=None, handler_args=None):
125125
'''Push metrics to the given pushgateway.
126126
127127
`gateway` the url for your push gateway. Either of the form
@@ -157,13 +157,14 @@ def push_to_gateway(gateway, job, registry, grouping_key=None, timeout=None, han
157157
failure.
158158
'content' is the data which should be used to form the HTTP
159159
Message Body.
160+
`handler_args` is an optional dict of extra arguments to provide
160161
161162
This overwrites all metrics with the same job and grouping_key.
162163
This uses the PUT HTTP method.'''
163-
_use_gateway('PUT', gateway, job, registry, grouping_key, timeout, handler)
164+
_use_gateway('PUT', gateway, job, registry, grouping_key, timeout, handler, handler_args)
164165

165166

166-
def pushadd_to_gateway(gateway, job, registry, grouping_key=None, timeout=None, handler=None):
167+
def pushadd_to_gateway(gateway, job, registry, grouping_key=None, timeout=None, handler=None, handler_args=None):
167168
'''PushAdd metrics to the given pushgateway.
168169
169170
`gateway` the url for your push gateway. Either of the form
@@ -184,10 +185,10 @@ def pushadd_to_gateway(gateway, job, registry, grouping_key=None, timeout=None,
184185
185186
This replaces metrics with the same name, job and grouping_key.
186187
This uses the POST HTTP method.'''
187-
_use_gateway('POST', gateway, job, registry, grouping_key, timeout, handler)
188+
_use_gateway('POST', gateway, job, registry, grouping_key, timeout, handler, handler_args)
188189

189190

190-
def delete_from_gateway(gateway, job, grouping_key=None, timeout=None, handler=None):
191+
def delete_from_gateway(gateway, job, grouping_key=None, timeout=None, handler=None, handler_args=None):
191192
'''Delete metrics from the given pushgateway.
192193
193194
`gateway` the url for your push gateway. Either of the form
@@ -207,10 +208,10 @@ def delete_from_gateway(gateway, job, grouping_key=None, timeout=None, handler=N
207208
208209
This deletes metrics with the given job and grouping_key.
209210
This uses the DELETE HTTP method.'''
210-
_use_gateway('DELETE', gateway, job, None, grouping_key, timeout, handler)
211+
_use_gateway('DELETE', gateway, job, None, grouping_key, timeout, handler, handler_args)
211212

212213

213-
def _use_gateway(method, gateway, job, registry, grouping_key, timeout, handler):
214+
def _use_gateway(method, gateway, job, registry, grouping_key, timeout, handler, handler_args):
214215
gateway_url = urlparse(gateway)
215216
if not gateway_url.scheme:
216217
gateway = 'http://{0}'.format(gateway)
@@ -228,8 +229,10 @@ def _use_gateway(method, gateway, job, registry, grouping_key, timeout, handler)
228229
headers=[('Content-Type', CONTENT_TYPE_LATEST)]
229230
if handler is None:
230231
handler = default_handler
232+
if handler_args is None:
233+
handler_args = dict()
231234
handler(url=url, method=method, timeout=timeout,
232-
headers=headers, data=data)
235+
headers=headers, data=data, **handler_args)
233236

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

prometheus_client/handlers/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# Python 3
77
from urllib.request import build_opener, Request, HTTPHandler
88

9-
def handler(url, method, timeout, headers, data):
9+
def handler(url, method, timeout, headers, data, **kwargs):
1010
'''Default handler that implements HTTP/HTTPS connections.'''
1111
request = Request(url, data=data)
1212
request.get_method = lambda: method
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/usr/bin/python
2+
3+
import os, base64
4+
5+
from prometheus_client.handlers.base import handler as default_handler
6+
7+
def handler(url, method, timeout, headers, data, **kwargs):
8+
'''Handler that implements HTTP Basic Auth by setting auth headers if
9+
'username' passed as keyword argument.'''
10+
if 'username' in kwargs:
11+
username = kwargs['username']
12+
password = kwargs['password']
13+
auth_value = "{0}:{1}".format(username, password)
14+
auth_header = "Basic {0}".format(base64.b64encode(bytes(auth_value)))
15+
headers.append(['Authorization', auth_header])
16+
default_handler(url, method, timeout, headers, data)

0 commit comments

Comments
 (0)
0