summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortrbs <trbs@trbs.net>2013-09-17 00:57:41 +0200
committertrbs <trbs@trbs.net>2013-09-17 00:57:41 +0200
commitaad3c697e0b11a6343ff18f69084e56925b2b355 (patch)
tree3a1625a023afce42a08001ca58724bc681e0c54c
parentda0005cb30fac365c7bc3a9ab9a4824e1a9c3a49 (diff)
downloadpystatsd-aad3c697e0b11a6343ff18f69084e56925b2b355.tar.gz
add maxudpsize option
allows to control the maximum udp packet size. when pipelining a lot of metrics within a controlled network or to 127.0.0.1 it can be desirable to lower the number of udp packets by sending larger onces. the documentation reflects that this option is not for the faint hearted, should not be used on the internet and should be handled with extreme care.
-rw-r--r--docs/configure.rst12
-rw-r--r--statsd/__init__.py6
-rw-r--r--statsd/client.py6
3 files changed, 19 insertions, 5 deletions
diff --git a/docs/configure.rst b/docs/configure.rst
index de95fbe..37354a1 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,
+ maxudpsize=512)
``host`` is the host running the statsd server. It will support any kind
of name or IP address you might use.
@@ -50,6 +51,13 @@ 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.
+``maxudpsize`` specifies the maximum packet size statsd will use. This is
+an advanced options and should not be changed unless you know what you are
+doing. Larger values then the default of 512 are generally deemed unsafe for use
+on the internet. On a controlled local network or when the statsd server is
+running on 127.0.0.1 larger values can decrease the number of UDP packets when
+pipelining many metrics. Use with care!
+
In Django
=========
@@ -63,6 +71,7 @@ Here are the settings and their defaults::
STATSD_HOST = 'localhost'
STATSD_PORT = 8125
STATSD_PREFIX = None
+ STATSD_MAXUDPSIZE = 512
You can use the default ``StatsClient`` simply::
@@ -86,6 +95,7 @@ You can set these variables in the environment::
STATSD_HOST
STATSD_PORT
STATSD_PREFIX
+ STATSD_MAXUDPSIZE
and then in your Python application, you can simply do::
diff --git a/statsd/__init__.py b/statsd/__init__.py
index 1b690f7..e47a620 100644
--- a/statsd/__init__.py
+++ b/statsd/__init__.py
@@ -28,7 +28,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)
+ maxudpsize = getattr(settings, 'STATSD_MAXUDPSIZE', 512)
+ statsd = StatsClient(host, port, prefix, maxudpsize)
except (socket.error, socket.gaierror, ImportError):
pass
elif 'STATSD_HOST' in os.environ:
@@ -36,6 +37,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)
+ maxudpsize = int(os.environ.get('STATSD_MAXUDPSIZE', 512))
+ statsd = StatsClient(host, port, prefix, maxudpsize)
except (socket.error, socket.gaierror, KeyError):
pass
diff --git a/statsd/client.py b/statsd/client.py
index fd5e43f..5a64e6d 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, maxudpsize=512):
"""Create a new client."""
self._addr = (socket.gethostbyname(host), port)
self._sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self._prefix = prefix
+ self._maxudpsize = maxudpsize
def _after(self, data):
self._send(data)
@@ -108,6 +109,7 @@ class Pipeline(StatsClient):
def __init__(self, client):
self._client = client
self._prefix = client._prefix
+ self._maxudpsize = client._maxudpsize
self._stats = []
def _after(self, data):
@@ -126,7 +128,7 @@ class Pipeline(StatsClient):
data = self._stats.pop(0)
while self._stats:
stat = self._stats.pop(0)
- if len(stat) + len(data) + 1 >= 512:
+ if len(stat) + len(data) + 1 >= self._maxudpsize:
self._client._after(data)
data = stat
else: