class Gauge(AbstractGauge): ttl = 15 * 60 def __init__(self, meterId, clock=SystemClock()): self.meterId = meterId self._clock = clock self._last_update = AtomicNumber(float('nan')) self._value = AtomicNumber(float('nan')) def get(self): return self._value.get() def set(self, value): self._last_update.set(self._clock.wall_time()) self._value.set(value) def _has_expired(self): return (self._clock.wall_time() - self._last_update.get()) > self.ttl def _measure(self): id = self.meterId.with_default_stat('gauge') if self._has_expired(): v = self._value.get_and_set(float('nan')) else: v = self._value.get() return {id: v}
class Gauge: def __init__(self, meterId): self.meterId = meterId self._value = AtomicNumber(float('nan')) def get(self): return self._value.get() def set(self, value): self._value.set(value) def _measure(self): id = self.meterId.with_stat('gauge') v = self._value.get_and_set(float('nan')) return {id: v}
def test_init(self): v = AtomicNumber(42) self.assertEqual(42, v.get()) v.set(44) self.assertEqual(44, v.get())