def test_value_recorder_to_envelope(self): aggregator = MinMaxSumCountAggregator() aggregator.update(123) aggregator.take_checkpoint() record = MetricRecord(self._test_value_recorder, 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, 1) 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_concurrent_update(self): mmsc = MinMaxSumCountAggregator() with concurrent.futures.ThreadPoolExecutor(max_workers=2) as ex: fut1 = ex.submit(self.call_update, mmsc) fut2 = ex.submit(self.call_update, mmsc) ret1 = fut1.result() ret2 = fut2.result() update_total = MinMaxSumCountAggregator._merge_checkpoint( ret1, ret2) mmsc.take_checkpoint() self.assertEqual(update_total, mmsc.checkpoint)
def test_concurrent_update(self): mmsc0 = MinMaxSumCountAggregator() mmsc1 = MinMaxSumCountAggregator() with concurrent.futures.ThreadPoolExecutor(max_workers=2) as ex: mmsc0.checkpoint = ex.submit(self.call_update, mmsc0).result() mmsc1.checkpoint = ex.submit(self.call_update, mmsc0).result() mmsc0.merge(mmsc1) mmsc0_checkpoint = mmsc0.checkpoint mmsc0.take_checkpoint() self.assertEqual(mmsc0_checkpoint, mmsc0.checkpoint) self.assertIsNot(mmsc0_checkpoint, mmsc0.checkpoint)
def test_concurrent_update_and_checkpoint(self): mmsc = MinMaxSumCountAggregator() checkpoint_total = MinMaxSumCountAggregator._TYPE(2**32, 0, 0, 0) with concurrent.futures.ThreadPoolExecutor(max_workers=1) as ex: fut = ex.submit(self.call_update, mmsc) while not fut.done(): mmsc.take_checkpoint() checkpoint_total = MinMaxSumCountAggregator._merge_checkpoint( checkpoint_total, mmsc.checkpoint) mmsc.take_checkpoint() checkpoint_total = MinMaxSumCountAggregator._merge_checkpoint( checkpoint_total, mmsc.checkpoint) self.assertEqual(checkpoint_total, fut.result())
def test_concurrent_update_and_checkpoint(self): mmsc0 = MinMaxSumCountAggregator() mmsc1 = MinMaxSumCountAggregator() mmsc1.checkpoint = MinMaxSumCountAggregator._TYPE(2**32, 0, 0, 0) with concurrent.futures.ThreadPoolExecutor(max_workers=1) as ex: fut = ex.submit(self.call_update, mmsc0) while not fut.done(): mmsc0.take_checkpoint() mmsc0.merge(mmsc1) mmsc1.checkpoint = mmsc0.checkpoint mmsc0.take_checkpoint() mmsc0.merge(mmsc1) self.assertEqual(mmsc0.checkpoint, fut.result())
def test_min_max_sum_aggregator_to_prometheus(self): meter = get_meter_provider().get_meter(__name__) metric = meter.create_metric("test@name", "testdesc", "unit", int, metrics.ValueRecorder, []) labels = {} key_labels = get_dict_as_key(labels) aggregator = MinMaxSumCountAggregator() aggregator.update(123) aggregator.update(456) aggregator.take_checkpoint() record = MetricRecord(metric, key_labels, aggregator) collector = CustomCollector("testprefix") collector.add_metrics_data([record]) result_bytes = generate_latest(collector) result = result_bytes.decode("utf-8") self.assertIn("testprefix_test_name_count 2.0", result) self.assertIn("testprefix_test_name_sum 579.0", result)
def test_live_metric_envelope_value_recorder(self): aggregator = MinMaxSumCountAggregator() aggregator.update(123) aggregator.take_checkpoint() record = MetricRecord(self._test_metric2, 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.documents, []) self.assertEqual(envelope.metrics[0].name, "testname") self.assertEqual(envelope.metrics[0].value, 1) self.assertEqual(envelope.metrics[0].weight, 1)
def test_checkpoint(self): mmsc = MinMaxSumCountAggregator() # take checkpoint wihtout any update mmsc.take_checkpoint() self.assertEqual(mmsc.checkpoint, MinMaxSumCountAggregator._EMPTY) # call update with some values values = (3, 50, 3, 97) for val in values: mmsc.update(val) mmsc.take_checkpoint() self.assertEqual( mmsc.checkpoint, (min(values), max(values), sum(values), len(values)), ) self.assertEqual(mmsc.current, MinMaxSumCountAggregator._EMPTY)