/
dogstatsd.py
37 lines (30 loc) · 1.25 KB
/
dogstatsd.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
from __future__ import absolute_import
__all__ = ["DogStatsdMetricsBackend"]
from datadog import initialize, statsd
from .base import MetricsBackend
class DogStatsdMetricsBackend(MetricsBackend):
def __init__(self, prefix=None, **kwargs):
# TODO(dcramer): it'd be nice if the initialize call wasn't a global
self.tags = kwargs.pop("tags", None)
initialize(**kwargs)
super(DogStatsdMetricsBackend, self).__init__(prefix=prefix)
def incr(self, key, instance=None, tags=None, amount=1, sample_rate=1):
if tags is None:
tags = {}
if self.tags:
tags.update(self.tags)
if instance:
tags["instance"] = instance
if tags:
tags = [u"{}:{}".format(*i) for i in tags.items()]
statsd.increment(self._get_key(key), amount, sample_rate=sample_rate, tags=tags)
def timing(self, key, value, instance=None, tags=None, sample_rate=1):
if tags is None:
tags = {}
if self.tags:
tags.update(self.tags)
if instance:
tags["instance"] = instance
if tags:
tags = [u"{}:{}".format(*i) for i in tags.items()]
statsd.timing(self._get_key(key), value, sample_rate=sample_rate, tags=tags)