InfluxDBMixin: Move call to super.__init__.

`tornado.web.RequestHandler.__init__` calls
`tornado.web.RequestHandler.initialize` to initialize the object.
Sub-classes should be able to call our methods to tag metrics in
their `initialize` implementations.  For this to work we need to
set our internal attributes *BEFORE* we call `super.__init__`.
This commit is contained in:
Dave Shawley 2016-03-21 11:15:05 -04:00
parent c11ba26790
commit 471adf5cb7
3 changed files with 15 additions and 1 deletions

View file

@ -3,6 +3,12 @@
Release History
===============
`Next Release`_
---------------
- Make it possible to call methods (e.g.,
:meth:`~sprockets.mixins.metrics.influxdb.InfluxDBMixin.set_metric_tag`)
during the Tornado request handler initialization phase.
`2.0.0`_ (11-Mar-2016)
----------------------
- Rework InfluxDB buffering to use a periodic callback instead of flushing

View file

@ -31,6 +31,10 @@ class SimpleHandler(influxdb.InfluxDBMixin, web.RequestHandler):
"""
def initialize(self):
super(SimpleHandler, self).initialize()
self.set_metric_tag('environment', 'testing')
@gen.coroutine
def prepare(self):
maybe_future = super(SimpleHandler, self).prepare()

View file

@ -20,7 +20,6 @@ class InfluxDBMixin(object):
"""Mix this class in to record measurements to a InfluxDB server."""
def __init__(self, application, request, **kwargs):
super(InfluxDBMixin, self).__init__(application, request, **kwargs)
self.__metrics = []
self.__tags = {
'handler': '{}.{}'.format(self.__module__,
@ -28,6 +27,11 @@ class InfluxDBMixin(object):
'method': request.method,
}
# Call to super().__init__() needs to be *AFTER* we create our
# properties since it calls initialize() which may want to call
# methods like ``set_metric_tag``
super(InfluxDBMixin, self).__init__(application, request, **kwargs)
def set_metric_tag(self, tag, value):
"""
Add a tag to the measurement key.