39
39
)
40
40
from opentelemetry .sdk ._metrics ._internal .point import PointT , Sum
41
41
from opentelemetry .util ._time import _time_ns
42
+ from opentelemetry .util .types import Attributes
42
43
43
44
_PointVarT = TypeVar ("_PointVarT" , bound = PointT )
44
45
@@ -58,8 +59,9 @@ class AggregationTemporality(IntEnum):
58
59
59
60
60
61
class _Aggregation (ABC , Generic [_PointVarT ]):
61
- def __init__ (self ):
62
+ def __init__ (self , attributes : Attributes ):
62
63
self ._lock = Lock ()
64
+ self ._attributes = attributes
63
65
64
66
@abstractmethod
65
67
def aggregate (self , measurement : Measurement ) -> None :
@@ -84,7 +86,9 @@ class Aggregation(ABC):
84
86
"""
85
87
86
88
@abstractmethod
87
- def _create_aggregation (self , instrument : Instrument ) -> _Aggregation :
89
+ def _create_aggregation (
90
+ self , instrument : Instrument , attributes : Attributes
91
+ ) -> _Aggregation :
88
92
"""Creates an aggregation"""
89
93
90
94
@@ -107,48 +111,55 @@ class DefaultAggregation(Aggregation):
107
111
==================================================== ====================================
108
112
"""
109
113
110
- def _create_aggregation (self , instrument : Instrument ) -> _Aggregation :
114
+ def _create_aggregation (
115
+ self , instrument : Instrument , attributes : Attributes
116
+ ) -> _Aggregation :
111
117
112
118
# pylint: disable=too-many-return-statements
113
119
if isinstance (instrument , Counter ):
114
120
return _SumAggregation (
121
+ attributes ,
115
122
instrument_is_monotonic = True ,
116
123
instrument_temporality = AggregationTemporality .DELTA ,
117
124
)
118
125
if isinstance (instrument , UpDownCounter ):
119
126
return _SumAggregation (
127
+ attributes ,
120
128
instrument_is_monotonic = False ,
121
129
instrument_temporality = AggregationTemporality .DELTA ,
122
130
)
123
131
124
132
if isinstance (instrument , ObservableCounter ):
125
133
return _SumAggregation (
134
+ attributes ,
126
135
instrument_is_monotonic = True ,
127
136
instrument_temporality = AggregationTemporality .CUMULATIVE ,
128
137
)
129
138
130
139
if isinstance (instrument , ObservableUpDownCounter ):
131
140
return _SumAggregation (
141
+ attributes ,
132
142
instrument_is_monotonic = False ,
133
143
instrument_temporality = AggregationTemporality .CUMULATIVE ,
134
144
)
135
145
136
146
if isinstance (instrument , Histogram ):
137
- return _ExplicitBucketHistogramAggregation ()
147
+ return _ExplicitBucketHistogramAggregation (attributes )
138
148
139
149
if isinstance (instrument , ObservableGauge ):
140
- return _LastValueAggregation ()
150
+ return _LastValueAggregation (attributes )
141
151
142
152
raise Exception (f"Invalid instrument type { type (instrument )} found" )
143
153
144
154
145
155
class _SumAggregation (_Aggregation [Sum ]):
146
156
def __init__ (
147
157
self ,
158
+ attributes : Attributes ,
148
159
instrument_is_monotonic : bool ,
149
160
instrument_temporality : AggregationTemporality ,
150
161
):
151
- super ().__init__ ()
162
+ super ().__init__ (attributes )
152
163
153
164
self ._start_time_unix_nano = _time_ns ()
154
165
self ._instrument_temporality = instrument_temporality
@@ -205,8 +216,8 @@ def collect(self) -> Optional[Sum]:
205
216
206
217
207
218
class _LastValueAggregation (_Aggregation [Gauge ]):
208
- def __init__ (self ):
209
- super ().__init__ ()
219
+ def __init__ (self , attributes : Attributes ):
220
+ super ().__init__ (attributes )
210
221
self ._value = None
211
222
212
223
def aggregate (self , measurement : Measurement ):
@@ -232,6 +243,7 @@ def collect(self) -> Optional[Gauge]:
232
243
class _ExplicitBucketHistogramAggregation (_Aggregation [HistogramPoint ]):
233
244
def __init__ (
234
245
self ,
246
+ attributes : Attributes ,
235
247
boundaries : Sequence [float ] = (
236
248
0.0 ,
237
249
5.0 ,
@@ -246,7 +258,7 @@ def __init__(
246
258
),
247
259
record_min_max : bool = True ,
248
260
):
249
- super ().__init__ ()
261
+ super ().__init__ (attributes )
250
262
self ._boundaries = tuple (boundaries )
251
263
self ._bucket_counts = self ._get_empty_bucket_counts ()
252
264
self ._min = inf
@@ -464,10 +476,13 @@ def __init__(
464
476
self ._boundaries = boundaries
465
477
self ._record_min_max = record_min_max
466
478
467
- def _create_aggregation (self , instrument : Instrument ) -> _Aggregation :
479
+ def _create_aggregation (
480
+ self , instrument : Instrument , attributes : Attributes
481
+ ) -> _Aggregation :
468
482
return _ExplicitBucketHistogramAggregation (
469
- boundaries = self ._boundaries ,
470
- record_min_max = self ._record_min_max ,
483
+ attributes ,
484
+ self ._boundaries ,
485
+ self ._record_min_max ,
471
486
)
472
487
473
488
@@ -477,7 +492,9 @@ class SumAggregation(Aggregation):
477
492
- The arithmetic sum of Measurement values.
478
493
"""
479
494
480
- def _create_aggregation (self , instrument : Instrument ) -> _Aggregation :
495
+ def _create_aggregation (
496
+ self , instrument : Instrument , attributes : Attributes
497
+ ) -> _Aggregation :
481
498
482
499
temporality = AggregationTemporality .UNSPECIFIED
483
500
if isinstance (instrument , Synchronous ):
@@ -486,6 +503,7 @@ def _create_aggregation(self, instrument: Instrument) -> _Aggregation:
486
503
temporality = AggregationTemporality .CUMULATIVE
487
504
488
505
return _SumAggregation (
506
+ attributes ,
489
507
isinstance (instrument , (Counter , ObservableCounter )),
490
508
temporality ,
491
509
)
@@ -499,12 +517,16 @@ class LastValueAggregation(Aggregation):
499
517
- The timestamp of the last Measurement.
500
518
"""
501
519
502
- def _create_aggregation (self , instrument : Instrument ) -> _Aggregation :
503
- return _LastValueAggregation ()
520
+ def _create_aggregation (
521
+ self , instrument : Instrument , attributes : Attributes
522
+ ) -> _Aggregation :
523
+ return _LastValueAggregation (attributes )
504
524
505
525
506
526
class DropAggregation (Aggregation ):
507
527
"""Using this aggregation will make all measurements be ignored."""
508
528
509
- def _create_aggregation (self , instrument : Instrument ) -> _Aggregation :
510
- return _DropAggregation ()
529
+ def _create_aggregation (
530
+ self , instrument : Instrument , attributes : Attributes
531
+ ) -> _Aggregation :
532
+ return _DropAggregation (attributes )
0 commit comments