This file is indexed.

/usr/lib/python3/dist-packages/ironic_lib/metrics_statsd.py is in python3-ironic-lib 2.12.0-0ubuntu1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
# Copyright 2016 Rackspace Hosting
# All Rights Reserved.
#
#    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.

import contextlib
import socket

from oslo_config import cfg
from oslo_log import log

from ironic_lib import metrics

statsd_opts = [
    cfg.StrOpt('statsd_host',
               default='localhost',
               help='Host for use with the statsd backend.'),
    cfg.PortOpt('statsd_port',
                default=8125,
                help='Port to use with the statsd backend.')
]

CONF = cfg.CONF
CONF.register_opts(statsd_opts, group='metrics_statsd')

LOG = log.getLogger(__name__)


class StatsdMetricLogger(metrics.MetricLogger):
    """Metric logger that reports data via the statsd protocol."""

    GAUGE_TYPE = 'g'
    COUNTER_TYPE = 'c'
    TIMER_TYPE = 'ms'

    def __init__(self, prefix, delimiter='.', host=None, port=None):
        """Initialize a StatsdMetricLogger

        The logger uses the given prefix list, delimiter, host, and port.

        :param prefix: Prefix for this metric logger.
        :param delimiter: Delimiter used to generate the full metric name.
        :param host: The statsd host
        :param port: The statsd port
        """
        super(StatsdMetricLogger, self).__init__(prefix,
                                                 delimiter=delimiter)

        self._host = host or CONF.metrics_statsd.statsd_host
        self._port = port or CONF.metrics_statsd.statsd_port

        self._target = (self._host, self._port)

    def _send(self, name, value, metric_type, sample_rate=None):
        """Send metrics to the statsd backend

        :param name: Metric name
        :param value: Metric value
        :param metric_type: Metric type (GAUGE_TYPE, COUNTER_TYPE,
            or TIMER_TYPE)
        :param sample_rate: Probabilistic rate at which the values will be sent
        """
        if sample_rate is None:
            metric = '%s:%s|%s' % (name, value, metric_type)
        else:
            metric = '%s:%s|%s@%s' % (name, value, metric_type, sample_rate)

        # Ideally, we'd cache a sending socket in self, but that
        # results in a socket getting shared by multiple green threads.
        with contextlib.closing(self._open_socket()) as sock:
            try:
                sock.settimeout(0.0)
                sock.sendto(metric, self._target)
            except socket.error as e:
                LOG.warning("Failed to send the metric value to host "
                            "%(host)s, port %(port)s. Error: %(error)s",
                            {'host': self._host, 'port': self._port,
                             'error': e})

    def _open_socket(self):
        return socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

    def _gauge(self, name, value):
        return self._send(name, value, self.GAUGE_TYPE)

    def _counter(self, name, value, sample_rate=None):
        return self._send(name, value, self.COUNTER_TYPE,
                          sample_rate=sample_rate)

    def _timer(self, name, value):
        return self._send(name, value, self.TIMER_TYPE)


def list_opts():
    """Entry point for oslo-config-generator."""
    return [('metrics_statsd', statsd_opts)]