From cdb2e4398d397a405f7729fea482e07699d23dfc Mon Sep 17 00:00:00 2001 From: Martin Chacon Piza Date: Wed, 18 Apr 2018 13:34:16 +0200 Subject: [PATCH] Make statsd self-monitoring optional Story: 2001873 Task: 14333 Change-Id: I35eaa298f1153e6b71a5bfe33a894adec6b593a3 --- monasca_notification/common/utils.py | 58 +++++++++++++++++++++++++--- monasca_notification/conf/statsd.py | 2 + 2 files changed, 55 insertions(+), 5 deletions(-) diff --git a/monasca_notification/common/utils.py b/monasca_notification/common/utils.py index 58c1984..cf84203 100644 --- a/monasca_notification/common/utils.py +++ b/monasca_notification/common/utils.py @@ -79,9 +79,57 @@ def grab_stored_notification_method(db_repo, notification_id): def get_statsd_client(dimensions=None): local_dims = dimensions.copy() if dimensions else {} local_dims.update(NOTIFICATION_DIMENSIONS) - client = monascastatsd.Client(name='monasca', - host=CONF.statsd.host, - port=CONF.statsd.port, - dimensions=local_dims) - + if CONF.statsd.enable: + LOG.debug("Establishing connection with statsd on {0}:{1}" + .format(CONF.statsd.host, CONF.statsd.port)) + client = monascastatsd.Client(name='monasca', + host=CONF.statsd.host, + port=CONF.statsd.port, + dimensions=local_dims) + else: + LOG.warn("StatsD monitoring disabled. Overriding monascastatsd.Client to use it offline") + client = OfflineClient(name='monasca', + host=CONF.statsd.host, + port=CONF.statsd.port, + dimensions=local_dims) return client + + +class OfflineClient(monascastatsd.Client): + + def _set_connection(self, connection, host, port): + if connection is None: + self.connection = OfflineConnection(host=host, + port=port, + max_buffer_size=self._max_buffer_size) + else: + self.connection = connection + + +class OfflineConnection(monascastatsd.Connection): + + def __init__(self, host='localhost', port=8125, max_buffer_size=50): + """Initialize an Offline Connection object. + + >>> monascastatsd = MonascaStatsd() + + :name: the name for this client. Everything sent by this client + will be prefixed by name + :param host: the host of the MonascaStatsd server. + :param port: the port of the MonascaStatsd server. + :param max_buffer_size: Maximum number of metric to buffer before + sending to the server if sending metrics in batch + """ + self.max_buffer_size = max_buffer_size + self._send = self._send_to_server + self.connect(host, port) + self.encoding = 'utf-8' + + def connect(self, host, port): + """Avoid connecting to the monascastatsd server. + + """ + pass + + def _send_to_server(self, packet): + pass diff --git a/monasca_notification/conf/statsd.py b/monasca_notification/conf/statsd.py index 5d5275c..4b6a366 100644 --- a/monasca_notification/conf/statsd.py +++ b/monasca_notification/conf/statsd.py @@ -24,6 +24,8 @@ statsd_group = cfg.OptGroup('statsd', 'to statsd server launched by monasca-agent.') statsd_opts = [ + cfg.BoolOpt('enable', default=True, + help='Enable or disable self monitoring.'), cfg.HostAddressOpt('host', default=_DEFAULT_HOST, help='IP address of statsd server.'), cfg.PortOpt('port', default=_DEFAULT_PORT, help='Port of statsd server.'),