def test_counter_to_prometheus(self): meter = get_meter_provider().get_meter(__name__) metric = meter.create_metric( "test@name", "testdesc", "unit", int, metrics.Counter, ["environment@", "os"], ) kvp = {"environment@": "staging", "os": "Windows"} label_set = meter.get_label_set(kvp) aggregator = CounterAggregator() aggregator.update(123) aggregator.take_checkpoint() record = MetricRecord(aggregator, label_set, metric) collector = CustomCollector("testprefix") collector.add_metrics_data([record]) for prometheus_metric in collector.collect(): self.assertEqual(type(prometheus_metric), CounterMetricFamily) self.assertEqual(prometheus_metric.name, "testprefix_test_name") self.assertEqual(prometheus_metric.documentation, "testdesc") self.assertTrue(len(prometheus_metric.samples) == 1) self.assertEqual(prometheus_metric.samples[0].value, 123) self.assertTrue(len(prometheus_metric.samples[0].labels) == 2) self.assertEqual( prometheus_metric.samples[0].labels["environment_"], "staging") self.assertEqual(prometheus_metric.samples[0].labels["os"], "Windows")
def test_concurrent_update(self): counter = CounterAggregator() with concurrent.futures.ThreadPoolExecutor(max_workers=2) as executor: fut1 = executor.submit(self.call_update, counter) fut2 = executor.submit(self.call_update, counter) updapte_total = fut1.result() + fut2.result() counter.take_checkpoint() self.assertEqual(updapte_total, counter.checkpoint)
def test_concurrent_update_and_checkpoint(self): counter = CounterAggregator() checkpoint_total = 0 with concurrent.futures.ThreadPoolExecutor(max_workers=1) as executor: fut = executor.submit(self.call_update, counter) while not fut.done(): counter.take_checkpoint() checkpoint_total += counter.checkpoint counter.take_checkpoint() checkpoint_total += counter.checkpoint self.assertEqual(fut.result(), checkpoint_total)
def test_metric_to_envelope(self): aggregator = CounterAggregator() aggregator.update(123) aggregator.take_checkpoint() record = MetricRecord( aggregator, self._test_label_set, self._test_metric ) exporter = AzureMonitorMetricsExporter() 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( record.metric.get_handle( 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.assertEqual(envelope.data.base_data.metrics[0].ns, "testname") self.assertEqual(envelope.data.base_data.metrics[0].name, "testdesc") self.assertEqual(envelope.data.base_data.metrics[0].value, 123) 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): counter = CounterAggregator() counter.update(2.0) counter.take_checkpoint() self.assertEqual(counter.current, 0) self.assertEqual(counter.checkpoint, 2.0)