2016-01-21 19:23:04 +00:00
|
|
|
import signal
|
|
|
|
|
2016-03-10 20:45:50 +00:00
|
|
|
from sprockets.mixins.metrics import influxdb
|
2016-01-29 14:25:50 +00:00
|
|
|
from tornado import concurrent, gen, ioloop, web
|
2016-01-21 19:23:04 +00:00
|
|
|
|
|
|
|
|
2016-03-10 20:45:50 +00:00
|
|
|
class SimpleHandler(influxdb.InfluxDBMixin, web.RequestHandler):
|
2016-01-21 19:23:04 +00:00
|
|
|
"""
|
|
|
|
Simply emits a few metrics around the GET method.
|
|
|
|
|
|
|
|
The ``InfluxDBMixin`` sends all of the metrics gathered during
|
|
|
|
the processing of a request as a single measurement when the
|
|
|
|
request is finished. Each request of this sample will result
|
|
|
|
in a single measurement using the service name as the key.
|
|
|
|
|
|
|
|
The following tag keys are defined by default:
|
|
|
|
|
|
|
|
handler="examples.influxdb.SimpleHandler"
|
|
|
|
host="$HOSTNAME"
|
|
|
|
method="GET"
|
|
|
|
|
|
|
|
and the following values are written:
|
|
|
|
|
|
|
|
duration=0.2573668956756592
|
|
|
|
sleepytime=0.255108118057251
|
|
|
|
slept=42
|
|
|
|
status_code=204
|
|
|
|
|
|
|
|
The duration and status_code values are handled by the mix-in
|
|
|
|
and the slept and sleepytime values are added in the method.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
2016-03-21 15:15:05 +00:00
|
|
|
def initialize(self):
|
|
|
|
super(SimpleHandler, self).initialize()
|
|
|
|
self.set_metric_tag('environment', 'testing')
|
|
|
|
|
2016-01-27 15:28:30 +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:28:30 +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-21 19:23:04 +00:00
|
|
|
@gen.coroutine
|
|
|
|
def get(self):
|
|
|
|
with self.execution_timer('sleepytime'):
|
|
|
|
yield gen.sleep(0.25)
|
|
|
|
self.increase_counter('slept', amount=42)
|
|
|
|
self.set_status(204)
|
|
|
|
self.finish()
|
|
|
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
Measurements will be sent to the ``testing`` database on the
|
|
|
|
configured InfluxDB instance. The measurement name is set
|
|
|
|
by the ``service`` setting.
|
|
|
|
|
|
|
|
"""
|
|
|
|
settings = {
|
2016-03-10 20:45:50 +00:00
|
|
|
influxdb.SETTINGS_KEY: {
|
|
|
|
'measurement': 'example',
|
2016-01-21 19:23:04 +00:00
|
|
|
}
|
|
|
|
}
|
2016-03-10 20:45:50 +00:00
|
|
|
application = web.Application([web.url('/', SimpleHandler)], **settings)
|
|
|
|
influxdb.install(application, **{'database': 'testing'})
|
|
|
|
return application
|
2016-01-21 19:23:04 +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()
|