Provide for meta information in counters

This allows us to provide some timing data along with the counters.
This commit is contained in:
Clint Byrum 2015-11-11 14:37:47 -08:00
parent 64f4826e02
commit 1d0cf8a515
2 changed files with 22 additions and 3 deletions

View File

@ -13,7 +13,7 @@
import json
def delta(previous, current):
def delta(previous, current, meta=False):
product = {}
seen = set()
@ -27,10 +27,17 @@ def delta(previous, current):
'Type of key %s changed from %s to %s' % (k,
type(v),
type(newv)))
if isinstance(v, int) or isinstance(v, float):
if k == '__meta__':
meta = True
if meta and k == 'delta_seconds':
continue
elif meta and k == 'unixtime':
product['delta_seconds'] = newv - v
product[k] = newv
elif isinstance(v, int) or isinstance(v, float):
product[k] = newv - v
elif isinstance(v, dict):
product[k] = delta(v, newv)
product[k] = delta(v, newv, meta)
else:
raise ValueError('Only mappings of numbers are understood')
seen.add(k)

View File

@ -60,6 +60,18 @@ class TestOSQATDelta(testscenarios.WithScenarios, base.TestCase):
previous={'zoo': {'horse': 7}},
current={'zoo': 15},
expected=ValueError)),
('meta_unixtime', dict(
previous={'__meta__': {'unixtime': 1.0}},
current={'__meta__': {'unixtime': 10.5}},
expected={'__meta__': {'unixtime': 10.5, 'delta_seconds': 9.5}})),
('meta_unixtime_gone', dict(
previous={'__meta__': {'unixtime': 1.0}},
current={},
expected={})),
('meta_unixtime_new', dict(
previous={},
current={'__meta__': {'unixtime': 1.0}},
expected={'__meta__': {'unixtime': 1.0}})),
]
def test_delta(self):