Skip to content

Commit a821311

Browse files
authored
Add attributes to aggregation constructor parameters (#2676)
Fixes #2675
1 parent 87b459f commit a821311

File tree

3 files changed

+75
-47
lines changed

3 files changed

+75
-47
lines changed

opentelemetry-sdk/src/opentelemetry/sdk/_metrics/_internal/_view_instrument_match.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,12 @@ def __init__(
6060
)
6161
if not isinstance(self._view._aggregation, DefaultAggregation):
6262
self._aggregation = self._view._aggregation._create_aggregation(
63-
self._instrument
63+
self._instrument, None
6464
)
6565
else:
6666
self._aggregation = self._instrument_class_aggregation[
6767
self._instrument.__class__
68-
]._create_aggregation(self._instrument)
68+
]._create_aggregation(self._instrument, None)
6969

7070
def conflicts(self, other: "_ViewInstrumentMatch") -> bool:
7171
# pylint: disable=protected-access
@@ -113,13 +113,13 @@ def consume_measurement(self, measurement: Measurement) -> None:
113113
):
114114
aggregation = (
115115
self._view._aggregation._create_aggregation(
116-
self._instrument
116+
self._instrument, attributes
117117
)
118118
)
119119
else:
120120
aggregation = self._instrument_class_aggregation[
121121
self._instrument.__class__
122-
]._create_aggregation(self._instrument)
122+
]._create_aggregation(self._instrument, attributes)
123123
self._attributes_aggregation[attributes] = aggregation
124124

125125
self._attributes_aggregation[attributes].aggregate(measurement)

opentelemetry-sdk/src/opentelemetry/sdk/_metrics/_internal/aggregation.py

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
)
4040
from opentelemetry.sdk._metrics._internal.point import PointT, Sum
4141
from opentelemetry.util._time import _time_ns
42+
from opentelemetry.util.types import Attributes
4243

4344
_PointVarT = TypeVar("_PointVarT", bound=PointT)
4445

@@ -58,8 +59,9 @@ class AggregationTemporality(IntEnum):
5859

5960

6061
class _Aggregation(ABC, Generic[_PointVarT]):
61-
def __init__(self):
62+
def __init__(self, attributes: Attributes):
6263
self._lock = Lock()
64+
self._attributes = attributes
6365

6466
@abstractmethod
6567
def aggregate(self, measurement: Measurement) -> None:
@@ -84,7 +86,9 @@ class Aggregation(ABC):
8486
"""
8587

8688
@abstractmethod
87-
def _create_aggregation(self, instrument: Instrument) -> _Aggregation:
89+
def _create_aggregation(
90+
self, instrument: Instrument, attributes: Attributes
91+
) -> _Aggregation:
8892
"""Creates an aggregation"""
8993

9094

@@ -107,48 +111,55 @@ class DefaultAggregation(Aggregation):
107111
==================================================== ====================================
108112
"""
109113

110-
def _create_aggregation(self, instrument: Instrument) -> _Aggregation:
114+
def _create_aggregation(
115+
self, instrument: Instrument, attributes: Attributes
116+
) -> _Aggregation:
111117

112118
# pylint: disable=too-many-return-statements
113119
if isinstance(instrument, Counter):
114120
return _SumAggregation(
121+
attributes,
115122
instrument_is_monotonic=True,
116123
instrument_temporality=AggregationTemporality.DELTA,
117124
)
118125
if isinstance(instrument, UpDownCounter):
119126
return _SumAggregation(
127+
attributes,
120128
instrument_is_monotonic=False,
121129
instrument_temporality=AggregationTemporality.DELTA,
122130
)
123131

124132
if isinstance(instrument, ObservableCounter):
125133
return _SumAggregation(
134+
attributes,
126135
instrument_is_monotonic=True,
127136
instrument_temporality=AggregationTemporality.CUMULATIVE,
128137
)
129138

