designate.metrics

Source code for designate.metrics

# Copyright 2016 Hewlett Packard Enterprise Development Company LP
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

"""
Monasca-Statsd based metrics
~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Based on metrics-and-stats blueprint

Usage examples:

.. code-block:: python

    from designate.metrics import metrics

    @metrics.timed('dot.separated.name')
    def your_function():
        pass

    with metrics.time('dot.separated.name'):
        pass

    # Increment and decrement a counter.
    metrics.counter(name='foo.bar').increment()
    metrics.counter(name='foo.bar') -= 10

"""

from oslo_log import log as logging
from oslo_utils import importutils

import designate.conf.metrics

stats_client = importutils.import_any('monascastatsd',
                                      'designate.metrics_client.noop')

CFG_GROUP_NAME = 'monasca:statsd'
CONF = designate.conf.CONF
LOG = logging.getLogger(__name__)


# Global metrics client to be imported by other modules
metrics = None


[docs]class Metrics(object):
[docs] def __init__(self): """Initialize Monasca-Statsd client with its default configuration. Do not start sending metrics yet. """ self._client = stats_client.Client(dimensions={ 'service_name': 'dns' }) # cfg.CONF is not available at this time # Buffer all metrics until init() is called # https://bugs.launchpad.net/monasca/+bug/1616060 self._client.connection.open_buffer() self._client.connection.max_buffer_size = 50000
[docs] def init(self): """Setup client connection or disable metrics based on configuration. This is called once the cfg.CONF is ready. """ conf = CONF[CFG_GROUP_NAME] if conf.enabled: LOG.info("Statsd reports to %(host)s %(port)d", { 'host': conf.hostname, 'port': conf.port }) self._client.connection._flush_buffer() self._client.connection.close_buffer() self._client.connection.connect(conf.hostname, conf.port) else: LOG.info("Statsd disabled") # The client cannot be disabled: mock out report() self._client.connection.report = lambda *a, **kw: None
# There's no clean way to drain the outgoing buffer
[docs] def counter(self, *a, **kw): return self._client.get_counter(*a, **kw)
[docs] def gauge(self, *a, **kw): return self._client.get_gauge(*a, **kw)
@property def timed(self): return self._client.get_timer().timed
[docs] def timer(self): return self._client.get_timer()
metrics = Metrics()
Creative Commons Attribution 3.0 License

Except where otherwise noted, this document is licensed under Creative Commons Attribution 3.0 License. See all OpenStack Legal Documents.