Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #1 from vgod/master

add statsd.gauge
  • Loading branch information...
commit 6a72f394604238a06a9aee8345f283623fbf8d38 2 parents 4b82d72 + d7271f8
@gaelenh authored
Showing with 50 additions and 12 deletions.
  1. +7 −0 README.md
  2. +25 −12 statsd.py
  3. +18 −0 statsd_test.py
View
7 README.md
@@ -62,6 +62,13 @@ Want to count things? Use StatsdCounter:
counter += 5 # equivalent to counter.incr(5)
counter -= 10 # equivalent to counter.decr(10)
+### Gauge
+Want to gauge something? Use statsd.gauge:
+
+ import statsd
+ statsd.init_statsd({'STATSD_BUCKET_PREFIX': 'photos'})
+ statsd.gauge('filesize', 100) # sends out gauge value 100 for bucket 'photos.filesize'
+
### Timing
Interested in timing? Check out all the ways you can time things:
View
37 statsd.py
@@ -7,8 +7,9 @@
import random
from socket import socket, AF_INET, SOCK_DGRAM
import time
+import logging
-__version__ = '1.0.2'
+__version__ = '1.0.4'
STATSD_HOST = 'localhost'
STATSD_PORT = 8125
@@ -22,6 +23,8 @@ def decrement(bucket, delta=1, sample_rate=None):
def increment(bucket, delta=1, sample_rate=None):
_statsd.incr(bucket, delta, sample_rate)
+def gauge(bucket, value, sample_rate=None):
+ _statsd.gauge(bucket, value, sample_rate)
def timing(bucket, ms, sample_rate=None):
_statsd.timing(bucket, ms, sample_rate)
@@ -50,23 +53,32 @@ def incr(self, bucket, delta=1, sample_rate=None):
value = str(delta).encode('utf8') + b'|c'
self._send(bucket, value, sample_rate)
+ def gauge(self, bucket, value, sample_rate=None):
+ """Send a gauge value.
+ """
+ str_value = str(value).encode('utf8') + b'|g'
+ self._send(bucket, str_value, sample_rate)
+
def _send(self, bucket, value, sample_rate=None):
"""Format and send data to statsd.
"""
- bucket = bucket if isinstance(bucket, bytes) else bucket.encode('utf8')
+ try:
+ bucket = bucket if isinstance(bucket, bytes) else bucket.encode('utf8')
- sample_rate = sample_rate or self._sample_rate
- if sample_rate and sample_rate < 1.0 and sample_rate > 0:
- if random.random() <= sample_rate:
- value = value + b'|@' + str(sample_rate).encode('utf8')
- else:
- return
+ sample_rate = sample_rate or self._sample_rate
+ if sample_rate and sample_rate < 1.0 and sample_rate > 0:
+ if random.random() <= sample_rate:
+ value = value + b'|@' + str(sample_rate).encode('utf8')
+ else:
+ return
- stat = bucket + b':' + value
- if self._prefix:
- stat = self._prefix + b'.' + stat
+ stat = bucket + b':' + value
+ if self._prefix:
+ stat = self._prefix + b'.' + stat
- self._socket.sendto(stat, (self._host, self._port))
+ self._socket.sendto(stat, (self._host, self._port))
+ except Exception,e:
+ _logger.error("Failed to send statsd packet.", exc_info=True)
def timing(self, bucket, ms, sample_rate=None):
"""Creates a timing sample.
@@ -166,4 +178,5 @@ def init_statsd(settings=None):
sample_rate=STATSD_SAMPLE_RATE, prefix=STATSD_BUCKET_PREFIX)
return _statsd
+_logger = logging.getLogger('statsd')
_statsd = init_statsd()
View
18 statsd_test.py
@@ -42,6 +42,14 @@ def test_init_statsd(self):
self.assertEqual(statsd.STATSD_SAMPLE_RATE, 0.99)
self.assertEqual(statsd.STATSD_BUCKET_PREFIX, 'testing')
+ def test_exception_in_send(self):
+ def mock_sendto_raise_error(data, addr):
+ mock_sendto_raise_error.exception_raised = True
+ raise socket.gaierror
+ statsd._statsd._socket.sendto = mock_sendto_raise_error
+ statsd.decrement('counted')
+ self.assertTrue(mock_sendto_raise_error.exception_raised)
+
def test_decrement(self):
statsd.decrement('counted')
self.assertEqual(statsd._statsd._socket.data, b'counted:-1|c')
@@ -62,6 +70,16 @@ def test_increment(self):
if statsd._statsd._socket.data != b'counted:5|c':
self.assertTrue(statsd._statsd._socket.data.endswith(b'|@0.99'))
+ def test_gauge(self):
+ statsd.gauge('gauged', 1)
+ self.assertEqual(statsd._statsd._socket.data, b'gauged:1|g')
+ statsd.gauge('gauged', 5)
+ self.assertEqual(statsd._statsd._socket.data, b'gauged:5|g')
+ statsd.gauge('gauged', -5, 0.99)
+ self.assertTrue(statsd._statsd._socket.data.startswith(b'gauged:-5|g'))
+ if statsd._statsd._socket.data != b'gauged:-5|g':
+ self.assertTrue(statsd._statsd._socket.data.endswith(b'|@0.99'))
+
def test_timing(self):
statsd.timing('timed', 250)
self.assertEqual(statsd._statsd._socket.data, b'timed:250|ms')

0 comments on commit 6a72f39

Please sign in to comment.
Something went wrong with that request. Please try again.