StatsD Handler Mixin¶
The RequestMetricsMixin
mixin will automatically instrument requests by
sending statsd increment and timing values as each request finishes.
The default statsd server that is used is localhost:8125
. The
STATSD_HOST
and STATSD_PORT
environment variables can be used to
set the statsd server connection parameters. Note that the socket for
communicating with statsd is created once upon module import and will not
change until the application is restarted or the module is reloaded.
In the RequestMetricsMixin
, the statsd metrics are prefixed by default
with sprockets
. To change this value, set the new prefix with the
STATSD_PREFIX
environment variable.
Each request will send metrics on_finish
in the following format:
<STATSD_PREFIX>.counters.package[.module].Class.METHOD.STATUS
<STATSD_PREFIX>.timers.package.[.module].Class.METHOD.STATUS
RequestMetricsMixin
Example:
from sprockets.handlers.mixins import statsd
from tornado import web
class MyRequestHandler(stats.RequestMetricsMixin,
web.RequestHandler):
def get(self, *args, **kwargs):
self.finish({'hello': 'world'})
-
class
sprockets.mixins.statsd.
RequestMetricsMixin
¶ Automatically sends statsd metrics upon the completion of each request.
As with all mixins, ensure that you inherit from the mixin classes before you inherit from a concrete class. In addition to this, alway remember to
super
theon_finish
andprepare
methods should you decide to extend them.- class MyRequestHandler(
- sprockets.mixins.statsd.RequestMetricsMixin, tornado.web.RequestHandler):
- def prepare(self):
- super(RequestMetricsMixin, self).prepare() do_prepare_stuff()
@gen.coroutine def post(self):
self.write(yield self.foo())
-
on_finish
()¶ Invoked once the request has been finished. Increments a counter created in the format:
<PREFIX>.counters.<host>.package[.module].Class.METHOD.STATUS sprockets.counters.localhost.tornado.web.RequestHandler.GET.200
Adds a value to a timer in the following format:
<PREFIX>.timers.<host>.package[.module].Class.METHOD.STATUS sprockets.timers.localhost.tornado.web.RequestHandler.GET.200