Skip to content

Commit

Permalink
refactor to avoid duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
loomis-relativity committed Jun 26, 2024
1 parent 57b0a61 commit f4cfaa7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}

Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit f4cfaa7

Please sign in to comment.