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

Add support for passing timedeltas to StatsClientBase.timing #104

Closed
wants to merge 2 commits into from
Closed
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: 10 additions & 1 deletion docs/timing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,26 @@ The simplest way to use a timer is to record the time yourself and send
it manually, using the :ref:`timing` method::

import time
from datetime import datetime
from statsd import StatsClient

statsd = StatsClient()

# Pass milliseconds directly

start = time.time()
time.sleep(3)

# You must convert to milliseconds:
dt = int((time.time() - start) * 1000)
statsd.timing('slept', dt)

# Or pass a timedelta

start = datetime.utcnow()
time.sleep(3)
dt = datetime.utcnow() - start
statsd.timing('slept', dt)


Using a context manager
=======================
Expand Down
13 changes: 12 additions & 1 deletion statsd/client.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import with_statement
from collections import deque
from datetime import timedelta
import functools
import random
import socket
Expand Down Expand Up @@ -95,7 +96,17 @@ def timer(self, stat, rate=1):
return Timer(self, stat, rate)

def timing(self, stat, delta, rate=1):
"""Send new timing information. `delta` is in milliseconds."""
"""
Send new timing information.

`delta` can be either a number of milliseconds or a timedelta.
"""
if isinstance(delta, timedelta):
# Convert timedelta to number of milliseconds. The total_seconds()
# methods isn't use as it isn't available in Python 2.6.
delta = (delta.days * 24 * 3600000.) + \
(delta.seconds * 1000.) + \
(delta.microseconds / 1000.)
self._send_stat(stat, '%0.6f|ms' % delta, rate)

def incr(self, stat, count=1, rate=1):
Expand Down
7 changes: 7 additions & 0 deletions statsd/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import random
import re
import socket
from datetime import timedelta

import mock
from nose.tools import eq_
Expand Down Expand Up @@ -364,6 +365,12 @@ def _test_timing(cl, proto):
cl.timing('foo', 100, rate=0.5)
_sock_check(cl._sock, 3, proto, 'foo:100.000000|ms|@0.5')

cl.timing('foo', timedelta(seconds=1.5))
_sock_check(cl._sock, 4, proto, 'foo:1500.000000|ms')

cl.timing('foo', timedelta(days=1.5), rate=0.2)
_sock_check(cl._sock, 5, proto, 'foo:129600000.000000|ms|@0.2')


@mock.patch.object(random, 'random', lambda: -1)
def test_timing_udp():
Expand Down