Skip to content
This repository has been archived by the owner on Dec 15, 2018. It is now read-only.

Commit

Permalink
Add StatsReporter timer helper; use it to report time spent in Tracke…
Browse files Browse the repository at this point in the history
…dTask execution

Summary: It is useful to track how long tasks take.

Test Plan: Unit, still queued.

Reviewers: jukka

Reviewed By: jukka

Subscribers: changesbot, wwu

Differential Revision: https://tails.corp.dropbox.com/D97157
  • Loading branch information
kylec1 committed Mar 20, 2015
1 parent 0ad6f2e commit 1fb2e22
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
12 changes: 12 additions & 0 deletions changes/ext/statsreporter.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import statsd
import re
import time
import logging
from contextlib import contextmanager

logger = logging.getLogger('statsreporter')

Expand Down Expand Up @@ -93,6 +95,16 @@ def log_timing(self, key, duration_ms):
if self._client:
self._client.timing(key, duration_ms)

@contextmanager
def timer(self, key):
"""A contextmanager that reports the duration in milliseconds on exit."""
t0 = time.time()
try:
yield
finally:
duration_ms = int(1000 * (time.time() - t0))
self.log_timing(key, duration_ms)

_KEY_RE = re.compile(r'^[A-Za-z0-9_-]+$')

@classmethod
Expand Down
5 changes: 3 additions & 2 deletions changes/queue/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,9 @@ def __init__(self, func, max_retries=MAX_RETRIES, on_abort=None):
self.__code__ = getattr(func, '__code__', None)

def __call__(self, **kwargs):
with self.lock:
self._run(kwargs)
with statsreporter.stats().timer('task_duration_' + self.task_name):
with self.lock:
self._run(kwargs)

def __repr__(self):
return '<%s: task_name=%s>' % (type(self), self.task_name)
Expand Down

0 comments on commit 1fb2e22

Please sign in to comment.