35
35
UpDownCounter ,
36
36
_Gauge ,
37
37
)
38
- from opentelemetry .sdk .metrics ._internal .exemplar import Exemplar , ExemplarReservoirFactory
38
+ from opentelemetry .sdk .metrics ._internal .exemplar import (
39
+ Exemplar ,
40
+ ExemplarReservoirFactory ,
41
+ )
39
42
from opentelemetry .sdk .metrics ._internal .exponential_histogram .buckets import (
40
43
Buckets ,
41
44
)
@@ -82,13 +85,19 @@ class AggregationTemporality(IntEnum):
82
85
83
86
84
87
class _Aggregation (ABC , Generic [_DataPointVarT ]):
85
- def __init__ (self , attributes : Attributes , reservoir_factory : ExemplarReservoirFactory ):
88
+ def __init__ (
89
+ self ,
90
+ attributes : Attributes ,
91
+ reservoir_factory : ExemplarReservoirFactory ,
92
+ ):
86
93
self ._lock = Lock ()
87
94
self ._attributes = attributes
88
95
self ._reservoir = reservoir_factory ()
89
96
self ._previous_point = None
90
97
91
- def aggregate (self , measurement : Measurement , should_sample_exemplar : bool = True ) -> None :
98
+ def aggregate (
99
+ self , measurement : Measurement , should_sample_exemplar : bool = True
100
+ ) -> None :
92
101
if should_sample_exemplar :
93
102
self ._reservoir .offer (
94
103
measurement .value ,
@@ -112,7 +121,9 @@ def _collect_exemplars(self) -> Sequence[Exemplar]:
112
121
113
122
114
123
class _DropAggregation (_Aggregation ):
115
- def aggregate (self , measurement : Measurement , should_sample_exemplar : bool = True ) -> None :
124
+ def aggregate (
125
+ self , measurement : Measurement , should_sample_exemplar : bool = True
126
+ ) -> None :
116
127
pass
117
128
118
129
def collect (
@@ -135,15 +146,19 @@ def __init__(
135
146
super ().__init__ (attributes , reservoir_factory )
136
147
137
148
self ._start_time_unix_nano = start_time_unix_nano
138
- self ._instrument_aggregation_temporality = instrument_aggregation_temporality
149
+ self ._instrument_aggregation_temporality = (
150
+ instrument_aggregation_temporality
151
+ )
139
152
self ._instrument_is_monotonic = instrument_is_monotonic
140
153
141
154
self ._value = None
142
155
143
156
self ._previous_collection_start_nano = self ._start_time_unix_nano
144
157
self ._previous_value = 0
145
158
146
- def aggregate (self , measurement : Measurement , should_sample_exemplar : bool = True ) -> None :
159
+ def aggregate (
160
+ self , measurement : Measurement , should_sample_exemplar : bool = True
161
+ ) -> None :
147
162
with self ._lock :
148
163
if self ._value is None :
149
164
self ._value = 0
@@ -363,14 +378,20 @@ def collect(
363
378
364
379
365
380
class _LastValueAggregation (_Aggregation [GaugePoint ]):
366
- def __init__ (self , attributes : Attributes , reservoir_factory : ExemplarReservoirFactory ):
381
+ def __init__ (
382
+ self ,
383
+ attributes : Attributes ,
384
+ reservoir_factory : ExemplarReservoirFactory ,
385
+ ):
367
386
super ().__init__ (attributes , reservoir_factory )
368
387
self ._value = None
369
388
370
- def aggregate (self , measurement : Measurement , should_sample_exemplar : bool = True ):
389
+ def aggregate (
390
+ self , measurement : Measurement , should_sample_exemplar : bool = True
391
+ ):
371
392
with self ._lock :
372
393
self ._value = measurement .value
373
-
394
+
374
395
super ().aggregate (measurement , should_sample_exemplar )
375
396
376
397
def collect (
@@ -424,7 +445,12 @@ def __init__(
424
445
),
425
446
record_min_max : bool = True ,
426
447
):
427
- super ().__init__ (attributes , reservoir_factory = partial (reservoir_factory , boundaries = boundaries ))
448
+ super ().__init__ (
449
+ attributes ,
450
+ reservoir_factory = partial (
451
+ reservoir_factory , boundaries = boundaries
452
+ ),
453
+ )
428
454
429
455
self ._instrument_aggregation_temporality = (
430
456
instrument_aggregation_temporality
@@ -448,7 +474,9 @@ def __init__(
448
474
def _get_empty_bucket_counts (self ) -> List [int ]:
449
475
return [0 ] * (len (self ._boundaries ) + 1 )
450
476
451
- def aggregate (self , measurement : Measurement , should_sample_exemplar : bool = True ) -> None :
477
+ def aggregate (
478
+ self , measurement : Measurement , should_sample_exemplar : bool = True
479
+ ) -> None :
452
480
453
481
with self ._lock :
454
482
if self ._value is None :
@@ -615,7 +643,12 @@ def __init__(
615
643
# _ExplicitBucketHistogramAggregation both size and amount of buckets
616
644
# remain constant once it is instantiated).
617
645
618
- super ().__init__ (attributes , reservoir_factory = partial (reservoir_factory , size = min (20 , max_size )))
646
+ super ().__init__ (
647
+ attributes ,
648
+ reservoir_factory = partial (
649
+ reservoir_factory , size = min (20 , max_size )
650
+ ),
651
+ )
619
652
620
653
self ._instrument_aggregation_temporality = (
621
654
instrument_aggregation_temporality
@@ -646,7 +679,9 @@ def __init__(
646
679
647
680
self ._mapping = self ._new_mapping (self ._max_scale )
648
681
649
- def aggregate (self , measurement : Measurement , should_sample_exemplar : bool = True ) -> None :
682
+ def aggregate (
683
+ self , measurement : Measurement , should_sample_exemplar : bool = True
684
+ ) -> None :
650
685
# pylint: disable=too-many-branches,too-many-statements, too-many-locals
651
686
652
687
with self ._lock :
@@ -1147,7 +1182,9 @@ def _create_aggregation(
1147
1182
self ,
1148
1183
instrument : Instrument ,
1149
1184
attributes : Attributes ,
1150
- reservoir_factory : Callable [[Type [_Aggregation ]], ExemplarReservoirFactory ],
1185
+ reservoir_factory : Callable [
1186
+ [Type [_Aggregation ]], ExemplarReservoirFactory
1187
+ ],
1151
1188
start_time_unix_nano : int ,
1152
1189
) -> _Aggregation :
1153
1190
"""Creates an aggregation"""
@@ -1176,7 +1213,9 @@ def _create_aggregation(
1176
1213
self ,
1177
1214
instrument : Instrument ,
1178
1215
attributes : Attributes ,
1179
- reservoir_factory : Callable [[Type [_Aggregation ]], ExemplarReservoirFactory ],
1216
+ reservoir_factory : Callable [
1217
+ [Type [_Aggregation ]], ExemplarReservoirFactory
1218
+ ],
1180
1219
start_time_unix_nano : int ,
1181
1220
) -> _Aggregation :
1182
1221
@@ -1227,18 +1266,26 @@ def _create_aggregation(
1227
1266
if isinstance (instrument , Histogram ):
1228
1267
return _ExplicitBucketHistogramAggregation (
1229
1268
attributes ,
1230
- reservoir_factory = reservoir_factory (_ExplicitBucketHistogramAggregation ),
1269
+ reservoir_factory = reservoir_factory (
1270
+ _ExplicitBucketHistogramAggregation
1271
+ ),
1231
1272
instrument_aggregation_temporality = (
1232
1273
AggregationTemporality .DELTA
1233
1274
),
1234
1275
start_time_unix_nano = start_time_unix_nano ,
1235
1276
)
1236
1277
1237
1278
if isinstance (instrument , ObservableGauge ):
1238
- return _LastValueAggregation (attributes , reservoir_factory = reservoir_factory (_LastValueAggregation ))
1279
+ return _LastValueAggregation (
1280
+ attributes ,
1281
+ reservoir_factory = reservoir_factory (_LastValueAggregation ),
1282
+ )
1239
1283
1240
1284
if isinstance (instrument , _Gauge ):
1241
- return _LastValueAggregation (attributes , reservoir_factory = reservoir_factory (_LastValueAggregation ))
1285
+ return _LastValueAggregation (
1286
+ attributes ,
1287
+ reservoir_factory = reservoir_factory (_LastValueAggregation ),
1288
+ )
1242
1289
1243
1290
# pylint: disable=broad-exception-raised
1244
1291
raise Exception (f"Invalid instrument type { type (instrument )} found" )
@@ -1257,7 +1304,9 @@ def _create_aggregation(
1257
1304
self ,
1258
1305
instrument : Instrument ,
1259
1306
attributes : Attributes ,
1260
- reservoir_factory : Callable [[Type [_Aggregation ]], ExemplarReservoirFactory ],
1307
+ reservoir_factory : Callable [
1308
+ [Type [_Aggregation ]], ExemplarReservoirFactory
1309
+ ],
1261
1310
start_time_unix_nano : int ,
1262
1311
) -> _Aggregation :
1263
1312
@@ -1321,7 +1370,9 @@ def _create_aggregation(
1321
1370
self ,
1322
1371
instrument : Instrument ,
1323
1372
attributes : Attributes ,
1324
- reservoir_factory : Callable [[Type [_Aggregation ]], ExemplarReservoirFactory ],
1373
+ reservoir_factory : Callable [
1374
+ [Type [_Aggregation ]], ExemplarReservoirFactory
1375
+ ],
1325
1376
start_time_unix_nano : int ,
1326
1377
) -> _Aggregation :
1327
1378
@@ -1353,7 +1404,9 @@ def _create_aggregation(
1353
1404
self ,
1354
1405
instrument : Instrument ,
1355
1406
attributes : Attributes ,
1356
- reservoir_factory : Callable [[Type [_Aggregation ]], ExemplarReservoirFactory ],
1407
+ reservoir_factory : Callable [
1408
+ [Type [_Aggregation ]], ExemplarReservoirFactory
1409
+ ],
1357
1410
start_time_unix_nano : int ,
1358
1411
) -> _Aggregation :
1359
1412
@@ -1386,10 +1439,15 @@ def _create_aggregation(
1386
1439
self ,
1387
1440
instrument : Instrument ,
1388
1441
attributes : Attributes ,
1389
- reservoir_factory : Callable [[Type [_Aggregation ]], ExemplarReservoirFactory ],
1442
+ reservoir_factory : Callable [
1443
+ [Type [_Aggregation ]], ExemplarReservoirFactory
1444
+ ],
1390
1445
start_time_unix_nano : int ,
1391
1446
) -> _Aggregation :
1392
- return _LastValueAggregation (attributes , reservoir_factory = reservoir_factory (_LastValueAggregation ))
1447
+ return _LastValueAggregation (
1448
+ attributes ,
1449
+ reservoir_factory = reservoir_factory (_LastValueAggregation ),
1450
+ )
1393
1451
1394
1452
1395
1453
class DropAggregation (Aggregation ):
@@ -1399,7 +1457,11 @@ def _create_aggregation(
1399
1457
self ,
1400
1458
instrument : Instrument ,
1401
1459
attributes : Attributes ,
1402
- reservoir_factory : Callable [[Type [_Aggregation ]], ExemplarReservoirFactory ],
1460
+ reservoir_factory : Callable [
1461
+ [Type [_Aggregation ]], ExemplarReservoirFactory
1462
+ ],
1403
1463
start_time_unix_nano : int ,
1404
1464
) -> _Aggregation :
1405
- return _DropAggregation (attributes , reservoir_factory (_DropAggregation ))
1465
+ return _DropAggregation (
1466
+ attributes , reservoir_factory (_DropAggregation )
1467
+ )
0 commit comments