Skip to content

Commit 7404386

Browse files
committed
Fixed UWSGICollector.collect to return unique metrics.
Fixed registry_to_text format.
1 parent 31d5cfb commit 7404386

File tree

9 files changed

+50
-15
lines changed

9 files changed

+50
-15
lines changed

CHANGELOG.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Version 0.0.4
2+
-------------
3+
4+
* [BUGFIX] Fixed `UWSGICollector.collect`` to return unique metrics.
5+
* [BUGFIX] Fixed registry_to_text format.
6+
7+
Version 0.0.2
8+
-------------
9+
10+
* Initial version

MANIFEST.in

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
include setup.py README.rst MANIFEST.in LICENSE *.txt
22
recursive-include pyprometheus/ *
33
graft tests
4-
global-exclude *~
4+
global-exclude *~
5+
global-exclude #*#

Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ clean-pyc:
2121
find . -name '*~' -exec rm -f {} +
2222
find . -name "__pycache__" -exec rm -rf {} +
2323

24+
clean-dist:
25+
rm -rf dist/*
26+
rm -rf build/*
27+
2428
help:
2529
@echo "Available commands:"
2630
@sed -n '/^[a-zA-Z0-9_.]*:/s/:.*//p' <Makefile | sort
@@ -29,7 +33,7 @@ help:
2933
release:
3034
git tag -f v$(version) && git push origin v$(version)
3135

32-
publish: clean-pyc
36+
publish: clean-pyc clean-dist
3337
@echo "Create release $(version) and upload to pypi"
3438

3539
$(DOCKER_RUN_COMMAND) "python setup.py sdist bdist_wheel"

pyprometheus/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
__license__ = "BSD, see LICENSE for more details"
2020

21-
__version__ = '0.0.2'
21+
__version__ = '0.0.4'
2222

2323
__version_info__ = list(map(int, __version__.split('.')))
2424

pyprometheus/const.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ class Types(object):
3636
CONTENT_TYPE = 'text/plain; version=0.0.4; charset=utf-8'
3737

3838

39-
CREDITS = """
40-
# Python client for prometheus.io
39+
CREDITS = """# Python client for prometheus.io
4140
# http://github.com/Lispython/pyprometheus
4241
# Generated at {dt}
4342
"""

pyprometheus/contrib/uwsgi_features.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -102,25 +102,34 @@ def collect(self):
102102

103103
yield self.get_memory_samples()
104104

105-
for worker in uwsgi.workers():
106-
for sample in self.get_worker_sample(worker):
107-
yield sample
105+
for x in self.get_workers_samples(uwsgi.workers()):
106+
yield x
108107

109-
def get_worker_sample(self, worker):
108+
109+
def get_workers_samples(self, workers):
110110
"""Read worker stats and create samples
111111
112112
:param worker: worker stats
113113
"""
114114
for name in ['requests', 'respawn_count', 'running_time',
115-
'exceptions', 'requests', 'delta_requests',
115+
'exceptions', 'delta_requests',
116116
'rss', 'vsz', 'last_spawn', 'tx', 'avg_rt', 'signals']:
117117
metric = self._collectors["process:" + name]
118-
yield metric.build_samples([(self._labels + (('id', worker['id']),), ( (TYPES.GAUGE, metric.name, '', self._labels + (('id', worker['id']),), worker[name]), ))])
118+
119+
for worker in workers:
120+
labels = self._labels + (('id', worker['id']),)
121+
metric.add_sample(labels, metric.build_sample(labels,
122+
( (TYPES.GAUGE, metric.name, '', self._labels + (('id', worker['id']),), worker[name]), )))
123+
124+
yield metric
119125

120126
metric = self._collectors["process:status"]
121-
yield metric.build_samples([(self._labels + (('id', worker['id']), ('status', worker['status'])),
122-
( (TYPES.GAUGE, metric.name, '', self._labels + (('id', worker['id']), ('status', worker['status'])), 1), ))])
127+
for worker in workers:
128+
labels = self._labels + (('id', worker['id']), ('status', worker['status']))
129+
metric.add_sample(labels, metric.build_sample(labels,
130+
( (TYPES.GAUGE, metric.name, '', self._labels + (('id', worker['id']), ('status', worker['status'])), 1), )))
123131

132+
yield metric
124133

125134
def get_sample(self, name, value):
126135
"""Create sample for given name and value

pyprometheus/utils/exposition.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ def registry_to_text(registry):
2424
output.append(collector.text_export_header)
2525
for sample in samples:
2626
output.append(sample.export_str)
27+
output.append('')
2728
return '\n'.join(output)
2829

2930

tests/test_registry.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,8 @@ def test_base_registry(storage_cls, measure_time):
124124
assert test1 == test2
125125
else:
126126
assert test1.split()[:-1] == test2.split()[:-1]
127+
128+
metrics_count = map(lambda x: x.split(' ')[2],
129+
filter(lambda x: x.startswith('# HELP'), [x for x in registry_to_text(registry).split('\n')]))
130+
131+
assert len(metrics_count) == len(set(metrics_count))

tests/test_uwsgi_collector.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,19 @@ def test_uwsgi_collector():
2121
registry.register(uwsgi_collector)
2222

2323
collectors = dict([(x.name, x) for x in registry.collect()])
24-
assert len(registry_to_text(registry).split('\n')) == 63
24+
25+
metrics_count = sorted(map(lambda x: x.split(' ')[2],
26+
filter(lambda x: x.startswith('# HELP'), [x for x in registry_to_text(registry).split('\n')])))
27+
28+
assert len(metrics_count) == len(set(metrics_count))
29+
30+
assert len(registry_to_text(registry).split('\n')) == 60
2531

2632
assert collectors['uwsgi_namespace:buffer_size_bytes'].get_samples()[0].value == uwsgi.buffer_size
2733
assert collectors['uwsgi_namespace:processes_total'].get_samples()[0].value == uwsgi.numproc
2834
assert collectors['uwsgi_namespace:requests_total'].get_samples()[0].value == uwsgi.total_requests()
2935

30-
for name in ['requests', 'respawn_count', 'running_time', 'exceptions', 'requests', 'delta_requests']:
36+
for name in ['requests', 'respawn_count', 'running_time', 'exceptions', 'delta_requests']:
3137
assert collectors['uwsgi_namespace:process:{0}'.format(name)].get_samples()[0].value == uwsgi.workers()[0][name]
3238

3339
assert uwsgi_collector.metric_name("test") == "uwsgi_namespace:test"

0 commit comments

Comments
 (0)