Repository: open-telemetry/opentelemetry-python

Issue:
OTLP Exporter: Add support for Gauge point types



Discussion:

Resolving patch:
diff --git a/exporter/opentelemetry-exporter-otlp-proto-grpc/src/opentelemetry/exporter/otlp/proto/grpc/_metric_exporter/__init__.py b/exporter/opentelemetry-exporter-otlp-proto-grpc/src/opentelemetry/exporter/otlp/proto/grpc/_metric_exporter/__init__.py
--- a/exporter/opentelemetry-exporter-otlp-proto-grpc/src/opentelemetry/exporter/otlp/proto/grpc/_metric_exporter/__init__.py
+++ b/exporter/opentelemetry-exporter-otlp-proto-grpc/src/opentelemetry/exporter/otlp/proto/grpc/_metric_exporter/__init__.py
@@ -122,10 +122,15 @@ def _translate_data(
                 unit=metric.unit,
             )
             if isinstance(metric.point, Gauge):
-                # TODO: implement gauge
-                pbmetric.gauge = pb2.Gauge(
-                    data_points=[],
+                pt = pb2.NumberDataPoint(
+                    attributes=self._translate_attributes(metric.attributes),
+                    time_unix_nano=metric.point.time_unix_nano,
                 )
+                if isinstance(metric.point.value, int):
+                    pt.as_int = metric.point.value
+                else:
+                    pt.as_double = metric.point.value
+                pbmetric.gauge.data_points.append(pt)
             elif isinstance(metric.point, Histogram):
                 # TODO: implement histogram
                 pbmetric.histogram = pb2.Histogram(


Test patch:
diff --git a/exporter/opentelemetry-exporter-otlp-proto-grpc/tests/metrics/test_otlp_metrics_exporter.py b/exporter/opentelemetry-exporter-otlp-proto-grpc/tests/metrics/test_otlp_metrics_exporter.py
--- a/exporter/opentelemetry-exporter-otlp-proto-grpc/tests/metrics/test_otlp_metrics_exporter.py
+++ b/exporter/opentelemetry-exporter-otlp-proto-grpc/tests/metrics/test_otlp_metrics_exporter.py
@@ -38,21 +38,14 @@
     InstrumentationLibrary,
     KeyValue,
 )
-from opentelemetry.proto.metrics.v1.metrics_pb2 import (
-    InstrumentationLibraryMetrics,
-)
-from opentelemetry.proto.metrics.v1.metrics_pb2 import Metric as OTLPMetric
-from opentelemetry.proto.metrics.v1.metrics_pb2 import (
-    NumberDataPoint as OTLPNumberDataPoint,
-)
-from opentelemetry.proto.metrics.v1.metrics_pb2 import ResourceMetrics
-from opentelemetry.proto.metrics.v1.metrics_pb2 import Sum as OTLPSum
+from opentelemetry.proto.metrics.v1 import metrics_pb2 as pb2
 from opentelemetry.proto.resource.v1.resource_pb2 import (
     Resource as OTLPResource,
 )
 from opentelemetry.sdk._metrics.export import MetricExportResult
 from opentelemetry.sdk._metrics.point import (
     AggregationTemporality,
+    Gauge,
     Metric,
     Sum,
 )
@@ -136,6 +129,16 @@ def _generate_sum(name, val) -> Sum:
     )
 
 
+def _generate_gauge(name, val) -> Gauge:
+    return _generate_metric(
+        name,
+        Gauge(
+            time_unix_nano=1641946016139533244,
+            value=val,
+        ),
+    )
+
+
 class TestOTLPMetricExporter(TestCase):
     def setUp(self):
 
@@ -150,6 +153,8 @@ def setUp(self):
         self.metrics = {
             "sum_int": _generate_sum("sum_int", 33),
             "sum_float": _generate_sum("sum_float", 2.98),
+            "gauge_int": _generate_gauge("gauge_int", 9000),
+            "gauge_float": _generate_gauge("gauge_float", 52.028),
         }
 
     def tearDown(self):
