Skip to content

Commit 10bf2d0

Browse files
authored
honor existing statistic if present (#24)
For counters and gauges, the statistic provided on the id should be used if present. This allows these base types to be used as a building block for other composite types.
1 parent 5f8ed9d commit 10bf2d0

File tree

6 files changed

+21
-3
lines changed

6 files changed

+21
-3
lines changed

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def read(fname):
1212

1313
setup(
1414
name='netflix-spectator-py',
15-
version='0.1.11',
15+
version='0.1.12',
1616
description='Python library for reporting metrics to Atlas.',
1717
long_description=read('README.md'),
1818
author='Brian Harrington',

spectator/counter.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,5 @@ def count(self):
4646

4747
def _measure(self):
4848
return {
49-
self.meterId.with_stat('count'): self._count.get_and_set(0)
49+
self.meterId.with_default_stat('count'): self._count.get_and_set(0)
5050
}

spectator/gauge.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def _has_expired(self):
5252
return (self._clock.wall_time() - self._last_update.get()) > self.ttl
5353

5454
def _measure(self):
55-
id = self.meterId.with_stat('gauge')
55+
id = self.meterId.with_default_stat('gauge')
5656

5757
if self._has_expired():
5858
v = self._value.get_and_set(float('nan'))

spectator/id.py

+6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ def tags(self):
99
def with_stat(self, v):
1010
return self.with_tag('statistic', v)
1111

12+
def with_default_stat(self, v):
13+
if 'statistic' in self._tags:
14+
return self
15+
else:
16+
return self.with_stat(v)
17+
1218
def with_tag(self, k, v):
1319
tags = self._tags.copy()
1420
tags[k] = v

tests/test_counter.py

+6
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,9 @@ def test_measure(self):
2525
self.assertEqual(len(ms), 1)
2626
self.assertEqual(ms[CounterTest.tid.with_stat('count')], 1)
2727
self.assertEqual(c.count(), 0)
28+
29+
def test_user_statistic(self):
30+
c = Counter(CounterTest.tid.with_stat('totalTime'))
31+
c.increment()
32+
for id in c._measure().keys():
33+
self.assertEqual('totalTime', id.tags()['statistic'])

tests/test_gauge.py

+6
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,9 @@ def test_ttl_reset(self):
3333
self.assertTrue(math.isnan(g.get()))
3434
self.assertEqual(1, len(ms))
3535
self.assertEqual(42, ms[GaugeTest.tid.with_stat('gauge')])
36+
37+
def test_user_statistic(self):
38+
g = Gauge(GaugeTest.tid.with_stat('duration'))
39+
g.set(42)
40+
for id in g._measure().keys():
41+
self.assertEqual('duration', id.tags()['statistic'])

0 commit comments

Comments
 (0)