コード例 #1
0
    def to_point(self, timestamp):
        """Get a Point conversion of this aggregation.

        This method creates a :class: `opencensus.metrics.export.point.Point`
        with a :class: `opencensus.metrics.export.value.ValueDistribution`
        value, and creates buckets and exemplars for that distribution from the
        appropriate classes in the `metrics` package.

        :type timestamp: :class: `datetime.datetime`
        :param timestamp: The time to report the point as having been recorded.

        :rtype: :class: `opencensus.metrics.export.point.Point`
        :return: a :class: `opencensus.metrics.export.value.ValueDistribution`
        -valued Point.
        """
        buckets = [None] * len(self.counts_per_bucket)
        for ii, count in enumerate(self.counts_per_bucket):
            stat_ex = self.exemplars.get(ii, None)
            if stat_ex is not None:
                metric_ex = value.Exemplar(stat_ex.value, stat_ex.timestamp,
                                           copy.copy(stat_ex.attachments))
                buckets[ii] = value.Bucket(count, metric_ex)
            else:
                buckets[ii] = value.Bucket(count)

        bucket_options = value.BucketOptions(value.Explicit(self.bounds))
        return point.Point(
            value.ValueDistribution(
                count=self.count_data,
                sum_=self.sum,
                sum_of_squared_deviation=self.sum_of_sqd_deviations,
                bucket_options=bucket_options,
                buckets=buckets), timestamp)
コード例 #2
0
 def test_init_no_histogram(self):
     distribution = value_module.ValueDistribution(
         VD_COUNT, VD_SUM, VD_SUM_OF_SQUARED_DEVIATION,
         value_module.BucketOptions(), None)
     self.assertEqual(distribution.count, VD_COUNT)
     self.assertEqual(distribution.sum, VD_SUM)
     self.assertEqual(distribution.sum_of_squared_deviation,
                      VD_SUM_OF_SQUARED_DEVIATION)
     self.assertIsNone(distribution.bucket_options.type_)
     self.assertEqual(distribution.buckets, None)
コード例 #3
0
def _create_distribution_value(count=1,
                               sum_=0,
                               sum_of_squared_deviation=0,
                               bounds=[],
                               buckets=[]):
    return value.ValueDistribution(
        count=count,
        sum_=sum_,
        sum_of_squared_deviation=sum_of_squared_deviation,
        bucket_options=value.BucketOptions(type_=value.Explicit(
            bounds=bounds)),
        buckets=buckets)
コード例 #4
0
    def setUp(self):
        self.double_value = value_module.ValueDouble(55.5)
        self.long_value = value_module.ValueLong(9876543210)
        self.timestamp = '2018-10-06T17:57:57.936475Z'

        value_at_percentile = [summary_module.ValueAtPercentile(99.5, 10.2)]
        snapshot = summary_module.Snapshot(10, 87.07, value_at_percentile)
        self.summary = summary_module.Summary(10, 6.6, snapshot)
        self.summary_value = value_module.ValueSummary(self.summary)
        self.distribution_value = value_module.ValueDistribution(
            100,
            1000.0,
            10.0,
            value_module.BucketOptions(
                value_module.Explicit(list(range(1, 10)))),
            [value_module.Bucket(10, None) for ii in range(10)],
        )
コード例 #5
0
    def test_init_bad_args(self):
        value_module.ValueDistribution(VD_COUNT, VD_SUM,
                                       VD_SUM_OF_SQUARED_DEVIATION,
                                       BUCKET_OPTIONS, BUCKETS)

        with self.assertRaises(ValueError):
            value_module.ValueDistribution(-1, VD_SUM,
                                           VD_SUM_OF_SQUARED_DEVIATION,
                                           BUCKET_OPTIONS, BUCKETS)

        with self.assertRaises(ValueError):
            value_module.ValueDistribution(0, VD_SUM,
                                           VD_SUM_OF_SQUARED_DEVIATION,
                                           BUCKET_OPTIONS, BUCKETS)

        with self.assertRaises(ValueError):
            value_module.ValueDistribution(0, 0, VD_SUM_OF_SQUARED_DEVIATION,
                                           BUCKET_OPTIONS, BUCKETS)

        with self.assertRaises(ValueError):
            value_module.ValueDistribution(
                VD_COUNT, VD_SUM, VD_SUM_OF_SQUARED_DEVIATION, None, BUCKETS)

        with self.assertRaises(ValueError):
            value_module.ValueDistribution(0, 0, 0, BUCKET_OPTIONS, BUCKETS)

        with self.assertRaises(ValueError):
            value_module.ValueDistribution(VD_COUNT - 1, VD_SUM,
                                           VD_SUM_OF_SQUARED_DEVIATION,
                                           BUCKET_OPTIONS, BUCKETS)

        with self.assertRaises(ValueError):
            value_module.ValueDistribution(VD_COUNT, VD_SUM,
                                           VD_SUM_OF_SQUARED_DEVIATION,
                                           value_module.BucketOptions(),
                                           BUCKETS)

        with self.assertRaises(ValueError):
            value_module.ValueDistribution(VD_COUNT, VD_SUM,
                                           VD_SUM_OF_SQUARED_DEVIATION,
                                           BUCKET_OPTIONS, BUCKETS[:-1])
コード例 #6
0
        value_at_percentile = [summary_module.ValueAtPercentile(99.5, 10.2)]
        snapshot = summary_module.Snapshot(10, 87.07, value_at_percentile)
        summary = summary_module.Summary(10, 6.6, snapshot)

        summary_value = value_module.Value.summary_value(summary)

        self.assertIsNotNone(summary_value)
        self.assertIsInstance(summary_value, value_module.ValueSummary)
        self.assertEqual(summary_value.value, summary)


VD_COUNT = 100
VD_SUM = 1000.0
VD_SUM_OF_SQUARED_DEVIATION = 10.0
BOUNDS = list(range(1, 10))
BUCKET_OPTIONS = value_module.BucketOptions(value_module.Explicit(BOUNDS))
BUCKETS = [value_module.Bucket(10, None) for ii in range(10)]


class TestValueDistribution(unittest.TestCase):
    def test_init(self):
        distribution = value_module.ValueDistribution(
            VD_COUNT, VD_SUM, VD_SUM_OF_SQUARED_DEVIATION, BUCKET_OPTIONS,
            BUCKETS)
        self.assertEqual(distribution.count, VD_COUNT)
        self.assertEqual(distribution.sum, VD_SUM)
        self.assertEqual(distribution.sum_of_squared_deviation,
                         VD_SUM_OF_SQUARED_DEVIATION)
        self.assertEqual(distribution.bucket_options, BUCKET_OPTIONS)
        self.assertEqual(distribution.bucket_options.type_.bounds, BOUNDS)
        self.assertEqual(distribution.buckets, BUCKETS)