2016-01-19 12:50:03 +00:00
|
|
|
import signal
|
|
|
|
|
2016-03-10 20:45:50 +00:00
|
|
|
from sprockets.mixins.metrics import statsd
|
2016-01-29 14:25:50 +00:00
|
|
|
from tornado import concurrent, gen, ioloop, web
|
2016-01-19 12:50:03 +00:00
|
|
|
|
|
|
|
|
2016-03-10 20:45:50 +00:00
|
|
|
class SimpleHandler(statsd.StatsdMixin, web.RequestHandler):
|
2016-01-19 12:50:03 +00:00
|
|
|
"""
|
|
|
|
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.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
2016-01-27 15:46:23 +00:00
|
|
|
@gen.coroutine
|
|
|
|
def prepare(self):
|
|
|
|
maybe_future = super(SimpleHandler, self).prepare()
|
2016-01-29 14:25:50 +00:00
|
|
|
if concurrent.is_future(maybe_future):
|
2016-01-27 15:46:23 +00:00
|
|
|
yield maybe_future
|
|
|
|
|
|
|
|
if 'Correlation-ID' in self.request.headers:
|
|
|
|
self.set_metric_tag('correlation_id',
|
|
|
|
self.request.headers['Correlation-ID'])
|
|
|
|
|
2016-01-19 12:50:03 +00:00
|
|
|
@gen.coroutine
|
|
|
|
def get(self):
|
|
|
|
yield gen.sleep(0.25)
|
|
|
|
self.set_status(204)
|
|
|
|
self.finish()
|
|
|
|
|
2016-01-19 15:43:02 +00:00
|
|
|
def post(self):
|
|
|
|
"""Example of increasing a counter."""
|
|
|
|
self.increase_counter('request', 'path')
|
|
|
|
self.set_status(204)
|
|
|
|
|
|
|
|
|
2016-01-19 12:50:03 +00:00
|
|
|
def _sig_handler(*args_):
|
|
|
|
iol = ioloop.IOLoop.instance()
|
|
|
|
iol.add_callback_from_signal(iol.stop)
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
"""
|
2016-07-18 19:32:59 +00:00
|
|
|
settings = {}
|
|
|
|
application = web.Application([web.url('/', SimpleHandler)], **settings)
|
|
|
|
statsd.install(application, **{'namespace': 'testing'})
|
|
|
|
return application
|
2016-01-19 12:50:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
app = make_application()
|
|
|
|
app.listen(8000)
|
|
|
|
signal.signal(signal.SIGINT, _sig_handler)
|
|
|
|
signal.signal(signal.SIGTERM, _sig_handler)
|
|
|
|
ioloop.IOLoop.instance().start()
|