Exemplo n.º 1
0
    def test_merge(self):
        observer1 = ObserverAggregator()
        observer2 = ObserverAggregator()

        mmsc_checkpoint1 = MinMaxSumCountAggregator._TYPE(3, 150, 101, 3)
        mmsc_checkpoint2 = MinMaxSumCountAggregator._TYPE(1, 33, 44, 2)

        checkpoint1 = ObserverAggregator._TYPE(*(mmsc_checkpoint1 + (23, )))

        checkpoint2 = ObserverAggregator._TYPE(*(mmsc_checkpoint2 + (27, )))

        observer1.mmsc.checkpoint = mmsc_checkpoint1
        observer2.mmsc.checkpoint = mmsc_checkpoint2

        observer1.checkpoint = checkpoint1
        observer2.checkpoint = checkpoint2

        observer1.merge(observer2)

        self.assertEqual(
            observer1.checkpoint,
            (
                min(checkpoint1.min, checkpoint2.min),
                max(checkpoint1.max, checkpoint2.max),
                checkpoint1.sum + checkpoint2.sum,
                checkpoint1.count + checkpoint2.count,
                checkpoint2.last,
            ),
        )
Exemplo n.º 2
0
    def test_update(self):
        observer = ObserverAggregator()
        # test current values without any update
        self.assertEqual(observer.mmsc.current, (None, None, None, 0))
        self.assertIsNone(observer.current)

        # call update with some values
        values = (3, 50, 3, 97, 27)
        for val in values:
            observer.update(val)

        self.assertEqual(
            observer.mmsc.current,
            (min(values), max(values), sum(values), len(values)),
        )

        self.assertEqual(observer.current, values[-1])
Exemplo n.º 3
0
    def test_update(self, time_mock):
        time_mock.return_value = 123
        observer = ObserverAggregator()
        # test current values without any update
        self.assertEqual(observer.mmsc.current, (None, None, None, 0))
        self.assertIsNone(observer.current)

        # call update with some values
        values = (3, 50, 3, 97, 27)
        for val in values:
            observer.update(val)

        self.assertEqual(
            observer.mmsc.current,
            (min(values), max(values), sum(values), len(values)),
        )
        self.assertEqual(observer.last_update_timestamp, 123)

        self.assertEqual(observer.current, values[-1])
Exemplo n.º 4
0
    def test_merge_with_empty(self):
        observer1 = ObserverAggregator()
        observer2 = ObserverAggregator()

        mmsc_checkpoint1 = MinMaxSumCountAggregator._TYPE(3, 150, 101, 3)
        checkpoint1 = ObserverAggregator._TYPE(*(mmsc_checkpoint1 + (23,)))

        observer1.mmsc.checkpoint = mmsc_checkpoint1
        observer1.checkpoint = checkpoint1
        observer1.last_update_timestamp = 100

        observer1.merge(observer2)

        self.assertEqual(observer1.checkpoint, checkpoint1)
Exemplo n.º 5
0
    def aggregator_for(self, metric_type: Type[MetricT]) -> Aggregator:
        """Returns an aggregator based on metric type.

        Aggregators keep track of and updates values when metrics get updated.
        """
        # pylint:disable=R0201
        if issubclass(metric_type, Counter):
            return CounterAggregator()
        if issubclass(metric_type, Measure):
            return MinMaxSumCountAggregator()
        if issubclass(metric_type, Observer):
            return ObserverAggregator()
        # TODO: Add other aggregators
        return CounterAggregator()
Exemplo n.º 6
0
    def test_checkpoint(self):
        observer = ObserverAggregator()

        # take checkpoint wihtout any update
        observer.take_checkpoint()
        self.assertEqual(observer.checkpoint, (None, None, None, 0, None))

        # call update with some values
        values = (3, 50, 3, 97)
        for val in values:
            observer.update(val)

        observer.take_checkpoint()
        self.assertEqual(
            observer.checkpoint,
            (min(values), max(values), sum(values), len(values), values[-1]),
        )
Exemplo n.º 7
0
    def test_observer_to_envelope_value_none(self):
        aggregator = ObserverAggregator()
        aggregator.update(None)
        aggregator.take_checkpoint()
        record = MetricRecord(aggregator, self._test_labels, self._test_obs)
        exporter = self._exporter
        envelope = exporter._metric_to_envelope(record)
        self.assertIsInstance(envelope, Envelope)
        self.assertEqual(envelope.ver, 1)
        self.assertEqual(envelope.name, "Microsoft.ApplicationInsights.Metric")
        # TODO: implement last updated timestamp for observer
        # self.assertEqual(
        #     envelope.time,
        #     ns_to_iso_str(
        #         record.metric.bind(
        #             record.label_set
        #         ).last_update_timestamp
        #     ),
        # )
        self.assertEqual(envelope.sample_rate, None)
        self.assertEqual(envelope.seq, None)
        self.assertEqual(envelope.ikey, "1234abcd-5678-4efa-8abc-1234567890ab")
        self.assertEqual(envelope.flags, None)

        self.assertIsInstance(envelope.data, Data)
        self.assertIsInstance(envelope.data.base_data, MetricData)
        self.assertEqual(envelope.data.base_data.ver, 2)
        self.assertEqual(len(envelope.data.base_data.metrics), 1)
        self.assertIsInstance(envelope.data.base_data.metrics[0], DataPoint)
        self.assertEqual(envelope.data.base_data.metrics[0].ns, "testdesc")
        self.assertEqual(envelope.data.base_data.metrics[0].name, "testname")
        self.assertEqual(envelope.data.base_data.metrics[0].value, 0)
        print(envelope.data.base_data.properties)
        self.assertEqual(
            envelope.data.base_data.properties["environment"], "staging"
        )
        self.assertIsNotNone(envelope.tags["ai.cloud.role"])
        self.assertIsNotNone(envelope.tags["ai.cloud.roleInstance"])
        self.assertIsNotNone(envelope.tags["ai.device.id"])
        self.assertIsNotNone(envelope.tags["ai.device.locale"])
        self.assertIsNotNone(envelope.tags["ai.device.osVersion"])
        self.assertIsNotNone(envelope.tags["ai.device.type"])
        self.assertIsNotNone(envelope.tags["ai.internal.sdkVersion"])