Provide an interface to store metrics

Internally, if we want to re-use ironic-prometheus-exporter
*and* get useful metrics data out through it for ironic itself,
we need we need someway to collect and return metrics data.

Turns out, we did most of this for statsd ages ago, and we can
just reuse the framework.

Change-Id: I6060da4ab80c2e6d19d78b808216ae262edcc84c
This commit is contained in:
Julia Kreger 2022-11-22 08:53:52 -08:00
parent c0ae15fb9c
commit 71e06086b0
6 changed files with 216 additions and 1 deletions

View File

@ -194,3 +194,7 @@ class KeystoneUnauthorized(IronicException):
class KeystoneFailure(IronicException):
pass
class MetricsNotSupported(IronicException):
_msg_fmt = _("Metrics action is not supported.")

View File

@ -19,6 +19,7 @@ import random
import time
from ironic_lib.common.i18n import _
from ironic_lib import exception
class Timer(object):
@ -289,6 +290,10 @@ class MetricLogger(object, metaclass=abc.ABCMeta):
def _timer(self, name, value):
"""Abstract method for backends to implement timer behavior."""
def get_metrics_data(self):
"""Return the metrics collection, if available."""
raise exception.MetricsNotSupported()
class NoopMetricLogger(MetricLogger):
"""Noop metric logger that throws away all metric data."""

View File

@ -0,0 +1,120 @@
# 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.
from oslo_concurrency import lockutils
from oslo_config import cfg
from ironic_lib import metrics
CONF = cfg.CONF
STATISTIC_DATA = {}
class DictCollectionMetricLogger(metrics.MetricLogger):
"""Metric logger that collects internal counters."""
# These are internal typing labels in Ironic-lib.
GAUGE_TYPE = 'g'
COUNTER_TYPE = 'c'
TIMER_TYPE = 'ms'
def __init__(self, prefix, delimiter='.'):
"""Initialize the Collection Metrics Logger.
The logger stores metrics data in a dictionary which can then be
retrieved by the program utilizing it whenever needed utilizing a
get_metrics_data call to return the metrics data structure.
:param prefix: Prefix for this metric logger.
:param delimiter: Delimiter used to generate the full metric name.
"""
super(DictCollectionMetricLogger, self).__init__(
prefix, delimiter=delimiter)
@lockutils.synchronized('statistics-update')
def _send(self, name, value, metric_type, sample_rate=None):
"""Send the metrics to be stored in memory.
This memory updates the internal dictionary to facilitate
collection of statistics, and the retrieval of them for
consumers or plugins in Ironic to retrieve the statistic
data utilizing the `get_metrics_data` method.
:param name: Metric name
:param value: Metric value
:param metric_type: Metric type (GAUGE_TYPE, COUNTER_TYPE),
TIMER_TYPE is not supported.
:param sample_rate: Not Applicable.
"""
global STATISTIC_DATA
if metric_type == self.TIMER_TYPE:
if name in STATISTIC_DATA:
STATISTIC_DATA[name] = {
'count': STATISTIC_DATA[name]['count'] + 1,
'sum': STATISTIC_DATA[name]['sum'] + value,
'type': 'timer'
}
else:
# Set initial data value.
STATISTIC_DATA[name] = {
'count': 1,
'sum': value,
'type': 'timer'
}
elif metric_type == self.GAUGE_TYPE:
STATISTIC_DATA[name] = {
'value': value,
'type': 'gauge'
}
elif metric_type == self.COUNTER_TYPE:
if name in STATISTIC_DATA:
# NOTE(TheJulia): Value is hard coded for counter
# data types as a value of 1.
STATISTIC_DATA[name] = {
'count': STATISTIC_DATA[name]['count'] + 1,
'type': 'counter'
}
else:
STATISTIC_DATA[name] = {
'count': 1,
'type': 'counter'
}
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 get_metrics_data(self):
"""Return the metrics collection dictionary.
:returns: Dictonary containing the keys and values of
data stored via the metrics collection hooks.
The values themselves are dictionaries which
contain a type field, indicating if the statistic
is a counter, gauge, or timer. A counter has a
`count` field, a gauge value has a `value` field,
and a 'timer' fiend las a 'count' and 'sum' fields.
The multiple fields for for a timer type allows
for additional statistics to be implied from the
data once collected and compared over time.
"""
return STATISTIC_DATA

