Exemple #1
0
    def get_sample_with_timestamp(self,
                                  metric,
                                  dimensions=None,
                                  device_name=None,
                                  expire=True):
        """Get (timestamp-epoch-style, value).
        """

        # Get the proper dimensions
        key = (tuple(sorted(dimensions.items())), device_name)

        # Never seen this metric
        if metric not in self._sample_store:
            raise exceptions.UnknownValue()

        # Not enough value to compute rate
        elif self.is_counter(metric) and len(
                self._sample_store[metric][key]) < 2:
            raise exceptions.UnknownValue()

        elif self.is_counter(metric) and len(
                self._sample_store[metric][key]) >= 2:
            res = self._rate(self._sample_store[metric][key][-2],
                             self._sample_store[metric][key][-1])
            if expire:
                del self._sample_store[metric][key][:-1]
            return res

        elif self.is_gauge(metric) and len(
                self._sample_store[metric][key]) >= 1:
            return self._sample_store[metric][key][-1]

        else:
            raise exceptions.UnknownValue()
Exemple #2
0
    def _rate(cls, sample1, sample2):
        """Simple rate.
        """
        try:
            rate_interval = sample2[0] - sample1[0]
            if rate_interval == 0:
                raise exceptions.Infinity()

            delta = sample2[1] - sample1[1]
            if delta < 0:
                raise exceptions.UnknownValue()

            return (sample2[0], delta / rate_interval, sample2[2], sample2[3])
        except exceptions.Infinity:
            raise
        except exceptions.UnknownValue:
            raise
        except Exception as e:
            raise exceptions.NaN(e)