def test_export_exemplar(self):
        metric = _create_metric(
            metric_descriptor.MetricDescriptorType.CUMULATIVE_DISTRIBUTION,
            points=[
                point.Point(value=_create_distribution_value(
                    bounds=[1],
                    buckets=[
                        value.Bucket(count=1,
                                     exemplar=value.Exemplar(
                                         value=2.5,
                                         timestamp=TEST_TIME_STR,
                                         attachments={'key1': 'value1'})),
                        value.Bucket(count=0),
                    ]),
                            timestamp=datetime.now())
            ])

        handler = mock.Mock(spec=ocagent.ExportRpcHandler)
        ocagent.StatsExporter(handler).export_metrics([metric])

        self.assertEqual(
            handler.send.call_args[0][0].metrics[0].timeseries[0].points[0].
            distribution_value.buckets[0].exemplar,
            metrics_pb2.DistributionValue.Exemplar(
                value=2.5,
                timestamp=timestamp_pb2.Timestamp(seconds=1545699723,
                                                  nanos=4000),
                attachments={'key1': 'value1'}))
    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)
Exemplo n.º 3
0
 def setUp(self):
     self.exemplar = value_module.Exemplar(EX_VALUE, EX_TIMESTAMP,
                                           EX_ATTACHMENTS)
Exemplo n.º 4
0
 def test_init(self):
     exemplar = value_module.Exemplar(EX_VALUE, EX_TIMESTAMP,
                                      EX_ATTACHMENTS)
     self.assertEqual(exemplar.value, EX_VALUE)
     self.assertEqual(exemplar.timestamp, EX_TIMESTAMP)
     self.assertEqual(exemplar.attachments, EX_ATTACHMENTS)