def test_counter_to_prometheus(self): meter = get_meter_provider().get_meter(__name__) metric = meter.create_counter( "test@name", "testdesc", "unit", int, ) labels = {"environment@": "staging", "os": "Windows"} key_labels = get_dict_as_key(labels) aggregator = SumAggregator() aggregator.update(123) aggregator.take_checkpoint() record = ExportRecord(metric, key_labels, aggregator, get_meter_provider().resource) 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_sanitize(self): collector = CustomCollector("testprefix") self.assertEqual( collector._sanitize("1!2@3#4$5%6^7&8*9(0)_-"), "1_2_3_4_5_6_7_8_9_0___", ) self.assertEqual(collector._sanitize(",./?;:[]{}"), "__________") self.assertEqual(collector._sanitize("TestString"), "TestString") self.assertEqual(collector._sanitize("aAbBcC_12_oi"), "aAbBcC_12_oi")
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_invalid_metric(self): meter = get_meter_provider().get_meter(__name__) metric = StubMetric("tesname", "testdesc", "unit", int, meter) labels = {"environment": "staging"} key_labels = get_dict_as_key(labels) record = ExportRecord(metric, key_labels, None, get_meter_provider().resource) collector = CustomCollector("testprefix") collector.add_metrics_data([record]) collector.collect() self.assertLogs("opentelemetry.exporter.prometheus", level="WARNING")