def test_observer_to_envelope_value_none(self): aggregator = ValueObserverAggregator() aggregator.update(None) aggregator.take_checkpoint() record = MetricRecord(self._test_obs, self._test_labels, aggregator) 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") self.assertEqual(envelope.time, ns_to_iso_str(aggregator.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"])
def test_checkpoint(self): observer = ValueObserverAggregator() # take checkpoint wihtout any update observer.take_checkpoint() self.assertEqual(observer.checkpoint, (inf, -inf, 0, 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]), )
def test_live_metric_envelope_observer(self): aggregator = ValueObserverAggregator() aggregator.update(123) aggregator.take_checkpoint() record = MetricRecord(self._test_obs, self._test_labels, aggregator) exporter = LiveMetricsExporter( instrumentation_key=self._instrumentation_key, span_processor=self._span_processor, ) envelope = exporter._metric_to_live_metrics_envelope([record]) self.assertIsInstance(envelope, LiveMetricEnvelope) self.assertEqual( envelope.instrumentation_key, "12345678-1234-5678-abcd-12345678abcd", ) self.assertEqual(envelope.documents, []) self.assertEqual(envelope.metrics[0].name, "testname") self.assertEqual(envelope.metrics[0].value, 123) self.assertEqual(envelope.metrics[0].weight, 1)