View File

@ -18,12 +18,18 @@ from oslo_config import cfg
from ironic_lib.common.i18n import _
from ironic_lib import exception
from ironic_lib import metrics
from ironic_lib import metrics_collector
from ironic_lib import metrics_statsd
metrics_opts = [
cfg.StrOpt('backend',
default='noop',
choices=['noop', 'statsd'],
choices=[
('noop', 'Do nothing in relation to metrics.'),
('statsd', 'Transmits metrics data to a statsd backend.'),
('collector', 'Collects metrics data and saves it in '
'memory for use by the running application.'),
],
help='Backend to use for the metrics system.'),
cfg.BoolOpt('prepend_host',
default=False,
@ -87,6 +93,9 @@ def get_metrics_logger(prefix='', backend=None, host=None, delimiter='.'):
return metrics_statsd.StatsdMetricLogger(prefix, delimiter=delimiter)
elif backend == 'noop':
return metrics.NoopMetricLogger(prefix, delimiter=delimiter)
elif backend == 'collector':
return metrics_collector.DictCollectionMetricLogger(
prefix, delimiter=delimiter)
else:
msg = (_("The backend is set to an unsupported type: "
"%s. Value should be 'noop' or 'statsd'.")

View File

@ -0,0 +1,68 @@
# 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.
from unittest import mock
from ironic_lib import metrics_collector
from ironic_lib.tests import base
def connect(family=None, type=None, proto=None):
"""Dummy function to provide signature for autospec"""
pass
class TestDictCollectionMetricLogger(base.IronicLibTestCase):
def setUp(self):
super(TestDictCollectionMetricLogger, self).setUp()
self.ml = metrics_collector.DictCollectionMetricLogger(
'prefix', '.')
@mock.patch('ironic_lib.metrics_collector.'
'DictCollectionMetricLogger._send',
autospec=True)
def test_gauge(self, mock_send):
self.ml._gauge('metric', 10)
mock_send.assert_called_once_with(self.ml, 'metric', 10, 'g')
@mock.patch('ironic_lib.metrics_collector.'
'DictCollectionMetricLogger._send',
autospec=True)
def test_counter(self, mock_send):
self.ml._counter('metric', 10)
mock_send.assert_called_once_with(self.ml, 'metric', 10, 'c',
sample_rate=None)
@mock.patch('ironic_lib.metrics_collector.'
'DictCollectionMetricLogger._send',
autospec=True)
def test_timer(self, mock_send):
self.ml._timer('metric', 10)
mock_send.assert_called_once_with(self.ml, 'metric', 10, 'ms')
def test_send(self):
expected = {
'part1.part1': {'count': 2, 'type': 'counter'},
'part1.part2': {'type': 'gauge', 'value': 66},
'part1.magic': {'count': 2, 'sum': 22, 'type': 'timer'},
}
self.ml._send('part1.part1', 1, 'c')
self.ml._send('part1.part1', 1, 'c')
self.ml._send('part1.part2', 66, 'g')
self.ml._send('part1.magic', 2, 'ms')
self.ml._send('part1.magic', 20, 'ms')
results = self.ml.get_metrics_data()
self.assertEqual(expected, results)

View File

@ -0,0 +1,9 @@
---
features:
- |
Adds a new metrics collection backend, ``collector``, to collect
counter, gauge, and timer information, enabling the applicationg to
access these statistics during process runtime. Adds a new metrics method
``get_metrics_data`` to allow the dictionary structure containing
the metrics data to be accessed. This feature may be enabled by setting
the ``[metrics]\backend`` option to ``collector``.