Skip to content

Commit

Permalink
Merge pull request sivy#24 from robyoung/master
Browse files Browse the repository at this point in the history
Convenience method timings since (plus previous commit) -- thanks @robyoung
  • Loading branch information
sivy committed Sep 12, 2011
2 parents 48f1ff0 + a8bd185 commit be0e205
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions pystatsd/statsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
import logging
import socket
import random
import time

# Sends statistics to the stats daemon over UDP
class Client(object):

def __init__(self, host='localhost', port=8125):
def __init__(self, host='localhost', port=8125, prefix=None):
"""
Create a new Statsd client.
* host: the host where statsd is listening, defaults to localhost
Expand All @@ -21,9 +22,20 @@ def __init__(self, host='localhost', port=8125):
"""
self.host = host
self.port = int(port)
self.prefix = prefix
self.log = logging.getLogger("pystatsd.client")
self.udp_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

def timing_since(self, stat, start, sample_rate=1):
"""
Log timing information as the number of microseconds since the provided time float
>>> start = time.time()
>>> # do stuff
>>> statsd_client.timing_since('some.time', start)
"""
self.timing(stat, int((time.time() - start) * 1000000), sample_rate)


def timing(self, stat, time, sample_rate=1):
"""
Log timing information for a single stat
Expand Down Expand Up @@ -64,7 +76,8 @@ def send(self, data, sample_rate=1):
"""
addr = (self.host, self.port)

sampled_data = {}
if self.prefix:
data = dict((".".join((self.prefix, stat)), value) for stat, value in data.iteritems())

if sample_rate < 1:
if random.random() > sample_rate:
Expand Down

0 comments on commit be0e205

Please sign in to comment.