-
Notifications
You must be signed in to change notification settings - Fork 834
Add handler parameter to pushgateway functions to permit HTTP AUTH etc. (refs issue #60) #126
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
Merged
Merged
Changes from all commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
6e1e16a
Just check for a url scheme to allow users to provide a handler.
tim-seoss 8736939
Allow a handler to be passed in to carry out a custom request.
tim-seoss 7b0677c
Document new handler parameter to pushgateway function
tim-seoss 0be1ec5
Merge branch 'master' of https://github.com/prometheus/client_python
tim-seoss c37a15c
Separate default handler code for re-use and add test case for custom…
rossigee 5914671
Fix relative import error.
rossigee 0c1220a
Add 'handlers' package to distribution.
rossigee d8a1694
Add 'handler_args' to pass extra informatoin to handler.
rossigee a67bf46
Merge branch 'master' of https://github.com/prometheus/client_python
rossigee 51b1ab0
Remove unnecessary imports.
rossigee e3071c4
Just check for a url scheme to allow users to provide a handler.
tim-seoss c3de82b
Allow a handler to be passed in to carry out a custom request.
tim-seoss 568f6ca
Document new handler parameter to pushgateway function
tim-seoss 814e24d
Separate default handler code for re-use and add test case for custom…
rossigee 9139e82
Fix relative import error.
rossigee ea1e3e7
Add 'handlers' package to distribution.
rossigee 0ac7442
Add 'handler_args' to pass extra informatoin to handler.
rossigee d3f5a58
Remove unnecessary imports.
rossigee 8adfe8a
Merge branch 'master' of https://github.com/rossigee/client_python
rossigee 44df258
Use closure to pass args to handlers (refs #60).
rossigee 46a9e92
Add missing import to auth example.
rossigee eb9500b
Simplify example by hardcoding username/password.
rossigee 7f863dd
Fix for docstring comment.
rossigee 0bebb0a
Move base handler back into exposition.
rossigee 7e45925
Move basic auth handler into exposition. Update docstring.
rossigee daa40fd
Fix for 'TypeError: string argument without an encoding' (regressing …
rossigee 1ba7c2e
Fix for 'TypeError: string argument without an encoding' (regressing …
rossigee 8f7511a
Fix for 'TypeError: string argument without an encoding' (regressing …
rossigee 444a748
Resort to determining python version to work around base64 byte encod…
rossigee 62e8012
Potential workaround for py26 test failures.
rossigee b084d64
Revert "Potential workaround for py26 test failures."
rossigee a7afc96
Another potential workaround for py26 test failures.
rossigee File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,8 @@ | |
| import threading | ||
| from contextlib import closing | ||
| from wsgiref.simple_server import make_server | ||
| import base64 | ||
| import sys | ||
|
|
||
| from . import core | ||
| try: | ||
|
|
@@ -118,7 +120,46 @@ def write_to_textfile(path, registry): | |
| os.rename(tmppath, path) | ||
|
|
||
|
|
||
| def push_to_gateway(gateway, job, registry, grouping_key=None, timeout=None): | ||
| def default_handler(url, method, timeout, headers, data): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add a docstring to indicate this is for use with the pgw functions? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
| '''Default handler that implements HTTP/HTTPS connections. | ||
|
|
||
| Used by the push_to_gateway functions. Can be re-used by other handlers.''' | ||
| def handle(): | ||
| request = Request(url, data=data) | ||
| request.get_method = lambda: method | ||
| for k, v in headers: | ||
| request.add_header(k, v) | ||
| 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)) | ||
|
|
||
| return handle | ||
|
|
||
|
|
||
| def basic_auth_handler(url, method, timeout, headers, data, username=None, password=None): | ||
| '''Handler that implements HTTP/HTTPS connections with Basic Auth. | ||
|
|
||
| Sets auth headers using supplied 'username' and 'password', if set. | ||
| Used by the push_to_gateway functions. Can be re-used by other handlers.''' | ||
| def handle(): | ||
| '''Handler that implements HTTP Basic Auth. | ||
| ''' | ||
| if username is not None and password is not None: | ||
| if sys.version_info >= (3,0): | ||
| auth_value = bytes('{0}:{1}'.format(username, password), 'utf8') | ||
| auth_token = str(base64.b64encode(auth_value), 'utf8') | ||
| else: | ||
| auth_value = '{0}:{1}'.format(username, password) | ||
| auth_token = base64.b64encode(auth_value) | ||
| auth_header = "Basic {0}".format(auth_token) | ||
| headers.append(['Authorization', auth_header]) | ||
| default_handler(url, method, timeout, headers, data)() | ||
|
|
||
| return handle | ||
|
|
||
|
|
||
| def push_to_gateway(gateway, job, registry, grouping_key=None, timeout=None, handler=default_handler): | ||
| '''Push metrics to the given pushgateway. | ||
|
|
||
| `gateway` the url for your push gateway. Either of the form | ||
|
|
@@ -130,13 +171,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 | ||
| 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=default_handler): | ||
| '''PushAdd metrics to the given pushgateway. | ||
|
|
||
| `gateway` the url for your push gateway. Either of the form | ||
|
|
@@ -148,13 +213,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=default_handler): | ||
| '''Delete metrics from the given pushgateway. | ||
|
|
||
| `gateway` the url for your push gateway. Either of the form | ||
|
|
@@ -165,14 +236,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)) | ||
|
|
||
|
|
@@ -185,13 +263,9 @@ def _use_gateway(method, gateway, job, registry, grouping_key, timeout): | |
| url = url + ''.join(['/{0}/{1}'.format(quote_plus(str(k)), quote_plus(str(v))) | ||
| for k, v in sorted(grouping_key.items())]) | ||
|
|
||
| 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)) | ||
| headers=[('Content-Type', CONTENT_TYPE_LATEST)] | ||
| handler(url=url, method=method, timeout=timeout, | ||
| headers=headers, data=data)() | ||
|
|
||
| def instance_ip_grouping_key(): | ||
| '''Grouping key with instance set to the IP Address of this host.''' | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd hardcode the user/pw here to keep it simpler.
If you use an env example, some users will be confused and think it only works if it comes from the env.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Example updated.