From 44984b315dc013ecda74aa3d53c2923c9faaf1b0 Mon Sep 17 00:00:00 2001 From: Alex Boten Date: Mon, 24 Jan 2022 16:14:13 -0800 Subject: [PATCH 1/3] Adding implementation for Gauge Fixes #2385 --- .../proto/grpc/_metric_exporter/__init__.py | 11 +- .../metrics/test_otlp_metrics_exporter.py | 153 +++++++++++++++--- 2 files changed, 142 insertions(+), 22 deletions(-) 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 index 75e5cfbed9c..46cb5d74269 100644 --- 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( 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 index 9cd1f6d18fa..39f4a832222 100644 --- 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) -> Sum: + 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) From 767298de487d0d7a992b250679d0cf8211a994d6 Mon Sep 17 00:00:00 2001 From: Alex Boten Date: Mon, 24 Jan 2022 16:17:19 -0800 Subject: [PATCH 2/3] update changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 80ee736e5e0..c2034e0f4d5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ([#2370](https://github.com/open-telemetry/opentelemetry-python/pull/2370)) - [api] Rename `_DefaultMeter` and `_DefaultMeterProvider` to `NoOpMeter` and `NoOpMeterProvider`. ([#2383](https://github.com/open-telemetry/opentelemetry-python/pull/2383)) +- [exporter/opentelemetry-exporter-otlp-proto-grpc] Add Gauge to OTLPMetricExporter + ([#2408](https://github.com/open-telemetry/opentelemetry-python/pull/2408)) ## [1.8.0-0.27b0](https://github.com/open-telemetry/opentelemetry-python/releases/tag/v1.8.0-0.27b0) - 2021-12-17 From 7bffffec5e14192c27b59adace02ba6189b44cd5 Mon Sep 17 00:00:00 2001 From: Alex Boten Date: Tue, 25 Jan 2022 09:52:39 -0800 Subject: [PATCH 3/3] Update exporter/opentelemetry-exporter-otlp-proto-grpc/tests/metrics/test_otlp_metrics_exporter.py Co-authored-by: Diego Hurtado --- .../tests/metrics/test_otlp_metrics_exporter.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index 39f4a832222..b13fe118823 100644 --- 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 @@ -129,7 +129,7 @@ def _generate_sum(name, val) -> Sum: ) -def _generate_gauge(name, val) -> Sum: +def _generate_gauge(name, val) -> Gauge: return _generate_metric( name, Gauge(