1515"""OTLP Metrics Exporter"""
1616
1717import logging
18- import os
19- from typing import List , Optional , Sequence , Type , TypeVar , Union
18+ from typing import List , Optional , Sequence , Type , TypeVar
2019
2120from grpc import ChannelCredentials
2221
7170
7271
7372def _get_data_points (
74- export_record : ExportRecord , data_point_class : Type [DataPointT ]
73+ export_record : ExportRecord ,
74+ data_point_class : Type [DataPointT ],
75+ aggregation_temporality : int ,
7576) -> List [DataPointT ]:
7677
7778 if isinstance (export_record .aggregator , SumAggregator ):
@@ -91,16 +92,23 @@ def _get_data_points(
9192 elif isinstance (export_record .aggregator , ValueObserverAggregator ):
9293 value = export_record .aggregator .checkpoint .last
9394
95+ if aggregation_temporality == (
96+ AggregationTemporality .AGGREGATION_TEMPORALITY_CUMULATIVE
97+ ):
98+ start_time_unix_nano = export_record .aggregator .first_timestamp
99+ else :
100+ start_time_unix_nano = (
101+ export_record .aggregator .initial_checkpoint_timestamp
102+ )
103+
94104 return [
95105 data_point_class (
96106 labels = [
97107 StringKeyValue (key = str (label_key ), value = str (label_value ))
98108 for label_key , label_value in export_record .labels
99109 ],
100110 value = value ,
101- start_time_unix_nano = (
102- export_record .aggregator .initial_checkpoint_timestamp
103- ),
111+ start_time_unix_nano = start_time_unix_nano ,
104112 time_unix_nano = (export_record .aggregator .last_update_timestamp ),
105113 )
106114 ]
@@ -215,25 +223,35 @@ def _translate_data(
215223 data_point_class = type_class [value_type ]["data_point_class" ]
216224
217225 if isinstance (export_record .instrument , Counter ):
226+
227+ aggregation_temporality = (
228+ AggregationTemporality .AGGREGATION_TEMPORALITY_CUMULATIVE
229+ )
230+
218231 otlp_metric_data = sum_class (
219232 data_points = _get_data_points (
220- export_record , data_point_class
221- ),
222- aggregation_temporality = (
223- AggregationTemporality .AGGREGATION_TEMPORALITY_DELTA
233+ export_record ,
234+ data_point_class ,
235+ aggregation_temporality ,
224236 ),
237+ aggregation_temporality = aggregation_temporality ,
225238 is_monotonic = True ,
226239 )
227240 argument = type_class [value_type ]["sum" ]["argument" ]
228241
229242 elif isinstance (export_record .instrument , UpDownCounter ):
243+
244+ aggregation_temporality = (
245+ AggregationTemporality .AGGREGATION_TEMPORALITY_CUMULATIVE
246+ )
247+
230248 otlp_metric_data = sum_class (
231249 data_points = _get_data_points (
232- export_record , data_point_class
233- ),
234- aggregation_temporality = (
235- AggregationTemporality .AGGREGATION_TEMPORALITY_DELTA
250+ export_record ,
251+ data_point_class ,
252+ aggregation_temporality ,
236253 ),
254+ aggregation_temporality = aggregation_temporality ,
237255 is_monotonic = False ,
238256 )
239257 argument = type_class [value_type ]["sum" ]["argument" ]
@@ -243,33 +261,45 @@ def _translate_data(
243261 continue
244262
245263 elif isinstance (export_record .instrument , SumObserver ):
264+
265+ aggregation_temporality = (
266+ AggregationTemporality .AGGREGATION_TEMPORALITY_CUMULATIVE
267+ )
268+
246269 otlp_metric_data = sum_class (
247270 data_points = _get_data_points (
248- export_record , data_point_class
249- ),
250- aggregation_temporality = (
251- AggregationTemporality .AGGREGATION_TEMPORALITY_CUMULATIVE
271+ export_record ,
272+ data_point_class ,
273+ aggregation_temporality ,
252274 ),
275+ aggregation_temporality = aggregation_temporality ,
253276 is_monotonic = True ,
254277 )
255278 argument = type_class [value_type ]["sum" ]["argument" ]
256279
257280 elif isinstance (export_record .instrument , UpDownSumObserver ):
281+
282+ aggregation_temporality = (
283+ AggregationTemporality .AGGREGATION_TEMPORALITY_CUMULATIVE
284+ )
285+
258286 otlp_metric_data = sum_class (
259287 data_points = _get_data_points (
260- export_record , data_point_class
261- ),
262- aggregation_temporality = (
263- AggregationTemporality .AGGREGATION_TEMPORALITY_CUMULATIVE
288+ export_record ,
289+ data_point_class ,
290+ aggregation_temporality ,
264291 ),
292+ aggregation_temporality = aggregation_temporality ,
265293 is_monotonic = False ,
266294 )
267295 argument = type_class [value_type ]["sum" ]["argument" ]
268296
269297 elif isinstance (export_record .instrument , (ValueObserver )):
270298 otlp_metric_data = gauge_class (
271299 data_points = _get_data_points (
272- export_record , data_point_class
300+ export_record ,
301+ data_point_class ,
302+ AggregationTemporality .AGGREGATION_TEMPORALITY_DELTA ,
273303 )
274304 )
275305 argument = type_class [value_type ]["gauge" ]["argument" ]
0 commit comments