130139
if isinstance(instrument, ObservableUpDownCounter):
131140
return _SumAggregation(
141+
attributes,
132142
instrument_is_monotonic=False,
133143
instrument_temporality=AggregationTemporality.CUMULATIVE,
134144
)
135145

136146
if isinstance(instrument, Histogram):
137-
return _ExplicitBucketHistogramAggregation()
147+
return _ExplicitBucketHistogramAggregation(attributes)
138148

139149
if isinstance(instrument, ObservableGauge):
140-
return _LastValueAggregation()
150+
return _LastValueAggregation(attributes)
141151

142152
raise Exception(f"Invalid instrument type {type(instrument)} found")
143153

144154

145155
class _SumAggregation(_Aggregation[Sum]):
146156
def __init__(
147157
self,
158+
attributes: Attributes,
148159
instrument_is_monotonic: bool,
149160
instrument_temporality: AggregationTemporality,
150161
):
151-
super().__init__()
162+
super().__init__(attributes)
152163

153164
self._start_time_unix_nano = _time_ns()
154165
self._instrument_temporality = instrument_temporality
@@ -205,8 +216,8 @@ def collect(self) -> Optional[Sum]:
205216

206217

207218
class _LastValueAggregation(_Aggregation[Gauge]):
208-
def __init__(self):
209-
super().__init__()
219+
def __init__(self, attributes: Attributes):
220+
super().__init__(attributes)
210221
self._value = None
211222

212223
def aggregate(self, measurement: Measurement):
@@ -232,6 +243,7 @@ def collect(self) -> Optional[Gauge]:
232243
class _ExplicitBucketHistogramAggregation(_Aggregation[HistogramPoint]):
233244
def __init__(
234245
self,
246+
attributes: Attributes,
235247
boundaries: Sequence[float] = (
236248
0.0,
237249
5.0,
@@ -246,7 +258,7 @@ def __init__(
246258
),
247259
record_min_max: bool = True,
248260
):
249-
super().__init__()
261+
super().__init__(attributes)
250262
self._boundaries = tuple(boundaries)
251263
self._bucket_counts = self._get_empty_bucket_counts()
252264
self._min = inf
@@ -464,10 +476,13 @@ def __init__(
464476
self._boundaries = boundaries
465477
self._record_min_max = record_min_max
466478

467-
def _create_aggregation(self, instrument: Instrument) -> _Aggregation:
479+
def _create_aggregation(
480+
self, instrument: Instrument, attributes: Attributes
481+
) -> _Aggregation:
468482
return _ExplicitBucketHistogramAggregation(
469-
boundaries=self._boundaries,
470-
record_min_max=self._record_min_max,
483+
attributes,
484+
self._boundaries,
485+
self._record_min_max,
471486
)
472487

473488

@@ -477,7 +492,9 @@ class SumAggregation(Aggregation):
477492
- The arithmetic sum of Measurement values.
478493
"""
479494

480-
def _create_aggregation(self, instrument: Instrument) -> _Aggregation:
495+
def _create_aggregation(
496+
self, instrument: Instrument, attributes: Attributes
497+
) -> _Aggregation:
481498

482499
temporality = AggregationTemporality.UNSPECIFIED
483500
if isinstance(instrument, Synchronous):
@@ -486,6 +503,7 @@ def _create_aggregation(self, instrument: Instrument) -> _Aggregation:
486503
temporality = AggregationTemporality.CUMULATIVE
487504

488505
return _SumAggregation(
506+
attributes,
489507
isinstance(instrument, (Counter, ObservableCounter)),
490508
temporality,
491509
)
@@ -499,12 +517,16 @@ class LastValueAggregation(Aggregation):
499517
- The timestamp of the last Measurement.
500518
"""
501519

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)
504524

505525

506526
class DropAggregation(Aggregation):
507527
"""Using this aggregation will make all measurements be ignored."""
508528

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

Comments
 (0)