Examples¶
Sending metrics to StatsD¶
This simple application emits metrics to port 8125 on localhost. The
mix-in is configured by passing a sprockets.mixins.metrics.statsd
key into the application settings as shown below.
def make_application():
"""
Create a application configured to send metrics.
Metrics will be sent to localhost:8125 namespaced with
``webapps``. Run netcat or a similar listener then run this
example. HTTP GETs will result in a metric like::
webapps.SimpleHandler.GET.204:255.24497032165527|ms
"""
settings = {}
application = web.Application([web.url('/', SimpleHandler)], **settings)
statsd.install(application, **{'namespace': 'testing'})
return application
The request handler is simple. In fact, there is nothing of interest
there except that it uses StatsdMixin
as a base class.
class SimpleHandler(statsd.StatsdMixin, web.RequestHandler):
"""
Simply emits a timing metric around the method call.
The metric namespace and StatsD endpoint are configured in
the application settings object so there is nothing to do in
a request handler.
"""
async def get(self):
await asyncio.sleep(0.25)
self.set_status(204)
self.finish()
def post(self):
"""Example of increasing a counter."""
self.increase_counter('request', 'path')
self.set_status(204)