Skip to content

Commit c3bc31f

Browse files
loomis-relativityxrmx
authored andcommitted
refactor to avoid duplication
1 parent 4a484bb commit c3bc31f

File tree

2 files changed

+19
-15
lines changed

2 files changed

+19
-15
lines changed

exporter/opentelemetry-exporter-otlp-proto-common/src/opentelemetry/exporter/otlp/proto/common/_internal/metrics_encoder/__init__.py

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -169,13 +169,25 @@ def _get_aggregation(
169169

170170
return instrument_class_aggregation
171171

172+
172173
def truncate_trailing_zeros(lst: Sequence[int]) -> Sequence[int]:
173174
if lst:
174175
for i, value in enumerate(reversed(lst)):
175176
if value != 0:
176177
return lst[0:len(lst)-i]
177178
return []
178179

180+
181+
def create_exponential_histogram_buckets(offset, bucket_counts):
182+
buckets = None
183+
if truncated_bucket_counts := truncate_trailing_zeros(bucket_counts):
184+
buckets = pb2.ExponentialHistogramDataPoint.Buckets(
185+
offset=offset,
186+
bucket_counts=truncated_bucket_counts,
187+
)
188+
return buckets
189+
190+
179191
class EncodingException(Exception):
180192
"""
181193
Raised by encode_metrics() when an exception is caught during encoding. Contains the problematic metric so
@@ -313,21 +325,8 @@ def _encode_metric(metric, pb2_metric):
313325
elif isinstance(metric.data, ExponentialHistogramType):
314326
for data_point in metric.data.data_points:
315327

316-
if positive_buckets := truncate_trailing_zeros(data_point.positive.bucket_counts):
317-
positive = pb2.ExponentialHistogramDataPoint.Buckets(
318-
offset=data_point.positive.offset,
319-
bucket_counts=positive_buckets,
320-
)
321-
else:
322-
positive = None
323-
324-
if negative_buckets := truncate_trailing_zeros(data_point.negative.bucket_counts):
325-
negative = pb2.ExponentialHistogramDataPoint.Buckets(
326-
offset=data_point.negative.offset,
327-
bucket_counts=negative_buckets,
328-
)
329-
else:
330-
negative = None
328+
positive = create_exponential_histogram_buckets(data_point.positive.offset, data_point.positive.bucket_counts)
329+
negative = create_exponential_histogram_buckets(data_point.negative.offset, data_point.negative.bucket_counts)
331330

332331
pt = pb2.ExponentialHistogramDataPoint(
333332
attributes=_encode_attributes(data_point.attributes),

exporter/opentelemetry-exporter-otlp-proto-common/tests/test_metrics_encoder.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from opentelemetry.exporter.otlp.proto.common._internal.metrics_encoder import (
1919
EncodingException,
2020
truncate_trailing_zeros,
21+
create_exponential_histogram_buckets,
2122
)
2223
from opentelemetry.exporter.otlp.proto.common.metrics_encoder import (
2324
encode_metrics,
@@ -862,6 +863,10 @@ def test_truncate_trailing_zeros(self):
862863
self.assertEqual([0, 1], truncate_trailing_zeros([0, 1, 0]))
863864
self.assertEqual([1, -1], truncate_trailing_zeros([1, -1, 0, 0]))
864865

866+
def test_create_histogram_buckets(self):
867+
self.assertIsNone([], create_exponential_histogram_buckets(0, [0, 0, 0, 0]))
868+
self.assertIsNotNone([], create_exponential_histogram_buckets(0, [1]))
869+
865870
def test_encode_exponential_histogram(self):
866871
exponential_histogram = Metric(
867872
name="exponential_histogram",

0 commit comments

Comments
 (0)