@@ -169,13 +169,25 @@ def _get_aggregation(
169169
170170 return instrument_class_aggregation
171171
172+
172173def 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+
179191class 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 ),
0 commit comments