From f4cfaa71058026db1f86731664659e9b91cd7679 Mon Sep 17 00:00:00 2001 From: Cal Loomis Date: Wed, 26 Jun 2024 09:48:47 -0500 Subject: [PATCH] refactor to avoid duplication --- .../_internal/metrics_encoder/__init__.py | 26 ++++++++----------- .../tests/test_metrics_encoder.py | 5 ++++ 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/exporter/opentelemetry-exporter-otlp-proto-common/src/opentelemetry/exporter/otlp/proto/common/_internal/metrics_encoder/__init__.py b/exporter/opentelemetry-exporter-otlp-proto-common/src/opentelemetry/exporter/otlp/proto/common/_internal/metrics_encoder/__init__.py index 3d6cc77a47c..a6a87b8632c 100644 --- a/exporter/opentelemetry-exporter-otlp-proto-common/src/opentelemetry/exporter/otlp/proto/common/_internal/metrics_encoder/__init__.py +++ b/exporter/opentelemetry-exporter-otlp-proto-common/src/opentelemetry/exporter/otlp/proto/common/_internal/metrics_encoder/__init__.py @@ -179,6 +179,15 @@ def truncate_trailing_zeros(lst: Sequence[int]) -> Sequence[int]: return lst[0:len(lst)-i] return [] +def create_exponential_histogram_buckets(offset, bucket_counts): + buckets = None + if truncated_bucket_counts := truncate_trailing_zeros(bucket_counts): + buckets = pb2.ExponentialHistogramDataPoint.Buckets( + offset=offset, + bucket_counts=truncated_bucket_counts, + ) + return buckets + def encode_metrics(data: MetricsData) -> ExportMetricsServiceRequest: resource_metrics_dict = {} @@ -278,21 +287,8 @@ def encode_metrics(data: MetricsData) -> ExportMetricsServiceRequest: elif isinstance(metric.data, ExponentialHistogramType): for data_point in metric.data.data_points: - if positive_buckets := truncate_trailing_zeros(data_point.positive.bucket_counts): - positive = pb2.ExponentialHistogramDataPoint.Buckets( - offset=data_point.positive.offset, - bucket_counts=positive_buckets, - ) - else: - positive = None - - if negative_buckets := truncate_trailing_zeros(data_point.negative.bucket_counts): - negative = pb2.ExponentialHistogramDataPoint.Buckets( - offset=data_point.negative.offset, - bucket_counts=negative_buckets, - ) - else: - negative = None + positive = create_exponential_histogram_buckets(data_point.positive.offset, data_point.positive.bucket_counts) + negative = create_exponential_histogram_buckets(data_point.negative.offset, data_point.negative.bucket_counts) pt = pb2.ExponentialHistogramDataPoint( attributes=_encode_attributes( diff --git a/exporter/opentelemetry-exporter-otlp-proto-common/tests/test_metrics_encoder.py b/exporter/opentelemetry-exporter-otlp-proto-common/tests/test_metrics_encoder.py index 941fe28b821..0c54ea797a8 100644 --- a/exporter/opentelemetry-exporter-otlp-proto-common/tests/test_metrics_encoder.py +++ b/exporter/opentelemetry-exporter-otlp-proto-common/tests/test_metrics_encoder.py @@ -17,6 +17,7 @@ from opentelemetry.exporter.otlp.proto.common._internal.metrics_encoder import ( truncate_trailing_zeros, + create_exponential_histogram_buckets, ) from opentelemetry.exporter.otlp.proto.common.metrics_encoder import ( encode_metrics, @@ -704,6 +705,10 @@ def test_truncate_trailing_zeros(self): self.assertEqual([0, 1], truncate_trailing_zeros([0, 1, 0])) self.assertEqual([1, -1], truncate_trailing_zeros([1, -1, 0, 0])) + def test_create_histogram_buckets(self): + self.assertIsNone([], create_exponential_histogram_buckets(0, [0, 0, 0, 0])) + self.assertIsNotNone([], create_exponential_histogram_buckets(0, [1])) + def test_encode_exponential_histogram(self): exponential_histogram = Metric( name="exponential_histogram",