From 5ecb53c9cf66a9c1be1ebd7e5d296e9e0528b98b Mon Sep 17 00:00:00 2001 From: Aaron Tygart Date: Wed, 28 Sep 2016 17:02:06 -0500 Subject: [PATCH 1/4] Add optional scheme support for gateway urls --- prometheus_client/exposition.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/prometheus_client/exposition.py b/prometheus_client/exposition.py index 7e51ecec..2fd5a2b4 100644 --- a/prometheus_client/exposition.py +++ b/prometheus_client/exposition.py @@ -15,13 +15,14 @@ from BaseHTTPServer import HTTPServer from urllib2 import build_opener, Request, HTTPHandler from urllib import quote_plus + from urlparse import urlparse except ImportError: # Python 3 unicode = str from http.server import BaseHTTPRequestHandler from http.server import HTTPServer from urllib.request import build_opener, Request, HTTPHandler - from urllib.parse import quote_plus + from urllib.parse import quote_plus, urlparse CONTENT_TYPE_LATEST = str('text/plain; version=0.0.4; charset=utf-8') @@ -105,6 +106,8 @@ def write_to_textfile(path, registry): def push_to_gateway(gateway, job, registry, grouping_key=None, timeout=None): '''Push metrics to the given pushgateway. + `gateway` is a url, but will assume http if no other scheme is provided. + 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) @@ -113,6 +116,8 @@ def push_to_gateway(gateway, job, registry, grouping_key=None, timeout=None): def pushadd_to_gateway(gateway, job, registry, grouping_key=None, timeout=None): '''PushAdd metrics to the given pushgateway. + `gateway` is a url, but will assume http if no other scheme is provided. + 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) @@ -121,13 +126,17 @@ def pushadd_to_gateway(gateway, job, registry, grouping_key=None, timeout=None): def delete_from_gateway(gateway, job, grouping_key=None, timeout=None): '''Delete metrics from the given pushgateway. + `gateway` is a url, but will assume http if no other scheme is provided. + 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) def _use_gateway(method, gateway, job, registry, grouping_key, timeout): - url = 'http://{0}/metrics/job/{1}'.format(gateway, quote_plus(job)) + if len(urlparse(gateway).scheme) == 0: + gateway = 'http://{0}'.format(gateway) + url = '{0}/metrics/job/{1}'.format(gateway, quote_plus(job)) data = b'' if method != 'DELETE': From 6ab873358882d155bf07f63c217a6f4b9ccfdbc2 Mon Sep 17 00:00:00 2001 From: Aaron Tygart Date: Fri, 30 Sep 2016 16:18:25 -0500 Subject: [PATCH 2/4] Add requested documentation updates --- prometheus_client/exposition.py | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/prometheus_client/exposition.py b/prometheus_client/exposition.py index 2fd5a2b4..a8c39a25 100644 --- a/prometheus_client/exposition.py +++ b/prometheus_client/exposition.py @@ -106,7 +106,13 @@ def write_to_textfile(path, registry): def push_to_gateway(gateway, job, registry, grouping_key=None, timeout=None): '''Push metrics to the given pushgateway. - `gateway` is a url, but will assume http if no other scheme is provided. + `gateway` is a url. Scheme defaults to 'http' if none is provided + `job` is the name of the local routine pushing metrics + `registry` is an instance of CollectorRegistry + `grouping_key` please see the pushgateway documentation for details. + Defaults to None + `timeout` is how long push will attempt to connect before giving up. + Defaults to None This overwrites all metrics with the same job and grouping_key. This uses the PUT HTTP method.''' @@ -116,7 +122,13 @@ def push_to_gateway(gateway, job, registry, grouping_key=None, timeout=None): def pushadd_to_gateway(gateway, job, registry, grouping_key=None, timeout=None): '''PushAdd metrics to the given pushgateway. - `gateway` is a url, but will assume http if no other scheme is provided. + `gateway` is a url. Scheme defaults to 'http' if none is provided + `job` is the name of the local routine pushing metrics + `registry` is an instance of CollectorRegistry + `grouping_key` please see the pushgateway documentation for details. + Defaults to None + `timeout` is how long push will attempt to connect before giving up. + Defaults to None This replaces metrics with the same name, job and grouping_key. This uses the POST HTTP method.''' @@ -126,7 +138,12 @@ def pushadd_to_gateway(gateway, job, registry, grouping_key=None, timeout=None): def delete_from_gateway(gateway, job, grouping_key=None, timeout=None): '''Delete metrics from the given pushgateway. - `gateway` is a url, but will assume http if no other scheme is provided. + `gateway` is a url. Scheme defaults to 'http' if none is provided + `job` is the name of the local routine pushing metrics + `grouping_key` please see the pushgateway documentation for details. + Defaults to None + `timeout` is how long delete will attempt to connect before giving up. + Defaults to None This deletes metrics with the given job and grouping_key. This uses the DELETE HTTP method.''' From edf8fe959fe10f8692102d66158b9f5d773fd2b0 Mon Sep 17 00:00:00 2001 From: Aaron Tygart Date: Fri, 30 Sep 2016 17:11:28 -0500 Subject: [PATCH 3/4] Update documentation. Resolve python 2.6.9 bug --- prometheus_client/exposition.py | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/prometheus_client/exposition.py b/prometheus_client/exposition.py index a8c39a25..d3fe1350 100644 --- a/prometheus_client/exposition.py +++ b/prometheus_client/exposition.py @@ -106,8 +106,10 @@ def write_to_textfile(path, registry): def push_to_gateway(gateway, job, registry, grouping_key=None, timeout=None): '''Push metrics to the given pushgateway. - `gateway` is a url. Scheme defaults to 'http' if none is provided - `job` is the name of the local routine pushing metrics + `gateway` the url for your push gateway. Either of the form + 'http://pushgateway.local', or 'pushgateway.local'. + Scheme defaults to 'http' if none is provided + `job` is the job label to be attached to all pushed metrics `registry` is an instance of CollectorRegistry `grouping_key` please see the pushgateway documentation for details. Defaults to None @@ -122,8 +124,10 @@ def push_to_gateway(gateway, job, registry, grouping_key=None, timeout=None): def pushadd_to_gateway(gateway, job, registry, grouping_key=None, timeout=None): '''PushAdd metrics to the given pushgateway. - `gateway` is a url. Scheme defaults to 'http' if none is provided - `job` is the name of the local routine pushing metrics + `gateway` the url for your push gateway. Either of the form + 'http://pushgateway.local', or 'pushgateway.local'. + Scheme defaults to 'http' if none is provided + `job` is the job label to be attached to all pushed metrics `registry` is an instance of CollectorRegistry `grouping_key` please see the pushgateway documentation for details. Defaults to None @@ -138,8 +142,10 @@ def pushadd_to_gateway(gateway, job, registry, grouping_key=None, timeout=None): def delete_from_gateway(gateway, job, grouping_key=None, timeout=None): '''Delete metrics from the given pushgateway. - `gateway` is a url. Scheme defaults to 'http' if none is provided - `job` is the name of the local routine pushing metrics + `gateway` the url for your push gateway. Either of the form + 'http://pushgateway.local', or 'pushgateway.local'. + Scheme defaults to 'http' if none is provided + `job` is the job label to be attached to all pushed metrics `grouping_key` please see the pushgateway documentation for details. Defaults to None `timeout` is how long delete will attempt to connect before giving up. @@ -151,7 +157,7 @@ def delete_from_gateway(gateway, job, grouping_key=None, timeout=None): def _use_gateway(method, gateway, job, registry, grouping_key, timeout): - if len(urlparse(gateway).scheme) == 0: + if not (gateway.startswith('http://') or gateway.startswith('https://')): gateway = 'http://{0}'.format(gateway) url = '{0}/metrics/job/{1}'.format(gateway, quote_plus(job)) From 92dbddc3246f782e33384a5848a3d82b58d325ea Mon Sep 17 00:00:00 2001 From: Aaron Tygart Date: Fri, 30 Sep 2016 17:18:27 -0500 Subject: [PATCH 4/4] Remove unneeded imports --- prometheus_client/exposition.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/prometheus_client/exposition.py b/prometheus_client/exposition.py index d3fe1350..dc829b6a 100644 --- a/prometheus_client/exposition.py +++ b/prometheus_client/exposition.py @@ -15,14 +15,13 @@ from BaseHTTPServer import HTTPServer from urllib2 import build_opener, Request, HTTPHandler from urllib import quote_plus - from urlparse import urlparse except ImportError: # Python 3 unicode = str from http.server import BaseHTTPRequestHandler from http.server import HTTPServer from urllib.request import build_opener, Request, HTTPHandler - from urllib.parse import quote_plus, urlparse + from urllib.parse import quote_plus CONTENT_TYPE_LATEST = str('text/plain; version=0.0.4; charset=utf-8')