summaryrefslogtreecommitdiff
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
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
-rw-r--r--docs/configure.rst17
-rw-r--r--docs/index.rst6
-rw-r--r--docs/reference.rst4
-rw-r--r--statsd/__init__.py6
-rw-r--r--statsd/client.py7
-rw-r--r--statsd/tests.py18
6 files changed, 48 insertions, 10 deletions
diff --git a/docs/configure.rst b/docs/configure.rst
index de95fbe..ae57c0b 100644
--- a/docs/configure.rst
+++ b/docs/configure.rst
@@ -26,7 +26,8 @@ They, and their defaults, are::
statsd = StatsClient(host='localhost',
port=8125,
- prefix=None)
+ prefix=None,
+ suffix=None)
``host`` is the host running the statsd server. It will support any kind
of name or IP address you might use.
@@ -50,6 +51,19 @@ will produce two different stats, ``foo.baz`` and ``bar.baz``. Without
the ``prefix`` argument, or with the same ``prefix``, two
``StatsClient`` instances will update the same stats.
+``suffix`` can also be used to distinguish between hosts or environments.
+The suffix will be appended to all stats, automatically. This is useful
+when working with clusters consisting of multiple hosts. For example::
+
+ from statsd import StatsClient
+ from os import uname
+
+ myhostname = uname()[1].split(".")[0]
+ stats = StatsClient(suffix=myhostname)
+
+will append ``.hostname`` to all stats sent to statsd. Without this, all
+hosts in a cluster would update the same stats.
+
In Django
=========
@@ -63,6 +77,7 @@ Here are the settings and their defaults::
STATSD_HOST = 'localhost'
STATSD_PORT = 8125
STATSD_PREFIX = None
+ STATSD_SUFFIX = None
You can use the default ``StatsClient`` simply::
diff --git a/docs/index.rst b/docs/index.rst
index 0118c76..5e3dba9 100644
--- a/docs/index.rst
+++ b/docs/index.rst
@@ -20,11 +20,11 @@ Quickly, to use::
>>> c.incr('foo') # Increment the 'foo' counter.
>>> c.timing('stats.timed', 320) # Record a 320ms 'stats.timed'.
-You can also add a prefix to all your stats::
+You can also add a prefix and/or suffix to all your stats::
>>> import statsd
- >>> c = statsd.StatsClient('localhost', 8125, prefix='foo')
- >>> c.incr('bar') # Will be 'foo.bar' in statsd/graphite.
+ >>> c = statsd.StatsClient('localhost', 8125, prefix='foo', suffix='baz')
+ >>> c.incr('bar') # Will be 'foo.bar.baz' in statsd/graphite.
Installing
diff --git a/docs/reference.rst b/docs/reference.rst
index df74e75..aa1a5a3 100644
--- a/docs/reference.rst
+++ b/docs/reference.rst
@@ -21,7 +21,7 @@ statsd_ server supports.
::
- StatsClient(host='localhost', port=8125, prefix=None)
+ StatsClient(host='localhost', port=8125, prefix=None, suffix=None)
Create a new ``StatsClient`` instance with the appropriate connection
and prefix information.
@@ -33,6 +33,8 @@ and prefix information.
* ``prefix``: a prefix to distinguish and group stats from an
application or environment.
+* ``suffix``: a suffix to distinguish and group stats from an
+ application or environment.
.. _incr:
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')