@@ -307,7 +312,7 @@ def test_failure(self):
     def test_translate_sum_int(self):
         expected = ExportMetricsServiceRequest(
             resource_metrics=[
-                ResourceMetrics(
+                pb2.ResourceMetrics(
                     resource=OTLPResource(
                         attributes=[
                             KeyValue(key="a", value=AnyValue(int_value=1)),
@@ -317,18 +322,18 @@ def test_translate_sum_int(self):
                         ]
                     ),
                     instrumentation_library_metrics=[
-                        InstrumentationLibraryMetrics(
+                        pb2.InstrumentationLibraryMetrics(
                             instrumentation_library=InstrumentationLibrary(
                                 name="first_name", version="first_version"
                             ),
                             metrics=[
-                                OTLPMetric(
+                                pb2.Metric(
                                     name="sum_int",
                                     unit="s",
                                     description="foo",
-                                    sum=OTLPSum(
+                                    sum=pb2.Sum(
                                         data_points=[
-                                            OTLPNumberDataPoint(
+                                            pb2.NumberDataPoint(
                                                 attributes=[
                                                     KeyValue(
                                                         key="a",
@@ -365,7 +370,7 @@ def test_translate_sum_int(self):
     def test_translate_sum_float(self):
         expected = ExportMetricsServiceRequest(
             resource_metrics=[
-                ResourceMetrics(
+                pb2.ResourceMetrics(
                     resource=OTLPResource(
                         attributes=[
                             KeyValue(key="a", value=AnyValue(int_value=1)),
@@ -375,18 +380,18 @@ def test_translate_sum_float(self):
                         ]
                     ),
                     instrumentation_library_metrics=[
-                        InstrumentationLibraryMetrics(
+                        pb2.InstrumentationLibraryMetrics(
                             instrumentation_library=InstrumentationLibrary(
                                 name="first_name", version="first_version"
                             ),
                             metrics=[
-                                OTLPMetric(
+                                pb2.Metric(
                                     name="sum_float",
                                     unit="s",
                                     description="foo",
-                                    sum=OTLPSum(
+                                    sum=pb2.Sum(
                                         data_points=[
-                                            OTLPNumberDataPoint(
+                                            pb2.NumberDataPoint(
                                                 attributes=[
                                                     KeyValue(
                                                         key="a",
@@ -419,3 +424,113 @@ def test_translate_sum_float(self):
         # pylint: disable=protected-access
         actual = self.exporter._translate_data([self.metrics["sum_float"]])
         self.assertEqual(expected, actual)
+
+    def test_translate_gauge_int(self):
+        expected = ExportMetricsServiceRequest(
+            resource_metrics=[
+                pb2.ResourceMetrics(
+                    resource=OTLPResource(
+                        attributes=[
+                            KeyValue(key="a", value=AnyValue(int_value=1)),
+                            KeyValue(
+                                key="b", value=AnyValue(bool_value=False)
+                            ),
+                        ]
+                    ),
+                    instrumentation_library_metrics=[
+                        pb2.InstrumentationLibraryMetrics(
+                            instrumentation_library=InstrumentationLibrary(
+                                name="first_name", version="first_version"
+                            ),
+                            metrics=[
+                                pb2.Metric(
+                                    name="gauge_int",
+                                    unit="s",
+                                    description="foo",
+                                    gauge=pb2.Gauge(
+                                        data_points=[
+                                            pb2.NumberDataPoint(
+                                                attributes=[
+                                                    KeyValue(
+                                                        key="a",
+                                                        value=AnyValue(
+                                                            int_value=1
+                                                        ),
+                                                    ),
+                                                    KeyValue(
+                                                        key="b",
+                                                        value=AnyValue(
+                                                            bool_value=True
+                                                        ),
+                                                    ),
+                                                ],
+                                                time_unix_nano=1641946016139533244,
+                                                as_int=9000,
+                                            )
+                                        ],
+                                    ),
+                                )
+                            ],
+                        )
+                    ],
+                )
+            ]
+        )
+        # pylint: disable=protected-access
+        actual = self.exporter._translate_data([self.metrics["gauge_int"]])
+        self.assertEqual(expected, actual)
+
+    def test_translate_gauge_float(self):
+        expected = ExportMetricsServiceRequest(
+            resource_metrics=[
+                pb2.ResourceMetrics(
+                    resource=OTLPResource(
+                        attributes=[
+                            KeyValue(key="a", value=AnyValue(int_value=1)),
+                            KeyValue(
+                                key="b", value=AnyValue(bool_value=False)
+                            ),
+                        ]
+                    ),
+                    instrumentation_library_metrics=[
+                        pb2.InstrumentationLibraryMetrics(
+                            instrumentation_library=InstrumentationLibrary(
+                                name="first_name", version="first_version"
+                            ),
+                            metrics=[
+                                pb2.Metric(
+                                    name="gauge_float",
+                                    unit="s",
+                                    description="foo",
+                                    gauge=pb2.Gauge(
+                                        data_points=[
+                                            pb2.NumberDataPoint(
+                                                attributes=[
+                                                    KeyValue(
+                                                        key="a",
+                                                        value=AnyValue(
+                                                            int_value=1
+                                                        ),
+                                                    ),
+                                                    KeyValue(
+                                                        key="b",
+                                                        value=AnyValue(
+                                                            bool_value=True
+                                                        ),
+                                                    ),
+                                                ],
+                                                time_unix_nano=1641946016139533244,
+                                                as_double=52.028,
+                                            )
+                                        ],
+                                    ),
+                                )
+                            ],
+                        )
+                    ],
+                )
+            ]
+        )
+        # pylint: disable=protected-access
+        actual = self.exporter._translate_data([self.metrics["gauge_float"]])
+        self.assertEqual(expected, actual)