Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expire counter values #245

Merged
merged 3 commits into from
Nov 7, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 14 additions & 11 deletions checks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ def _rate(cls, sample1, sample2):
except Exception, e:
raise NaN(e)

def get_sample_with_timestamp(self, metric, tags=None, device_name=None):
def get_sample_with_timestamp(self, metric, tags=None, device_name=None, expire=True):
"Get (timestamp-epoch-style, value)"

# Get the proper tags
Expand All @@ -210,45 +210,48 @@ def get_sample_with_timestamp(self, metric, tags=None, device_name=None):

# Not enough value to compute rate
elif self.is_counter(metric) and len(self._sample_store[metric][key]) < 2:
raise UnknownValue()
raise UnknownValue()

elif self.is_counter(metric) and len(self._sample_store[metric][key]) >= 2:
return self._rate(self._sample_store[metric][key][-2], self._sample_store[metric][key][-1])
res = self._rate(self._sample_store[metric][key][-2], self._sample_store[metric][key][-1])
if expire:
del self._sample_store[metric][key][:-1]
return res

elif self.is_gauge(metric) and len(self._sample_store[metric][key]) >= 1:
return self._sample_store[metric][key][-1]

else:
raise UnknownValue()

def get_sample(self, metric, tags=None, device_name=None):
def get_sample(self, metric, tags=None, device_name=None, expire=True):
"Return the last value for that metric"
x = self.get_sample_with_timestamp(metric, tags, device_name)
x = self.get_sample_with_timestamp(metric, tags, device_name, expire)
assert type(x) == types.TupleType and len(x) == 4, x
return x[1]

def get_samples_with_timestamps(self):
def get_samples_with_timestamps(self, expire=True):
"Return all values {metric: (ts, value)} for non-tagged metrics"
values = {}
for m in self._sample_store:
try:
values[m] = self.get_sample_with_timestamp(m)
values[m] = self.get_sample_with_timestamp(m, expire=expire)
except:
pass
return values

def get_samples(self):
def get_samples(self, expire=True):
"Return all values {metric: value} for non-tagged metrics"
values = {}
for m in self._sample_store:
try:
# Discard the timestamp
values[m] = self.get_sample_with_timestamp(m)[1]
values[m] = self.get_sample_with_timestamp(m, expire=expire)[1]
except:
pass
return values

def get_metrics(self):
def get_metrics(self, expire=True):
"""Get all metrics, including the ones that are tagged.
This is the preferred method to retrieve metrics

Expand All @@ -261,7 +264,7 @@ def get_metrics(self):
for key in self._sample_store[m]:
tags, device_name = key
try:
ts, val, hostname, device_name = self.get_sample_with_timestamp(m, tags, device_name)
ts, val, hostname, device_name = self.get_sample_with_timestamp(m, tags, device_name, expire)
except UnknownValue:
continue
attributes = {}
Expand Down
16 changes: 8 additions & 8 deletions tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ def testEdgeCases(self):

def test_counter(self):
self.c.save_sample("test-counter", 1.0, 1.0)
self.assertRaises(UnknownValue, self.c.get_sample, "test-counter")
self.assertRaises(UnknownValue, self.c.get_sample, "test-counter", expire=False)
self.c.save_sample("test-counter", 2.0, 2.0)
self.assertEquals(self.c.get_sample("test-counter"), 1.0)
self.assertEquals(self.c.get_sample_with_timestamp("test-counter"), (2.0, 1.0, None, None))
self.assertEquals(self.c.get_samples(), {"test-counter": 1.0})
self.assertEquals(self.c.get_sample("test-counter", expire=False), 1.0)
self.assertEquals(self.c.get_sample_with_timestamp("test-counter", expire=False), (2.0, 1.0, None, None))
self.assertEquals(self.c.get_samples(expire=False), {"test-counter": 1.0})
self.c.save_sample("test-counter", -2.0, 3.0)
self.assertRaises(UnknownValue, self.c.get_sample_with_timestamp, "test-counter")

Expand Down Expand Up @@ -76,10 +76,10 @@ def test_samples(self):
self.c.save_sample("test-metric", 1.0, 0.0) # value, ts
self.c.save_sample("test-counter", 1.0, 1.0) # value, ts
self.c.save_sample("test-counter", 4.0, 2.0) # value, ts
assert "test-metric" in self.c.get_samples_with_timestamps(), self.c.get_samples_with_timestamps()
self.assertEquals(self.c.get_samples_with_timestamps()["test-metric"], (0.0, 1.0, None, None))
assert "test-counter" in self.c.get_samples_with_timestamps(), self.c.get_samples_with_timestamps()
self.assertEquals(self.c.get_samples_with_timestamps()["test-counter"], (2.0, 3.0, None, None))
assert "test-metric" in self.c.get_samples_with_timestamps(expire=False), self.c.get_samples_with_timestamps(expire=False)
self.assertEquals(self.c.get_samples_with_timestamps(expire=False)["test-metric"], (0.0, 1.0, None, None))
assert "test-counter" in self.c.get_samples_with_timestamps(expire=False), self.c.get_samples_with_timestamps(expire=False)
self.assertEquals(self.c.get_samples_with_timestamps(expire=False)["test-counter"], (2.0, 3.0, None, None))

def test_name(self):
self.assertEquals(self.c.normalize("metric"), "metric")
Expand Down