-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement a direct to prometheus export for c2cwsgiutils_stats_db
Redirect warnings to logging
- Loading branch information
Showing
11 changed files
with
85 additions
and
8 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -6,3 +6,4 @@ | |
!rel_requirements.txt | ||
!setup.py | ||
!setup.cfg | ||
!tests |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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 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 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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
""" | ||
Implement parts of the Prometheus Pushgateway protocol, as defined here: | ||
https://github.com/prometheus/pushgateway | ||
""" | ||
import requests | ||
|
||
|
||
class PushgatewayGroupPublisher(object): | ||
def __init__(self, base_url, job, instance=None): | ||
if not base_url.endswith('/'): | ||
base_url += '/' | ||
self._url = "%smetrics/job/%s" % (base_url, job) | ||
if instance is not None: | ||
self._url += '/instance/' + instance | ||
self._reset() | ||
|
||
def add(self, metric_name, metric_value, metric_type='gauge', metric_labels=None): | ||
if metric_name in self._types: | ||
if self._types[metric_name] != metric_type: | ||
raise ValueError("Cannot change the type of a given metric") | ||
else: | ||
self._types[metric_name] = metric_type | ||
self._to_send += '# TYPE %s %s\n' % (metric_name, metric_type) | ||
self._to_send += metric_name | ||
if metric_labels is not None: | ||
self._to_send += '{' + ', '.join('%s="%s"' % (k, v) for k, v in metric_labels.items()) + '}' | ||
self._to_send += ' %s\n' % metric_value | ||
|
||
def commit(self): | ||
requests.put(self._url, data=self._to_send).raise_for_status() | ||
self._reset() | ||
|
||
def _reset(self): | ||
self._to_send = '' | ||
self._types = {} | ||
|
||
def __str__(self): | ||
return self._url + ' ->\n' + self._to_send |
This file contains 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 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 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
Empty file.
This file contains 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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import pytest | ||
|
||
from c2cwsgiutils.prometheus import PushgatewayGroupPublisher | ||
|
||
|
||
def test_pushgateway_group_publisher(): | ||
publisher = PushgatewayGroupPublisher('http://example.com/', 'test') | ||
publisher.add('metric1', 12) | ||
publisher.add('metric2', 13, metric_labels={'toto': 'TOTO', 'tutu': 'TUTU'}) | ||
publisher.add('metric2', 14, metric_labels={'toto': 'TOTO', 'tutu': 'TITI'}) | ||
with pytest.raises(ValueError): | ||
publisher.add('metric1', 12, metric_type='counter') | ||
|
||
assert str(publisher) == 'http://example.com/metrics/job/test ->\n' \ | ||
'# TYPE metric1 gauge\n' \ | ||
'metric1 12\n' \ | ||
'# TYPE metric2 gauge\n' \ | ||
'metric2{toto="TOTO", tutu="TUTU"} 13\n' \ | ||
'metric2{toto="TOTO", tutu="TITI"} 14\n' |