summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRobert Leftwich <rleftwich@lightkeeper.com>2012-10-27 10:58:35 (GMT)
committerPatrick Hensley <spaceboy@indirect.com>2013-12-03 21:05:12 (GMT)
commit479826e9bb0a849f37c4a524cc3c86b3c9c59afa (patch)
treec621b52ea006ab34bce779846a2aaba1cd23c8c5
parentc2d934ea6bc3b48bbe2a673a3ea7e416ced759df (diff)
downloadgstatsd-479826e9bb0a849f37c4a524cc3c86b3c9c59afa.zip
gstatsd-479826e9bb0a849f37c4a524cc3c86b3c9c59afa.tar.gz
gstatsd-479826e9bb0a849f37c4a524cc3c86b3c9c59afa.tar.bz2
Add ability to specify a prefix to be automatically added to all metric keys
-rw-r--r--gstatsd/service.py10
-rw-r--r--gstatsd/service_test.py7
2 files changed, 15 insertions, 2 deletions
diff --git a/gstatsd/service.py b/gstatsd/service.py
index 7130965..4ece952 100644
--- a/gstatsd/service.py
+++ b/gstatsd/service.py
@@ -94,7 +94,8 @@ class StatsDaemon(object):
A statsd service implementation in Python + gevent.
"""
- def __init__(self, bindaddr, sinkspecs, interval, percent, debug=0):
+ def __init__(self, bindaddr, sinkspecs, interval, percent, debug=0,
+ key_prefix=''):
_, host, port = parse_addr(bindaddr)
if port is None:
self.exit(E_BADADDR % bindaddr)
@@ -124,6 +125,7 @@ class StatsDaemon(object):
self._debug = debug
self._sock = None
self._flush_task = None
+ self._key_prefix = key_prefix
self._reset_stats()
@@ -188,6 +190,8 @@ class StatsDaemon(object):
# interpret the packet and update stats
stats = self._stats
key = parts[0].translate(KEY_TABLE, KEY_DELETIONS)
+ if self._key_prefix:
+ key = '.'.join([self._key_prefix, key])
for part in parts[1:]:
srate = 1.0
fields = part.split('|')
@@ -224,6 +228,8 @@ def main():
help="increase verbosity (currently used for debugging)")
opts.add_option('-f', '--flush', dest='interval', default=INTERVAL,
help="flush interval, in seconds (default 10)")
+ opts.add_option('-x', '--prefix', dest='key_prefix', default='',
+ help="key prefix added to all keys (default None)")
opts.add_option('-p', '--percent', dest='percent', default=PERCENT,
help="percent threshold (default 90)")
opts.add_option('-D', '--daemonize', dest='daemonize', action='store_true',
@@ -242,7 +248,7 @@ def main():
daemonize()
sd = StatsDaemon(options.bind_addr, options.sink, options.interval,
- options.percent, options.verbose)
+ options.percent, options.verbose, options.key_prefix)
sd.start()
diff --git a/gstatsd/service_test.py b/gstatsd/service_test.py
index 6f46ff8..129c96f 100644
--- a/gstatsd/service_test.py
+++ b/gstatsd/service_test.py
@@ -60,6 +60,13 @@ class StatsServiceTest(unittest.TestCase):
self.svc._process(pkt, None)
self.assertEquals(self.stats.counts, {'foo.bar': 1})
+ def test_key_prefix(self):
+ args = (':8125', [':2003'], 5, 90, 0, 'pfx')
+ svc = service.StatsDaemon(*args)
+ pkt = 'foo:1|c'
+ svc._process(pkt, None)
+ self.assertEquals(svc._stats.counts, {'pfx.foo': 1})
+
def main():
unittest.main()