Skip to content

Commit

Permalink
fix(metric): only print help/type once for summaries
Browse files Browse the repository at this point in the history
  • Loading branch information
ssube committed Dec 26, 2019
1 parent 72a5b0b commit ad58e32
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 7 deletions.
8 changes: 4 additions & 4 deletions README.md
Expand Up @@ -83,7 +83,7 @@ with Chrome, curl, and Prometheus itself.

### Labels

Labels are not yet implemented.
Labels are stored and used to accumulate values. Missing labels are reported as `None`.

### Metric Types

Expand All @@ -99,7 +99,7 @@ Absolute values. Extends [counter](#counter) with `set(value)`.

#### Summary

Prints count and total of `observe(value)`.
Individual values. Prints count and total of `observe(value)`.

### Registries

Expand Down Expand Up @@ -147,7 +147,7 @@ Transfer/sec: 32.60KB

Some are fatal:

```
```none
Connection from ('client', 8080)
Accepting...
Connection from ('client', 8080)
Expand All @@ -165,7 +165,7 @@ OSError: 4

Others require the socket to be rebound:

```
```none
Connection from ('client', 8080)
Accepting...
Error accepting request: 7
Expand Down
2 changes: 0 additions & 2 deletions prometheus_express/metric.py
Expand Up @@ -143,9 +143,7 @@ def render(self, namespace):
lines = super(Summary, self).render(namespace)
for l, v in self.values.items():
ll = render_labels(self.labelKeys, l)
lines.extend(render_help(nn + '_count', self.desc, self.metricType))
lines.append('{}_count{} {}'.format(nn, ll, v[0]))
lines.extend(render_help(nn + '_total', self.desc, self.metricType))
lines.append('{}_total{} {}'.format(nn, ll, v[1]))

return lines
54 changes: 53 additions & 1 deletion tests/test_metric.py
Expand Up @@ -67,7 +67,7 @@ def test(self):
], r)

class CounterRenderTest(unittest.TestCase):
def test(self):
def test_simple(self):
m = pm.Counter('bin', 'bin values', [
'key_1', 'key_2',
])
Expand All @@ -86,3 +86,55 @@ def test(self):
'foo_bin{key_1="None",key_2="None"} 0',
'foo_bin{key_1="value-1",key_2="value-2"} 90',
], v)

'''
ensure repeated label sets are combined and values
are accumulated correctly
'''
def test_repeat_labels(self):
m = pm.Counter('bin', 'bin values', [
'key_1', 'key_2',
])
m.labels('value-1', 'value-2')
m.inc(30)

m.labels('foo-1', 'foo-2')
m.inc(20)

m.labels('value-1', 'value-2')
m.inc(60)

r = m.render('foo')
v = r[2:]
v.sort()
self.assertEqual([
'foo_bin{key_1="None",key_2="None"} 0',
'foo_bin{key_1="foo-1",key_2="foo-2"} 20',
'foo_bin{key_1="value-1",key_2="value-2"} 90',
], v)

class SummaryRenderTest(unittest.TestCase):
def test(self):
m = pm.Summary('foo', 'foo values', ['group'])
m.labels('foo').observe(4)
m.labels('bar').observe(2)
m.labels('foo').observe(6)
m.labels('bar').observe(8)

r = m.render('bar')
self.assertEqual(len(r), 8, 'should have 2 lines of help and 6 values')
self.assertEqual(r[:2], [
'# HELP bar_foo foo values',
'# TYPE bar_foo summary',
], 'should begin with the help and type lines')

v = r[2:]
v.sort()
self.assertEqual([
'bar_foo_count{group="None"} 0',
'bar_foo_count{group="bar"} 2',
'bar_foo_count{group="foo"} 2',
'bar_foo_total{group="None"} 0',
'bar_foo_total{group="bar"} 10',
'bar_foo_total{group="foo"} 10',
], v)

0 comments on commit ad58e32

Please sign in to comment.