2016-05-13 17:15:39 +01:00
|
|
|
from statsd import StatsClient
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class StatsdClient(StatsClient):
|
|
|
|
|
def init_app(self, app, *args, **kwargs):
|
|
|
|
|
self.active = app.config.get('STATSD_ENABLED')
|
2016-08-08 10:20:33 +01:00
|
|
|
self.namespace = app.config.get('NOTIFY_ENVIRONMENT') + ".notifications.api."
|
2016-08-05 10:44:43 +01:00
|
|
|
|
2016-08-30 10:37:06 +01:00
|
|
|
if self.active:
|
|
|
|
|
StatsClient.__init__(
|
|
|
|
|
self,
|
|
|
|
|
app.config.get('STATSD_HOST'),
|
|
|
|
|
app.config.get('STATSD_PORT'),
|
|
|
|
|
prefix=app.config.get('STATSD_PREFIX')
|
|
|
|
|
)
|
|
|
|
|
|
2016-08-05 10:44:43 +01:00
|
|
|
def format_stat_name(self, stat):
|
|
|
|
|
return self.namespace + stat
|
2016-05-13 17:15:39 +01:00
|
|
|
|
|
|
|
|
def incr(self, stat, count=1, rate=1):
|
|
|
|
|
if self.active:
|
2016-08-05 10:44:43 +01:00
|
|
|
super(StatsClient, self).incr(self.format_stat_name(stat), count, rate)
|
2016-05-13 17:15:39 +01:00
|
|
|
|
|
|
|
|
def timing(self, stat, delta, rate=1):
|
|
|
|
|
if self.active:
|
2016-08-05 10:44:43 +01:00
|
|
|
super(StatsClient, self).timing(self.format_stat_name(stat), delta, rate)
|
2016-09-13 13:57:06 +01:00
|
|
|
|
|
|
|
|
def timing_with_dates(self, stat, start, end, rate=1):
|
|
|
|
|
if self.active:
|
2016-09-13 14:40:04 +01:00
|
|
|
delta = (start - end).total_seconds()
|
2016-09-13 13:57:06 +01:00
|
|
|
super(StatsClient, self).timing(stat, delta, rate)
|