summaryrefslogtreecommitdiff
path: root/statsd
diff options
context:
space:
mode:
authorJasper Capel <jasper.capel@spilgames.com>2013-04-23 13:42:04 +0200
committerJasper Capel <jasper.capel@spilgames.com>2013-04-23 13:42:04 +0200
commit3c4ec687273416b355c20282e33249d87cd84b52 (patch)
tree5ef1b2bf276d312088c230892fc68a09c4d46c29 /statsd
parente81fb4dea2077f6e9e873d07db831bbf931df174 (diff)
downloadpystatsd-3c4ec687273416b355c20282e33249d87cd84b52.tar.gz
Added suffix parameter
Added a statsd suffix that works just like the prefix, but appends to the metric's key instead Added tests Updated documentation
Diffstat (limited to 'statsd')
-rw-r--r--statsd/__init__.py6
-rw-r--r--statsd/client.py7
-rw-r--r--statsd/tests.py18
3 files changed, 26 insertions, 5 deletions
diff --git a/statsd/__init__.py b/statsd/__init__.py
index 325fd9d..8097411 100644
--- a/statsd/__init__.py
+++ b/statsd/__init__.py
@@ -20,7 +20,8 @@ if settings:
host = getattr(settings, 'STATSD_HOST', 'localhost')
port = getattr(settings, 'STATSD_PORT', 8125)
prefix = getattr(settings, 'STATSD_PREFIX', None)
- statsd = StatsClient(host, port, prefix)
+ suffix = getattr(settings, 'STATSD_SUFFIX', None)
+ statsd = StatsClient(host, port, prefix, suffix)
except (socket.error, socket.gaierror, ImportError):
pass
elif 'STATSD_HOST' in os.environ:
@@ -28,6 +29,7 @@ elif 'STATSD_HOST' in os.environ:
host = os.environ['STATSD_HOST']
port = int(os.environ['STATSD_PORT'])
prefix = os.environ.get('STATSD_PREFIX')
- statsd = StatsClient(host, port, prefix)
+ suffix = os.environ.get('STATSD_SUFFIX')
+ statsd = StatsClient(host, port, prefix, suffix)
except (socket.error, socket.gaierror, KeyError):
pass
diff --git a/statsd/client.py b/statsd/client.py
index e88d23a..3d6bffd 100644
--- a/statsd/client.py
+++ b/statsd/client.py
@@ -37,11 +37,12 @@ class Timer(object):
class StatsClient(object):
"""A client for statsd."""
- def __init__(self, host='localhost', port=8125, prefix=None):
+ def __init__(self, host='localhost', port=8125, prefix=None, suffix=None):
"""Create a new client."""
self._addr = (socket.gethostbyname(host), port)
self._sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self._prefix = prefix
+ self._suffix = suffix
def _after(self, data):
self._send(data)
@@ -88,6 +89,9 @@ class StatsClient(object):
if self._prefix:
stat = '%s.%s' % (self._prefix, stat)
+ if self._suffix:
+ stat = '%s.%s' % (stat, self._suffix)
+
data = '%s:%s' % (stat, value)
return data
@@ -104,6 +108,7 @@ class Pipeline(StatsClient):
def __init__(self, client):
self._client = client
self._prefix = client._prefix
+ self._suffix = client._suffix
self._stats = []
def _after(self, data):
diff --git a/statsd/tests.py b/statsd/tests.py
index 59e8f24..52221c2 100644
--- a/statsd/tests.py
+++ b/statsd/tests.py
@@ -12,8 +12,8 @@ from statsd import StatsClient
ADDR = (socket.gethostbyname('localhost'), 8125)
-def _client(prefix=None):
- sc = StatsClient(host=ADDR[0], port=ADDR[1], prefix=prefix)
+def _client(prefix=None, suffix=None):
+ sc = StatsClient(host=ADDR[0], port=ADDR[1], prefix=prefix, suffix=suffix)
sc._sock = mock.Mock()
return sc
@@ -184,6 +184,20 @@ def test_prefix():
_sock_check(sc, 1, 'foo.bar:1|c')
+def test_suffix():
+ sc = _client(suffix='foo')
+
+ sc.incr('bar')
+ _sock_check(sc, 1, 'bar.foo:1|c')
+
+
+def test_prefix_and_suffix():
+ sc = _client(prefix='fooprefix', suffix='foosuffix')
+
+ sc.incr('bar')
+ _sock_check(sc, 1, 'fooprefix.bar.foosuffix:1|c')
+
+
def _timer_check(cl, count, start, end):
eq_(cl._sock.sendto.call_count, count)
value = cl._sock.sendto.call_args[0][0].decode('ascii')