-
Notifications
You must be signed in to change notification settings - Fork 844
Add support for exporting metrics to a pushgateway #13
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
Changes from all commits
ddb23a1
e1fb108
7bfdf8b
b554227
4065c3b
ec4564f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,14 @@ | |
| import os | ||
| import time | ||
| import threading | ||
|
|
||
| try: | ||
| from urllib2 import urlopen, quote | ||
| except ImportError: | ||
| # Python 3 | ||
| from urllib.request import urlopen | ||
| from urllib.parse import quote | ||
|
|
||
| try: | ||
| from BaseHTTPServer import BaseHTTPRequestHandler | ||
| except ImportError: | ||
|
|
@@ -15,6 +23,7 @@ | |
| from functools import wraps | ||
| from threading import Lock | ||
|
|
||
|
|
||
| __all__ = ['Counter', 'Gauge', 'Summary', 'Histogram'] | ||
|
|
||
| _METRIC_NAME_RE = re.compile(r'^[a-zA-Z_:][a-zA-Z0-9_:]*$') | ||
|
|
@@ -434,6 +443,38 @@ def do_GET(self): | |
| self.wfile.write(generate_latest(REGISTRY)) | ||
|
|
||
|
|
||
| def build_pushgateway_url(job, instance=None, host='localhost', port=9091): | ||
| ''' | ||
| Build a valid pushgateway url | ||
| ''' | ||
|
|
||
| if instance: | ||
| instancestr = '/instances/{}'.format(instance) | ||
| else: | ||
| instancestr = '' | ||
|
|
||
| url = 'http://{}:{}/metrics/jobs/{}{}'.format(host, port, | ||
|
Contributor
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. This should be
Contributor
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. You're assuming http, best to let the user pass in all that as a prefix |
||
| quote(job), | ||
| quote(instancestr)) | ||
| return url | ||
|
|
||
|
|
||
| def push_to_gateway_url(url, registry, timeout=None): | ||
|
Contributor
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. I'd stick with just this function |
||
| '''Push metrics to the given url''' | ||
|
|
||
| resp = urlopen(url, data=generate_latest(registry), timeout=timeout) | ||
| if resp.code >= 400: | ||
| raise IOError("error pushing to pushgateway: {0} {1}".format( | ||
| resp.code, resp.msg)) | ||
|
|
||
|
|
||
| def push_to_gateway(registry, job, instance=None, host='localhost', port=9091, timeout=None): | ||
| '''Push metrics to a pushgateway''' | ||
|
|
||
| url = build_pushgateway_url(job, instance, host, port) | ||
| push_to_gateway_url(url, registry, timeout) | ||
|
|
||
|
|
||
| def write_to_textfile(path, registry): | ||
| '''Write metrics to the given path. | ||
|
|
||
|
|
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.
This should be prefixed with _ to mark it as internal