Skip to content

Commit fde6521

Browse files
Add test coverage
1 parent ba3c932 commit fde6521

File tree

1 file changed

+155
-0
lines changed

1 file changed

+155
-0
lines changed

exporter/opentelemetry-exporter-otlp-proto-http/tests/metrics/test_otlp_metrics_exporter.py

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
InstrumentationScope,
4040
KeyValue,
4141
)
42+
from opentelemetry.proto.resource.v1.resource_pb2 import Resource as Pb2Resource
4243
from opentelemetry.sdk.environment_variables import (
4344
OTEL_EXPORTER_OTLP_CERTIFICATE,
4445
OTEL_EXPORTER_OTLP_CLIENT_CERTIFICATE,
@@ -92,6 +93,7 @@
9293

9394
# pylint: disable=protected-access
9495
class TestOTLPMetricExporter(TestCase):
96+
# pylint: disable=too-many-public-methods
9597
def setUp(self):
9698
self.metrics = {
9799
"sum_int": MetricsData(
@@ -604,6 +606,159 @@ def test_split_metrics_data_many_resources_scopes_metrics(self):
604606
split_metrics_data,
605607
)
606608

609+
def test_get_split_resource_metrics_pb2_one_of_each(self):
610+
split_resource_metrics = [
611+
{
612+
"resource": Pb2Resource(
613+
attributes=[
614+
KeyValue(key="foo", value={"string_value": "bar"})
615+
],
616+
),
617+
"schema_url": "http://foo-bar",
618+
"scope_metrics": [
619+
{
620+
"scope": InstrumentationScope(name="foo-scope", version="1.0.0"),
621+
"schema_url": "http://foo-baz",
622+
"metrics": [
623+
{
624+
"name": "foo-metric",
625+
"description": "foo-description",
626+
"unit": "foo-unit",
627+
"sum": {
628+
"aggregation_temporality": 1,
629+
"is_monotonic": True,
630+
"data_points": [
631+
pb2.NumberDataPoint(
632+
attributes=[
633+
KeyValue(key="dp_key", value={"string_value": "dp_value"})
634+
],
635+
start_time_unix_nano=12345,
636+
time_unix_nano=12350,
637+
as_double=42.42,
638+
)
639+
],
640+
},
641+
}
642+
],
643+
}
644+
],
645+
}
646+
]
647+
648+
result = OTLPMetricExporter()._get_split_resource_metrics_pb2(split_resource_metrics)
649+
self.assertEqual(len(result), 1)
650+
self.assertIsInstance(result[0], pb2.ResourceMetrics)
651+
self.assertEqual(result[0].schema_url, "http://foo-bar")
652+
self.assertEqual(len(result[0].scope_metrics), 1)
653+
self.assertEqual(result[0].scope_metrics[0].scope.name, "foo-scope")
654+
self.assertEqual(len(result[0].scope_metrics[0].metrics), 1)
655+
self.assertEqual(result[0].scope_metrics[0].metrics[0].name, "foo-metric")
656+
self.assertEqual(result[0].scope_metrics[0].metrics[0].sum.is_monotonic, True)
657+
658+
def test_get_split_resource_metrics_pb2_multiples(self):
659+
split_resource_metrics = [
660+
{
661+
"resource": Pb2Resource(
662+
attributes=[KeyValue(key="foo1", value={"string_value": "bar2"})],
663+
),
664+
"schema_url": "http://foo-bar-1",
665+
"scope_metrics": [
666+
{
667+
"scope": InstrumentationScope(name="foo-scope-1", version="1.0.0"),
668+
"schema_url": "http://foo-baz-1",
669+
"metrics": [
670+
{
671+
"name": "foo-metric-1",
672+
"description": "foo-description-1",
673+
"unit": "foo-unit-1",
674+
"gauge": {
675+
"data_points": [
676+
pb2.NumberDataPoint(
677+
attributes=[
678+
KeyValue(key="dp_key", value={"string_value": "dp_value"})
679+
],
680+
start_time_unix_nano=12345,
681+
time_unix_nano=12350,
682+
as_double=42.42,
683+
)
684+
],
685+
},
686+
}
687+
],
688+
}
689+
],
690+
},
691+
{
692+
"resource": Pb2Resource(
693+
attributes=[KeyValue(key="foo2", value={"string_value": "bar2"})],
694+
),
695+
"schema_url": "http://foo-bar-2",
696+
"scope_metrics": [
697+
{
698+
"scope": InstrumentationScope(name="foo-scope-2", version="2.0.0"),
699+
"schema_url": "http://foo-baz-2",
700+
"metrics": [
701+
{
702+
"name": "foo-metric-2",
703+
"description": "foo-description-2",
704+
"unit": "foo-unit-2",
705+
"histogram": {
706+
"aggregation_temporality": 2,
707+
"data_points": [
708+
pb2.HistogramDataPoint(
709+
attributes=[KeyValue(key="dp_key", value={"string_value": "dp_value"})],
710+
start_time_unix_nano=12345,
711+
time_unix_nano=12350,
712+
)
713+
],
714+
},
715+
}
716+
],
717+
}
718+
],
719+
},
720+
]
721+
722+
result = OTLPMetricExporter()._get_split_resource_metrics_pb2(split_resource_metrics)
723+
self.assertEqual(len(result), 2)
724+
self.assertEqual(result[0].schema_url, "http://foo-bar-1")
725+
self.assertEqual(result[1].schema_url, "http://foo-bar-2")
726+
self.assertEqual(len(result[0].scope_metrics), 1)
727+
self.assertEqual(len(result[1].scope_metrics), 1)
728+
self.assertEqual(result[0].scope_metrics[0].scope.name, "foo-scope-1")
729+
self.assertEqual(result[1].scope_metrics[0].scope.name, "foo-scope-2")
730+
self.assertEqual(result[0].scope_metrics[0].metrics[0].name, "foo-metric-1")
731+
self.assertEqual(result[1].scope_metrics[0].metrics[0].name, "foo-metric-2")
732+
733+
def test_get_split_resource_metrics_pb2_unsupported_metric_type(self):
734+
split_resource_metrics = [
735+
{
736+
"resource": Pb2Resource(
737+
attributes=[KeyValue(key="foo", value={"string_value": "bar"})],
738+
),
739+
"schema_url": "http://foo-bar",
740+
"scope_metrics": [
741+
{
742+
"scope": InstrumentationScope(name="foo", version="1.0.0"),
743+
"schema_url": "http://foo-baz",
744+
"metrics": [
745+
{
746+
"name": "unsupported-metric",
747+
"description": "foo-bar",
748+
"unit": "foo-bar",
749+
"unsupported_metric_type": {},
750+
}
751+
],
752+
}
753+
],
754+
}
755+
]
756+
757+
with self.assertLogs(level="WARNING") as log:
758+
result = OTLPMetricExporter()._get_split_resource_metrics_pb2(split_resource_metrics)
759+
self.assertEqual(len(result), 1)
760+
self.assertIn("Tried to split and export an unsupported metric type", log.output[0])
761+
607762
@activate
608763
@patch("opentelemetry.exporter.otlp.proto.http.metric_exporter.sleep")
609764
def test_exponential_backoff(self, mock_sleep):

0 commit comments

Comments
 (0)