Add ability to specify a prefix to be automatically added to all metric keys
This commit is contained in:
parent
c2d934ea6b
commit
479826e9bb
|
@ -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()
|
||||
|
||||
|
||||
|
|
|
@ -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()
|
||||
|
|
Loading…
Reference in New Issue