summaryrefslogtreecommitdiffstats
path: root/gstatsd/client.py
diff options
context:
space:
mode:
Diffstat (limited to 'gstatsd/client.py')
-rw-r--r--gstatsd/client.py43
1 files changed, 43 insertions, 0 deletions
diff --git a/gstatsd/client.py b/gstatsd/client.py
new file mode 100644
index 0000000..5d5f34f
--- /dev/null
+++ b/gstatsd/client.py
@@ -0,0 +1,43 @@
+
+
+import random
+import socket
+
+
+class StatsClient(object):
+
+ "Simple client to exercise the statsd server."
+
+ HOSTPORT = ('', 8125)
+
+ def __init__(self, hostport=None):
+ if hostport is None:
+ hostport = StatsClient.HOSTPORT
+ self._hostport = hostport
+ self._sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
+
+ def timer(self, key, timestamp, sample_rate=1):
+ self._send('%s:%d|ms' % (key, timestamp), sample_rate)
+
+ def increment(self, key, sample_rate=1):
+ return self.counter(key, 1, sample_rate)
+
+ def decrement(self, key, sample_rate=1):
+ return self.counter(key, -1, sample_rate)
+
+ def counter(self, keys, magnitude=1, sample_rate=1):
+ if not isinstance(keys, (list, tuple)):
+ keys = [keys]
+ for key in keys:
+ self._send('%s:%s|c' % (key, magnitude), sample_rate)
+
+ def _send(self, data, sample_rate=1):
+ packet = None
+ if sample_rate < 1.0:
+ if random.random() < sample_rate:
+ packet = data + '|@%s' % sample_rate
+ else:
+ packet = data
+ if packet:
+ self._sock.sendto(packet, self._hostport)
+