From 6e1e16a7ac98d7a37073c7b0ddd50136f0068993 Mon Sep 17 00:00:00 2001 From: Tim Small Date: Wed, 23 Nov 2016 18:12:01 +0000 Subject: [PATCH 1/3] Just check for a url scheme to allow users to provide a handler. --- prometheus_client/exposition.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/prometheus_client/exposition.py b/prometheus_client/exposition.py index 13a91e01..0fa39a81 100644 --- a/prometheus_client/exposition.py +++ b/prometheus_client/exposition.py @@ -165,7 +165,8 @@ def delete_from_gateway(gateway, job, grouping_key=None, timeout=None): def _use_gateway(method, gateway, job, registry, grouping_key, timeout): - if not (gateway.startswith('http://') or gateway.startswith('https://')): + gateway_url = urlparse(gateway) + if not gateway_url.scheme: gateway = 'http://{0}'.format(gateway) url = '{0}/metrics/job/{1}'.format(gateway, quote_plus(job)) From 87369398bc1ccb7b22970b8ade92465a39850b5c Mon Sep 17 00:00:00 2001 From: Tim Small Date: Wed, 23 Nov 2016 18:16:27 +0000 Subject: [PATCH 2/3] 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. --- prometheus_client/exposition.py | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/prometheus_client/exposition.py b/prometheus_client/exposition.py index 0fa39a81..8eb20724 100644 --- a/prometheus_client/exposition.py +++ b/prometheus_client/exposition.py @@ -111,7 +111,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 @@ -126,10 +126,10 @@ def push_to_gateway(gateway, job, registry, grouping_key=None, timeout=None): 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 @@ -144,10 +144,10 @@ def pushadd_to_gateway(gateway, job, registry, grouping_key=None, timeout=None): 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 @@ -161,10 +161,10 @@ def delete_from_gateway(gateway, job, grouping_key=None, timeout=None): 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): +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) @@ -182,10 +182,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) + 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, + 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.''' From 7b0677c02e486bc1a23521d3ee425c5540c0a916 Mon Sep 17 00:00:00 2001 From: Tim Small Date: Fri, 25 Nov 2016 17:45:38 +0000 Subject: [PATCH 3/3] Document new handler parameter to pushgateway function Provide pydoc for the new handler function to the various pushgateway functions, provide parameter descriptions in the documentatation for the push_to_gateway function to avoid excessive copy-and-paste. --- prometheus_client/exposition.py | 36 +++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/prometheus_client/exposition.py b/prometheus_client/exposition.py index 8eb20724..e965f9d5 100644 --- a/prometheus_client/exposition.py +++ b/prometheus_client/exposition.py @@ -123,6 +123,30 @@ def push_to_gateway(gateway, job, registry, grouping_key=None, timeout=None, han 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.''' @@ -141,6 +165,12 @@ 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.''' @@ -158,6 +188,12 @@ def delete_from_gateway(gateway, job, grouping_key=None, timeout=None, handler=N 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.'''