2021-03-09 20:06:23 +00:00
|
|
|
import contextlib
|
2021-04-24 13:15:50 +00:00
|
|
|
import logging
|
2021-03-09 20:06:23 +00:00
|
|
|
import os
|
2021-03-21 21:45:23 +00:00
|
|
|
import socket
|
2021-03-09 20:06:23 +00:00
|
|
|
import time
|
2021-03-26 10:40:27 +00:00
|
|
|
import typing
|
2021-03-09 20:06:23 +00:00
|
|
|
|
2021-04-24 13:15:50 +00:00
|
|
|
from tornado import ioloop, web
|
2021-03-09 20:06:23 +00:00
|
|
|
|
|
|
|
from sprockets_statsd import statsd
|
|
|
|
|
|
|
|
|
|
|
|
class Application(web.Application):
|
|
|
|
"""Mix this into your application to add a statsd connection.
|
|
|
|
|
2021-03-23 11:43:20 +00:00
|
|
|
.. attribute:: statsd_connector
|
2021-04-12 12:08:07 +00:00
|
|
|
:type: sprockets_statsd.statsd.AbstractConnector
|
2021-03-23 11:43:20 +00:00
|
|
|
|
|
|
|
Connection to the StatsD server that is set between calls
|
|
|
|
to :meth:`.start_statsd` and :meth:`.stop_statsd`.
|
|
|
|
|
2021-03-09 20:06:23 +00:00
|
|
|
This mix-in is configured by the ``statsd`` settings key. The
|
2021-03-23 11:08:34 +00:00
|
|
|
value is a dictionary with the following keys.
|
2021-03-09 20:06:23 +00:00
|
|
|
|
2021-04-12 12:08:07 +00:00
|
|
|
+-------------------+---------------------------------------------+
|
|
|
|
| enabled | should the statsd connector be enabled? |
|
2021-03-11 12:31:24 +00:00
|
|
|
+-------------------+---------------------------------------------+
|
|
|
|
| host | the statsd host to send metrics to |
|
|
|
|
+-------------------+---------------------------------------------+
|
2021-03-23 11:08:34 +00:00
|
|
|
| port | port number that statsd is listening on |
|
2021-03-11 12:31:24 +00:00
|
|
|
+-------------------+---------------------------------------------+
|
2021-03-24 02:01:27 +00:00
|
|
|
| prefix | segment to prefix to metrics |
|
2021-03-11 12:31:24 +00:00
|
|
|
+-------------------+---------------------------------------------+
|
2021-03-21 21:45:23 +00:00
|
|
|
| protocol | "tcp" or "udp" |
|
|
|
|
+-------------------+---------------------------------------------+
|
2021-03-11 12:31:24 +00:00
|
|
|
| reconnect_timeout | number of seconds to sleep after a statsd |
|
|
|
|
| | connection attempt fails |
|
|
|
|
+-------------------+---------------------------------------------+
|
|
|
|
| wait_timeout | number of seconds to wait for a metric to |
|
|
|
|
| | arrive on the queue before verifying the |
|
|
|
|
| | connection |
|
|
|
|
+-------------------+---------------------------------------------+
|
|
|
|
|
2021-04-12 12:08:07 +00:00
|
|
|
**enabled** defaults to the :envvar:`STATSD_ENABLED` environment
|
|
|
|
variable coerced to a :class:`bool`. If this variable is not set,
|
|
|
|
then the statsd connector *WILL BE* enabled. Set this to a *falsy*
|
|
|
|
value to disable the connector. The following values are considered
|
|
|
|
*truthy*: a non-zero integer or a case-insensitive match of "on",
|
|
|
|
"t", "true", or "yes". All other values are considered *falsy*.
|
|
|
|
|
2021-03-23 11:08:34 +00:00
|
|
|
**host** defaults to the :envvar:`STATSD_HOST` environment variable.
|
|
|
|
If this value is not set, then the statsd connector *WILL NOT* be
|
|
|
|
enabled.
|
|
|
|
|
|
|
|
**port** defaults to the :envvar:`STATSD_PORT` environment variable
|
|
|
|
with a back up default of 8125 if the environment variable is not
|
|
|
|
set.
|
|
|
|
|
|
|
|
**prefix** is prefixed to all metric paths. This provides a
|
|
|
|
namespace for metrics so that each applications metrics are maintained
|
2021-03-24 02:01:27 +00:00
|
|
|
in separate buckets. The default is to use the :envvar:`STATSD_PREFIX`
|
|
|
|
environment variable. If it is unset and the *service* and
|
|
|
|
*environment* keys are set in ``settings``, then the default is
|
|
|
|
``applications.<service>.<environment>``. This is a convenient way to
|
|
|
|
maintain consistent metric paths when you are managing a larger number
|
|
|
|
of services.
|
2021-03-23 11:08:34 +00:00
|
|
|
|
2021-03-24 02:01:27 +00:00
|
|
|
.. rubric:: Warning
|
|
|
|
|
2021-03-24 12:10:07 +00:00
|
|
|
If you want to run without a prefix, then you are required to explicitly
|
|
|
|
set ``statsd.prefix`` to ``None``. This prevents accidentally polluting
|
|
|
|
the metric namespace with unqualified paths.
|
2021-03-09 20:06:23 +00:00
|
|
|
|
2021-03-21 21:45:23 +00:00
|
|
|
**protocol** defaults to the :envvar:`STATSD_PROTOCOL` environment
|
|
|
|
variable with a back default of "tcp" if the environment variable
|
|
|
|
is not set.
|
|
|
|
|
2021-03-23 11:08:34 +00:00
|
|
|
**reconnect_timeout** defaults to 1.0 seconds which limits the
|
2021-03-11 12:31:24 +00:00
|
|
|
aggressiveness of creating new TCP connections.
|
|
|
|
|
2021-03-23 11:08:34 +00:00
|
|
|
**wait_timeout** defaults to 0.1 seconds which ensures that the
|
2021-03-11 12:31:24 +00:00
|
|
|
processor quickly responds to connection faults.
|
|
|
|
|
2021-03-09 20:06:23 +00:00
|
|
|
"""
|
2021-04-12 12:08:07 +00:00
|
|
|
statsd_connector: typing.Optional[statsd.AbstractConnector]
|
2021-03-26 10:40:27 +00:00
|
|
|
|
|
|
|
def __init__(self, *args: typing.Any, **settings: typing.Any):
|
2021-03-09 20:06:23 +00:00
|
|
|
statsd_settings = settings.setdefault('statsd', {})
|
2021-04-12 12:08:07 +00:00
|
|
|
statsd_settings.setdefault('enabled',
|
|
|
|
os.environ.get('STATSD_ENABLED', 'yes'))
|
2021-03-09 20:06:23 +00:00
|
|
|
statsd_settings.setdefault('host', os.environ.get('STATSD_HOST'))
|
|
|
|
statsd_settings.setdefault('port',
|
|
|
|
os.environ.get('STATSD_PORT', '8125'))
|
2021-03-21 21:45:23 +00:00
|
|
|
statsd_settings.setdefault('protocol',
|
|
|
|
os.environ.get('STATSD_PROTOCOL', 'tcp'))
|
2021-03-09 20:06:23 +00:00
|
|
|
|
2021-03-24 12:10:07 +00:00
|
|
|
if 'prefix' not in statsd_settings:
|
|
|
|
statsd_settings['prefix'] = os.environ.get('STATSD_PREFIX')
|
|
|
|
if not statsd_settings['prefix']:
|
|
|
|
try:
|
|
|
|
statsd_settings['prefix'] = '.'.join([
|
|
|
|
'applications',
|
|
|
|
settings['service'],
|
|
|
|
settings['environment'],
|
|
|
|
])
|
|
|
|
except KeyError:
|
|
|
|
raise RuntimeError(
|
|
|
|
'statsd configuration error: prefix is not set. Set'
|
|
|
|
' $STATSD_PREFIX or configure settings.statsd.prefix')
|
2021-03-09 20:06:23 +00:00
|
|
|
|
|
|
|
super().__init__(*args, **settings)
|
|
|
|
|
2021-04-12 12:08:07 +00:00
|
|
|
self.settings['statsd']['enabled'] = _parse_bool(
|
|
|
|
self.settings['statsd']['enabled'])
|
2021-03-09 20:06:23 +00:00
|
|
|
self.settings['statsd']['port'] = int(self.settings['statsd']['port'])
|
2021-03-23 11:43:20 +00:00
|
|
|
self.statsd_connector = None
|
2021-03-09 20:06:23 +00:00
|
|
|
|
2021-04-24 13:15:50 +00:00
|
|
|
try:
|
|
|
|
self.on_start_callbacks.append(self.start_statsd)
|
|
|
|
self.on_shutdown_callbacks.append(self.stop_statsd)
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
async def start_statsd(self, *_) -> None:
|
2021-03-09 20:06:23 +00:00
|
|
|
"""Start the connector during startup.
|
|
|
|
|
|
|
|
Call this method during application startup to enable the statsd
|
|
|
|
connection. A new :class:`~sprockets_statsd.statsd.Connector`
|
|
|
|
instance will be created and started. This method does not return
|
|
|
|
until the connector is running.
|
|
|
|
|
|
|
|
"""
|
2021-03-23 11:43:20 +00:00
|
|
|
if self.statsd_connector is None:
|
2021-04-24 13:15:50 +00:00
|
|
|
logger = self.__get_logger()
|
2021-04-12 12:08:07 +00:00
|
|
|
if not self.settings['statsd']['enabled']:
|
2021-04-24 13:15:50 +00:00
|
|
|
logger.info('statsd connector is disabled by configuration')
|
2021-04-12 12:08:07 +00:00
|
|
|
self.statsd_connector = statsd.AbstractConnector()
|
2021-03-21 21:45:23 +00:00
|
|
|
else:
|
2021-04-12 12:08:07 +00:00
|
|
|
kwargs = self.settings['statsd'].copy()
|
|
|
|
kwargs.pop('enabled', None) # consume this one
|
|
|
|
protocol = kwargs.pop('protocol', None)
|
|
|
|
if protocol == 'tcp':
|
|
|
|
kwargs['ip_protocol'] = socket.IPPROTO_TCP
|
|
|
|
elif protocol == 'udp':
|
|
|
|
kwargs['ip_protocol'] = socket.IPPROTO_UDP
|
|
|
|
else:
|
2021-04-24 13:15:50 +00:00
|
|
|
return self.__handle_fatal_error(
|
2021-04-12 12:08:07 +00:00
|
|
|
f'statsd configuration error: {protocol} is not '
|
|
|
|
f'a valid protocol')
|
2021-04-24 13:15:50 +00:00
|
|
|
|
|
|
|
logger.info('creating %s statsd connector', protocol.upper())
|
|
|
|
try:
|
|
|
|
self.statsd_connector = statsd.Connector(**kwargs)
|
|
|
|
except RuntimeError as error:
|
|
|
|
return self.__handle_fatal_error(
|
|
|
|
'statsd.Connector failed to start', error)
|
2021-03-23 11:43:20 +00:00
|
|
|
|
|
|
|
await self.statsd_connector.start()
|
2021-03-09 20:06:23 +00:00
|
|
|
|
2021-04-24 13:15:50 +00:00
|
|
|
async def stop_statsd(self, *_) -> None:
|
2021-03-09 20:06:23 +00:00
|
|
|
"""Stop the connector during shutdown.
|
|
|
|
|
|
|
|
If the connector was started, then this method will gracefully
|
|
|
|
terminate it. The method does not return until after the
|
|
|
|
connector is stopped.
|
|
|
|
|
|
|
|
"""
|
2021-03-23 11:43:20 +00:00
|
|
|
if self.statsd_connector is not None:
|
|
|
|
await self.statsd_connector.stop()
|
|
|
|
self.statsd_connector = None
|
2021-03-09 20:06:23 +00:00
|
|
|
|
2021-04-24 13:15:50 +00:00
|
|
|
def __handle_fatal_error(self,
|
|
|
|
message: str,
|
|
|
|
exc: typing.Optional[Exception] = None):
|
|
|
|
logger = self.__get_logger()
|
|
|
|
if exc is not None:
|
|
|
|
logger.exception('%s', message)
|
|
|
|
else:
|
|
|
|
logger.error('%s', message)
|
|
|
|
if hasattr(self, 'stop'):
|
|
|
|
self.stop(ioloop.IOLoop.current())
|
|
|
|
else:
|
|
|
|
raise RuntimeError(message)
|
|
|
|
|
|
|
|
def __get_logger(self) -> logging.Logger:
|
|
|
|
try:
|
|
|
|
return getattr(self, 'logger')
|
|
|
|
except AttributeError:
|
|
|
|
return logging.getLogger(__package__).getChild(
|
|
|
|
'tornado.Application')
|
|
|
|
|
2021-03-09 20:06:23 +00:00
|
|
|
|
|
|
|
class RequestHandler(web.RequestHandler):
|
|
|
|
"""Mix this into your handler to send metrics to a statsd server."""
|
2021-03-26 10:40:27 +00:00
|
|
|
statsd_connector: typing.Optional[statsd.Connector]
|
2021-03-09 20:06:23 +00:00
|
|
|
|
2021-03-26 10:40:27 +00:00
|
|
|
def initialize(self, **kwargs: typing.Any) -> None:
|
2021-03-09 20:06:23 +00:00
|
|
|
super().initialize(**kwargs)
|
2021-03-23 11:43:20 +00:00
|
|
|
self.application: Application
|
|
|
|
self.statsd_connector = self.application.statsd_connector
|
2021-03-09 20:06:23 +00:00
|
|
|
|
2021-03-26 10:40:27 +00:00
|
|
|
def __build_path(self, *path: typing.Any) -> str:
|
2021-03-30 11:56:20 +00:00
|
|
|
return '.'.join(str(c) for c in path)
|
2021-03-09 20:06:23 +00:00
|
|
|
|
2021-03-26 10:40:27 +00:00
|
|
|
def record_timing(self, secs: float, *path: typing.Any) -> None:
|
2021-03-09 20:06:23 +00:00
|
|
|
"""Record the duration.
|
|
|
|
|
|
|
|
:param secs: number of seconds to record
|
|
|
|
:param path: path to record the duration under
|
|
|
|
|
|
|
|
"""
|
2021-03-23 11:43:20 +00:00
|
|
|
if self.statsd_connector is not None:
|
2021-03-30 11:56:20 +00:00
|
|
|
self.statsd_connector.timing(self.__build_path(*path), secs)
|
2021-03-09 20:06:23 +00:00
|
|
|
|
2021-03-26 10:40:27 +00:00
|
|
|
def increase_counter(self, *path: typing.Any, amount: int = 1) -> None:
|
2021-03-09 20:06:23 +00:00
|
|
|
"""Adjust a counter.
|
|
|
|
|
|
|
|
:param path: path of the counter to adjust
|
|
|
|
:param amount: amount to adjust the counter by. Defaults to
|
|
|
|
1 and can be negative
|
|
|
|
|
|
|
|
"""
|
2021-03-23 11:43:20 +00:00
|
|
|
if self.statsd_connector is not None:
|
2021-03-30 11:56:20 +00:00
|
|
|
self.statsd_connector.incr(self.__build_path(*path), amount)
|
2021-03-09 20:06:23 +00:00
|
|
|
|
|
|
|
@contextlib.contextmanager
|
2021-03-26 10:40:27 +00:00
|
|
|
def execution_timer(
|
|
|
|
self, *path: typing.Any) -> typing.Generator[None, None, None]:
|
2021-03-09 20:06:23 +00:00
|
|
|
"""Record the execution duration of a block of code.
|
|
|
|
|
|
|
|
:param path: path to record the duration as
|
|
|
|
|
|
|
|
"""
|
|
|
|
start = time.time()
|
|
|
|
try:
|
|
|
|
yield
|
|
|
|
finally:
|
|
|
|
self.record_timing(time.time() - start, *path)
|
|
|
|
|
2021-03-26 10:40:27 +00:00
|
|
|
def on_finish(self) -> None:
|
2021-03-09 20:06:23 +00:00
|
|
|
"""Extended to record the request time as a duration.
|
|
|
|
|
|
|
|
This method extends :meth:`tornado.web.RequestHandler.on_finish`
|
|
|
|
to record ``self.request.request_time`` as a timing metric.
|
|
|
|
|
|
|
|
"""
|
|
|
|
super().on_finish()
|
|
|
|
self.record_timing(self.request.request_time(),
|
|
|
|
self.__class__.__name__, self.request.method,
|
|
|
|
self.get_status())
|
2021-04-12 12:08:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _parse_bool(value: str) -> bool:
|
|
|
|
try:
|
|
|
|
return int(value) != 0
|
|
|
|
except ValueError:
|
|
|
|
return value.lower() in {'true', 't', 'yes', 'on'}
|