15
15
"""OTLP Metrics Exporter"""
16
16
17
17
import logging
18
- import os
19
- from typing import List , Optional , Sequence , Type , TypeVar , Union
18
+ from typing import List , Optional , Sequence , Type , TypeVar
20
19
21
20
from grpc import ChannelCredentials
22
21
71
70
72
71
73
72
def _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 ,
75
76
) -> List [DataPointT ]:
76
77
77
78
if isinstance (export_record .aggregator , SumAggregator ):
@@ -91,16 +92,23 @@ def _get_data_points(
91
92
elif isinstance (export_record .aggregator , ValueObserverAggregator ):
92
93
value = export_record .aggregator .checkpoint .last
93
94
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
+
94
104
return [
95
105
data_point_class (
96
106
labels = [
97
107
StringKeyValue (key = str (label_key ), value = str (label_value ))
98
108
for label_key , label_value in export_record .labels
99
109
],
100
110
value = value ,
101
- start_time_unix_nano = (
102
- export_record .aggregator .initial_checkpoint_timestamp
103
- ),
111
+ start_time_unix_nano = start_time_unix_nano ,
104
112
time_unix_nano = (export_record .aggregator .last_update_timestamp ),
105
113
)
106
114
]
@@ -215,25 +223,35 @@ def _translate_data(
215
223
data_point_class = type_class [value_type ]["data_point_class" ]
216
224
217
225
if isinstance (export_record .instrument , Counter ):
226
+
227
+ aggregation_temporality = (
228
+ AggregationTemporality .AGGREGATION_TEMPORALITY_CUMULATIVE
229
+ )
230
+
218
231
otlp_metric_data = sum_class (
219
232
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 ,
224
236
),
237
+ aggregation_temporality = aggregation_temporality ,
225
238
is_monotonic = True ,
226
239
)
227
240
argument = type_class [value_type ]["sum" ]["argument" ]
228
241
229
242
elif isinstance (export_record .instrument , UpDownCounter ):
243
+
244
+ aggregation_temporality = (
245
+ AggregationTemporality .AGGREGATION_TEMPORALITY_CUMULATIVE
246
+ )
247
+
230
248
otlp_metric_data = sum_class (
231
249
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 ,
236
253
),
254
+ aggregation_temporality = aggregation_temporality ,
237
255
is_monotonic = False ,
238
256
)
239
257
argument = type_class [value_type ]["sum" ]["argument" ]
@@ -243,33 +261,45 @@ def _translate_data(
243
261
continue
244
262
245
263
elif isinstance (export_record .instrument , SumObserver ):
264
+
265
+ aggregation_temporality = (
266
+ AggregationTemporality .AGGREGATION_TEMPORALITY_CUMULATIVE
267
+ )
268
+
246
269
otlp_metric_data = sum_class (
247
270
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 ,
252
274
),
275
+ aggregation_temporality = aggregation_temporality ,
253
276
is_monotonic = True ,
254
277
)
255
278
argument = type_class [value_type ]["sum" ]["argument" ]
256
279
257
280
elif isinstance (export_record .instrument , UpDownSumObserver ):
281
+
282
+ aggregation_temporality = (
283
+ AggregationTemporality .AGGREGATION_TEMPORALITY_CUMULATIVE
284
+ )
285
+
258
286
otlp_metric_data = sum_class (
259
287
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 ,
264
291
),
292
+ aggregation_temporality = aggregation_temporality ,
265
293
is_monotonic = False ,
266
294
)
267
295
argument = type_class [value_type ]["sum" ]["argument" ]
268
296
269
297
elif isinstance (export_record .instrument , (ValueObserver )):
270
298
otlp_metric_data = gauge_class (
271
299
data_points = _get_data_points (
272
- export_record , data_point_class
300
+ export_record ,
301
+ data_point_class ,
302
+ AggregationTemporality .AGGREGATION_TEMPORALITY_DELTA ,
273
303
)
274
304
)
275
305
argument = type_class [value_type ]["gauge" ]["argument" ]
0 commit comments