Skip to content

Commit

Permalink
honor existing statistic if present (#24)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
brharrington committed Dec 12, 2019
1 parent 5f8ed9d commit 10bf2d0
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 3 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def read(fname):

setup(
name='netflix-spectator-py',
version='0.1.11',
version='0.1.12',
description='Python library for reporting metrics to Atlas.',
long_description=read('README.md'),
author='Brian Harrington',
Expand Down
2 changes: 1 addition & 1 deletion spectator/counter.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,5 @@ def count(self):

def _measure(self):
return {
self.meterId.with_stat('count'): self._count.get_and_set(0)
self.meterId.with_default_stat('count'): self._count.get_and_set(0)
}
2 changes: 1 addition & 1 deletion spectator/gauge.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def _has_expired(self):
return (self._clock.wall_time() - self._last_update.get()) > self.ttl

def _measure(self):
id = self.meterId.with_stat('gauge')
id = self.meterId.with_default_stat('gauge')

if self._has_expired():
v = self._value.get_and_set(float('nan'))
Expand Down
6 changes: 6 additions & 0 deletions spectator/id.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ def tags(self):
def with_stat(self, v):
return self.with_tag('statistic', v)

def with_default_stat(self, v):
if 'statistic' in self._tags:
return self
else:
return self.with_stat(v)

def with_tag(self, k, v):
tags = self._tags.copy()
tags[k] = v
Expand Down
6 changes: 6 additions & 0 deletions tests/test_counter.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,9 @@ def test_measure(self):
self.assertEqual(len(ms), 1)
self.assertEqual(ms[CounterTest.tid.with_stat('count')], 1)
self.assertEqual(c.count(), 0)

def test_user_statistic(self):
c = Counter(CounterTest.tid.with_stat('totalTime'))
c.increment()
for id in c._measure().keys():
self.assertEqual('totalTime', id.tags()['statistic'])
6 changes: 6 additions & 0 deletions tests/test_gauge.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,9 @@ def test_ttl_reset(self):
self.assertTrue(math.isnan(g.get()))
self.assertEqual(1, len(ms))
self.assertEqual(42, ms[GaugeTest.tid.with_stat('gauge')])

def test_user_statistic(self):
g = Gauge(GaugeTest.tid.with_stat('duration'))
g.set(42)
for id in g._measure().keys():
self.assertEqual('duration', id.tags()['statistic'])

0 comments on commit 10bf2d0

Please sign in to comment.