19
19
20
20
from typing_extensions import final
21
21
22
+ from opentelemetry .sdk ._metrics .aggregation import (
23
+ DefaultAggregation ,
24
+ _AggregationFactory ,
25
+ )
22
26
from opentelemetry .sdk ._metrics .instrument import (
23
27
Counter ,
24
28
Histogram ,
@@ -52,6 +56,19 @@ class MetricReader(ABC):
52
56
their association to their default aggregation temporalities.
53
57
The value passed here will override the corresponding values set
54
58
via the environment variable
59
+ preferred_aggregation: A mapping between instrument classes and
60
+ aggregation instances. By default maps all instrument classes to an
61
+ instance of `DefaultAggregation`. This mapping will be used to
62
+ define the default aggregation of every instrument class. If the
63
+ user wants to make a change in the default aggregation of an
64
+ instrument class, it is enough to pass here a dictionary whose keys
65
+ are the instrument classes and the values are the corresponding
66
+ desired aggregation for the instrument classes that the user wants
67
+ to change, not necessarily all of them. The classes not included in
68
+ the passed dictionary will retain their association to their
69
+ default aggregations. The aggregation defined here will be
70
+ overriden by an aggregation defined by a view that is not
71
+ `DefaultAggregation`.
55
72
56
73
.. document protected _receive_metrics which is a intended to be overriden by subclass
57
74
.. automethod:: _receive_metrics
@@ -61,7 +78,9 @@ class MetricReader(ABC):
61
78
# to the end of the documentation paragraph above.
62
79
63
80
def __init__ (
64
- self , preferred_temporality : Dict [type , AggregationTemporality ] = None
81
+ self ,
82
+ preferred_temporality : Dict [type , AggregationTemporality ] = None ,
83
+ preferred_aggregation : Dict [type , _AggregationFactory ] = None ,
65
84
) -> None :
66
85
self ._collect : Callable [
67
86
["MetricReader" , AggregationTemporality ], Iterable [Metric ]
@@ -106,6 +125,17 @@ def __init__(
106
125
)
107
126
108
127
self ._instrument_class_temporality .update (preferred_temporality or {})
128
+ self ._preferred_temporality = preferred_temporality
129
+ self ._instrument_class_aggregation = {
130
+ Counter : DefaultAggregation (),
131
+ UpDownCounter : DefaultAggregation (),
132
+ Histogram : DefaultAggregation (),
133
+ ObservableCounter : DefaultAggregation (),
134
+ ObservableUpDownCounter : DefaultAggregation (),
135
+ ObservableGauge : DefaultAggregation (),
136
+ }
137
+
138
+ self ._instrument_class_aggregation .update (preferred_aggregation or {})
109
139
110
140
@final
111
141
def collect (self ) -> None :
0 commit comments