Skip to content

Commit

Permalink
Merge pull request #154 from tuukkamustonen/135-provide-elapsed
Browse files Browse the repository at this point in the history
Provide elapsed time from _TimedContextManagerDecorator
  • Loading branch information
yannmh authored Sep 13, 2016
2 parents 0c87da3 + d589a2e commit 5722392
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
2 changes: 2 additions & 0 deletions datadog/dogstatsd/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ def __init__(self, statsd, metric=None, tags=None, sample_rate=1, use_ms=None):
self.tags = tags
self.sample_rate = sample_rate
self.use_ms = use_ms
self.elapsed = None

def __call__(self, func):
"""Decorator which returns the elapsed time of the function call."""
Expand Down Expand Up @@ -223,6 +224,7 @@ def _send(self, start):
use_ms = self.use_ms if self.use_ms is not None else self.statsd.use_ms
elapsed = int(round(1000 * elapsed)) if use_ms else elapsed
self.statsd.timing(self.metric, elapsed, self.tags, self.sample_rate)
self.elapsed = elapsed

def timed(self, metric=None, tags=None, sample_rate=1, use_ms=None):
"""
Expand Down
4 changes: 3 additions & 1 deletion tests/unit/dogstatsd/test_statsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,9 +324,10 @@ def test_timed_context(self):
t.assert_equal('ms', type_)
t.assert_equal('timed_context.test', name)
self.assert_almost_equal(0.5, float(value), 0.1)
self.assert_almost_equal(0.5, timer.elapsed, 0.1)

# In milliseconds
with self.statsd.timed('timed_context.test', use_ms=True):
with self.statsd.timed('timed_context.test', use_ms=True) as timer:
time.sleep(0.5)

packet = self.recv()
Expand All @@ -336,6 +337,7 @@ def test_timed_context(self):
t.assert_equal('ms', type_)
t.assert_equal('timed_context.test', name)
self.assert_almost_equal(500, float(value), 100)
self.assert_almost_equal(500, timer.elapsed, 100)

def test_timed_context_exception(self):
"""
Expand Down

0 comments on commit 5722392

Please sign in to comment.