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

Provide elapsed time from _TimedContextManagerDecorator #154

Merged
merged 1 commit into from
Sep 13, 2016
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
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