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

[dogstatsd] use monotonic clock source when available for timers #615

Merged
merged 1 commit into from
Jan 8, 2021
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
11 changes: 7 additions & 4 deletions datadog/dogstatsd/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
# Copyright 2015-Present Datadog, Inc
# stdlib
from functools import wraps
from time import time
try:
from time import monotonic # type: ignore[attr-defined]
except ImportError:
from time import time as monotonic

# datadog
from datadog.dogstatsd.context_async import _get_wrapped_co
Expand Down Expand Up @@ -40,7 +43,7 @@ def __call__(self, func):
# Others
@wraps(func)
def wrapped(*args, **kwargs):
start = time()
start = monotonic()
try:
return func(*args, **kwargs)
finally:
Expand All @@ -50,15 +53,15 @@ def wrapped(*args, **kwargs):
def __enter__(self):
if not self.metric:
raise TypeError("Cannot used timed without a metric!")
self._start = time()
self._start = monotonic()
return self

def __exit__(self, type, value, traceback):
# Report the elapsed time of the context manager.
self._send(self._start)

def _send(self, start):
elapsed = time() - start
elapsed = monotonic() - 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.timing_func(self.metric, elapsed, self.tags, self.sample_rate)
Expand Down
7 changes: 5 additions & 2 deletions datadog/dogstatsd/context_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@
# https://github.com/python/mypy/issues/6897
ASYNC_SOURCE = r'''
from functools import wraps
from time import time
try:
from time import monotonic
except ImportError:
from time import time as monotonic


def _get_wrapped_co(self, func):
Expand All @@ -29,7 +32,7 @@ def _get_wrapped_co(self, func):
"""
@wraps(func)
async def wrapped_co(*args, **kwargs):
start = time()
start = monotonic()
try:
result = await func(*args, **kwargs)
return result
Expand Down
10 changes: 7 additions & 3 deletions datadog/threadstats/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
from contextlib import contextmanager
from functools import wraps
from time import time
try:
from time import monotonic # type: ignore[attr-defined]
except ImportError:
from time import time as monotonic

# datadog
from datadog.api.exceptions import ApiNotInitialized
Expand Down Expand Up @@ -272,12 +276,12 @@ def get_user(user_id):
finally:
stats.histogram('user.query.time', time.time() - start)
"""
start = time()
start = monotonic()
try:
yield
finally:
end = time()
self.timing(metric_name, end - start, end, tags=tags,
end = monotonic()
self.timing(metric_name, end - start, time(), tags=tags,
sample_rate=sample_rate, host=host)

def timed(self, metric_name, sample_rate=1, tags=None, host=None):
Expand Down