sprockets.mixins.statsd¶
The RequestMetricsMixin
mixin will automatically instrument requests by
sending statsd increment and timing values as each request finishes.
Installation¶
sprockets.mixins.statsd
is available on the
Python Package Index
and can be installed via pip
or easy_install
:
pip install sprockets.mixins.statsd
Requirements¶
API Documentation¶
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
Examples¶
The following RequestHandler
will automatically increment a request counter
and add a request duration timing value to statsd when the request finishes.
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'})
When the request has finished, the following keys would be used:
- Counter:
sprockets.counter.example.RequestHandler.GET.200
- Timing:
sprockets.timers.example.RequestHandler.GET.200
Version History¶
See Version History
Issues¶
Please report any issues to the Github project at https://github.com/sprockets/sprockets.mixins.statsd/issues
Source¶
sprockets.mixins.statsd source is available on Github at https://github.com/sprockets/sprockets.mixins.statsd
License¶
sprockets.mixins.statsd is released under the 3-Clause BSD license.