aodh/ceilometer/transformer/conversions.py
Ubuntu f8ccabfcc5 Remove replace/preserve logic from rate of change transformer
The idea is to not re-emit cpu samples from the cpu_pipeline
by default, but instead simplify the pipeline.yaml by allowing
these original samples to be emitted by the general-purpose
pipeline as before, thus avoiding unintended double-publication.

Now only the derived samples would be emitted from the
scaling or rate of change transformers.

Change-Id: I12623181d289fc0ee3cdb6fcdbacf8e76e53d244
2013-07-23 15:26:50 +01:00

146 lines
5.5 KiB
Python

# -*- encoding: utf-8 -*-
#
# Copyright © 2013 Red Hat, Inc
#
# Author: Eoghan Glynn <eglynn@redhat.com>
#
# 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 collections import defaultdict
from ceilometer import counter as ceilocounter
from ceilometer.openstack.common import log
from ceilometer.openstack.common import timeutils
from ceilometer import transformer
LOG = log.getLogger(__name__)
class Namespace(object):
"""Encapsulates the namespace wrapping the evaluation of the
configured scale factor. This allows nested dicts to be
accessed in the attribute style, and missing attributes
to yield false when used in a boolean expression.
"""
def __init__(self, seed):
self.__dict__ = defaultdict(lambda: Namespace({}))
self.__dict__.update(seed)
for k, v in self.__dict__.iteritems():
if isinstance(v, dict):
self.__dict__[k] = Namespace(v)
def __getattr__(self, attr):
return self.__dict__[attr]
def __getitem__(self, key):
return self.__dict__[key]
def __nonzero__(self):
return len(self.__dict__) > 0
class ScalingTransformer(transformer.TransformerBase):
"""Transformer to apply a scaling conversion.
"""
def __init__(self, source={}, target={}, **kwargs):
"""Initialize transformer with configured parameters.
:param source: dict containing source counter unit
:param target: dict containing target counter name, type,
unit and scaling factor (a missing value
connotes no change)
"""
self.source = source
self.target = target
LOG.debug(_('scaling conversion transformer with source:'
' %(source)s target: %(target)s:') % locals())
super(ScalingTransformer, self).__init__(**kwargs)
@staticmethod
def _scale(counter, scale):
"""Apply the scaling factor (either a straight multiplicative
factor or else a string to be eval'd).
"""
ns = Namespace(counter._asdict())
return ((eval(scale, {}, ns) if isinstance(scale, basestring)
else counter.volume * scale) if scale else counter.volume)
def _convert(self, counter, growth=1):
"""Transform the appropriate counter fields.
"""
scale = self.target.get('scale')
return ceilocounter.Counter(
name=self.target.get('name', counter.name),
unit=self.target.get('unit', counter.unit),
type=self.target.get('type', counter.type),
volume=self._scale(counter, scale) * growth,
user_id=counter.user_id,
project_id=counter.project_id,
resource_id=counter.resource_id,
timestamp=counter.timestamp,
resource_metadata=counter.resource_metadata
)
def handle_sample(self, context, counter, source):
"""Handle a sample, converting if necessary."""
LOG.debug('handling counter %s', (counter,))
if (self.source.get('unit', counter.unit) == counter.unit):
counter = self._convert(counter)
LOG.debug(_('converted to: %s') % (counter,))
return counter
class RateOfChangeTransformer(ScalingTransformer):
"""Transformer based on the rate of change of a counter volume,
for example taking the current and previous volumes of a
cumulative counter and producing a gauge value based on the
proportion of some maximum used.
"""
def __init__(self, **kwargs):
"""Initialize transformer with configured parameters.
"""
self.cache = {}
super(RateOfChangeTransformer, self).__init__(**kwargs)
def handle_sample(self, context, counter, source):
"""Handle a sample, converting if necessary."""
LOG.debug('handling counter %s', (counter,))
key = counter.name + counter.resource_id
prev = self.cache.get(key)
timestamp = timeutils.parse_isotime(counter.timestamp)
self.cache[key] = (counter.volume, timestamp)
if prev:
prev_volume = prev[0]
prev_timestamp = prev[1]
time_delta = timeutils.delta_seconds(prev_timestamp, timestamp)
# we only allow negative deltas for noncumulative counters, whereas
# for cumulative we assume that a reset has occurred in the interim
# so that the current volume gives a lower bound on growth
volume_delta = (counter.volume - prev_volume
if (prev_volume <= counter.volume or
counter.type != ceilocounter.TYPE_CUMULATIVE)
else counter.volume)
rate_of_change = ((1.0 * volume_delta / time_delta)
if time_delta else 0.0)
counter = self._convert(counter, rate_of_change)
LOG.debug(_('converted to: %s') % (counter,))
else:
LOG.warn(_('dropping counter with no predecessor: %s') % counter)
counter = None
return counter