8000 Add handler parameter to pushgateway functions to permit HTTP AUTH etc. by tim-seoss · Pull Request #120 · prometheus/client_python · GitHub
[go: up one dir, main page]

Skip to content

Add handler parameter to pushgateway functions to permit HTTP AUTH etc. #120

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 53 additions & 12 deletions prometheus_client/exposition.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def write_to_textfile(path, registry):
os.rename(tmppath, path)


def push_to_gateway(gateway, job, registry, grouping_key=None, timeout=None):
def push_to_gateway(gateway, job, registry, grouping_key=None, timeout=None, handler=None):
'''Push metrics to the given pushgateway.

`gateway` the url for your push gateway. Either of the form
Expand All @@ -130,13 +130,37 @@ def push_to_gateway(gateway, job, registry, grouping_key=None, timeout=None):
Defaults to None
`timeout` is how long push will attempt to connect before giving up.
Defaults to None
`handler` is an optional function which can be provided to perform
requests to the 'gateway'.
Defaults to None, in which case an http or https request
will be carried out by a default handler.
If not None, the argument must be a function which accepts
the following arguments:
url, method, timeout, headers, and content
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd be good to include an example one doing something simple like basic auth

May be used to implement additional functionality not
supported by the built-in default handler (such as SSL
client certicates, and HTTP authentication mechanisms).
'url' is the URL for the request, the 'gateway' argument
described earlier will form the basis of this URL.
'method' is the HTTP method which should be used when
carrying out the request.
'timeout' requests not successfully completed after this
many seconds should be aborted. If timeout is None, then
the handler should not set a timeout.
'headers' is a list of ("header-name","header-value") tuples
which must be passed to the pushgateway in the form of HTTP
request headers.
The function should raise an exception (e.g. IOError) on
failure.
'content' is the data which should be used to form the HTTP
Message Body.

This overwrites all metrics with the same job and grouping_key.
This uses the PUT HTTP method.'''
_use_gateway('PUT', gateway, job, registry, grouping_key, timeout)
_use_gateway('PUT', gateway, job, registry, grouping_key, timeout, handler)


def pushadd_to_gateway(gateway, job, registry, grouping_key=None, timeout=None):
def pushadd_to_gateway(gateway, job, registry, grouping_key=None, timeout=None, handler=None):
'''PushAdd metrics to the given pushgateway.

`gateway` the url for your push gateway. Either of the form
Expand All @@ -148,13 +172,19 @@ def pushadd_to_gateway(gateway, job, registry, grouping_key=None, timeout=None):
Defaults to None
`timeout` is how long push will attempt to connect before giving up.
Defaults to None
`handler` is an optional function which can be provided to perform
requests to the 'gateway'.
Defaults to None, in which case an http or https request
will be carried out by a default handler.
See the 'prometheus_client.push_to_gateway' documentation
for implementation requirements.

This replaces metrics with the same name, job and grouping_key.
This uses the POST HTTP method.'''
_use_gateway('POST', gateway, job, registry, grouping_key, timeout)
_use_gateway('POST', gateway, job, registry, grouping_key, timeout, handler)


def delete_from_gateway(gateway, job, grouping_key=None, timeout=None):
def delete_from_gateway(gateway, job, grouping_key=None, timeout=None, handler=None):
'''Delete metrics from the given pushgateway.

`gateway` the url for your push gateway. Either of the form
Expand All @@ -165,14 +195,21 @@ def delete_from_gateway(gateway, job, grouping_key=None, timeout=None):
Defaults to None
`timeout` is how long delete will attempt to connect before giving up.
Defaults to None
`handler` is an optional function which can be provided to perform
requests to the 'gateway'.
Defaults to None, in which case an http or https request
will be carried out by a default handler.
See the 'prometheus_client.push_to_gateway' documentation
for implementation requirements.

This deletes metrics with the given job and grouping_key.
This uses the DELETE HTTP method.'''
_use_gateway('DELETE', gateway, job, None, grouping_key, timeout)
_use_gateway('DELETE', gateway, job, None, grouping_key, timeout, handler)


def _use_gateway(method, gateway, job, registry, grouping_key, timeout):
if not (gateway.startswith('http://') or gateway.startswith('https://')):
def _use_gateway(method, gateway, job, registry, grouping_key, timeout, handler):
gateway_url = urlparse(gateway)
if not gateway_url.scheme:
gateway = 'http://{0}'.format(gateway)
url = '{0}/metrics/job/{1}'.format(gateway, quote_plus(job))

Expand All @@ -188,10 +225,14 @@ def _use_gateway(method, gateway, job, registry, grouping_key, timeout):
request = Request(url, data=data)
request.add_header('Content-Type', CONTENT_TYPE_LATEST)
request.get_method = lambda: method
resp = build_opener(HTTPHandler).open(request, timeout=timeout)
if resp.code >= 400:
raise IOError("error talking to pushgateway: {0} {1}".format(
resp.code, resp.msg))
if handler is None:
resp = build_opener(handler).open(request, timeout=timeout)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's changed HTTPHandler to handler, which will be None. Is this correct?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That looks off alright. Can you send a PR?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sent in PR #126.

if resp.code >= 400:
raise IOError("error talking to pushgateway: {0} {1}".format(
resp.code, resp.msg))
else:
handler(url=url, method=lambda: method, timeout=timeout,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is there a lambda for the method?

headers=[('Content-Type', CONTENT_TYPE_LATEST)], content=data)

def instance_ip_grouping_key():
'''Grouping key with instance set to the IP Address of this host.'''
Expand Down
0