Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f31a2f5

Browse files
authored
Merge branch 'main' into codeboten/restore-prometheus
2 parents 29fee5d + 24bd23d commit f31a2f5

File tree

10 files changed

+515
-76
lines changed

10 files changed

+515
-76
lines changed

opentelemetry-sdk/src/opentelemetry/sdk/_metrics/__init__.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
)
4747
from opentelemetry.sdk._metrics.metric_reader import MetricReader
4848
from opentelemetry.sdk._metrics.sdk_configuration import SdkConfiguration
49+
from opentelemetry.sdk._metrics.view import View
4950
from opentelemetry.sdk.resources import Resource
5051
from opentelemetry.sdk.util.instrumentation import InstrumentationInfo
5152
from opentelemetry.util._once import Once
@@ -154,12 +155,17 @@ def __init__(
154155
metric_readers: Sequence[MetricReader] = (),
155156
resource: Resource = Resource.create({}),
156157
shutdown_on_exit: bool = True,
158+
views: Sequence[View] = (),
159+
enable_default_view: bool = True,
157160
):
158161
self._lock = Lock()
159162
self._meter_lock = Lock()
160163
self._atexit_handler = None
161164
self._sdk_config = SdkConfiguration(
162-
resource=resource, metric_readers=metric_readers, views=()
165+
resource=resource,
166+
metric_readers=metric_readers,
167+
views=views,
168+
enable_default_view=enable_default_view,
163169
)
164170
self._measurement_consumer = SynchronousMeasurementConsumer(
165171
sdk_config=self._sdk_config

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from typing import Iterable, Set
1919

2020
from opentelemetry.sdk._metrics.aggregation import (
21+
_Aggregation,
2122
_convert_aggregation_temporality,
2223
)
2324
from opentelemetry.sdk._metrics.measurement import Measurement
@@ -34,10 +35,10 @@ def __init__(
3435
name: str,
3536
unit: str,
3637
description: str,
37-
aggregation: type,
38+
aggregation: _Aggregation,
3839
instrumentation_info: InstrumentationInfo,
3940
resource: Resource,
40-
attribute_keys: Set[str] = None,
41+
attribute_keys: Set[str],
4142
):
4243
self._name = name
4344
self._unit = unit
@@ -52,7 +53,7 @@ def __init__(
5253

5354
def consume_measurement(self, measurement: Measurement) -> None:
5455

55-
if self._attribute_keys is not None:
56+
if self._attribute_keys:
5657

5758
attributes = {}
5859

@@ -68,7 +69,7 @@ def consume_measurement(self, measurement: Measurement) -> None:
6869

6970
if attributes not in self._attributes_aggregation.keys():
7071
with self._lock:
71-
self._attributes_aggregation[attributes] = self._aggregation()
72+
self._attributes_aggregation[attributes] = self._aggregation
7273

7374
self._attributes_aggregation[attributes].aggregate(measurement)
7475

opentelemetry-sdk/src/opentelemetry/sdk/_metrics/instrument.py

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
# pylint: disable=too-many-ancestors
1616

1717
import logging
18+
from abc import ABC, abstractmethod
1819
from typing import Dict, Generator, Iterable, Union
1920

2021
from opentelemetry._metrics.instrument import CallbackT
@@ -30,14 +31,28 @@
3031
ObservableUpDownCounter as APIObservableUpDownCounter,
3132
)
3233
from opentelemetry._metrics.instrument import UpDownCounter as APIUpDownCounter
34+
from opentelemetry.sdk._metrics.aggregation import (
35+
_Aggregation,
36+
_ExplicitBucketHistogramAggregation,
37+
_LastValueAggregation,
38+
_SumAggregation,
39+
)
3340
from opentelemetry.sdk._metrics.measurement import Measurement
3441
from opentelemetry.sdk._metrics.measurement_consumer import MeasurementConsumer
42+
from opentelemetry.sdk._metrics.point import AggregationTemporality
3543
from opentelemetry.sdk.util.instrumentation import InstrumentationInfo
3644

3745
_logger = logging.getLogger(__name__)
3846

3947

40-
class _Synchronous:
48+
class _Instrument(ABC):
49+
@property
50+
@abstractmethod
51+
def _default_aggregation(self) -> _Aggregation:
52+
pass
53+
54+
55+
class _Synchronous(_Instrument):
4156
def __init__(
4257
self,
4358
name: str,
@@ -49,12 +64,12 @@ def __init__(
4964
self.name = name
5065
self.unit = unit
5166
self.description = description
52-
self._instrumentation_info = instrumentation_info
67+
self.instrumentation_info = instrumentation_info
5368
self._measurement_consumer = measurement_consumer
5469
super().__init__(name, unit=unit, description=description)
5570

5671

57-
class _Asynchronous:
72+
class _Asynchronous(_Instrument):
5873
def __init__(
5974
self,
6075
name: str,
@@ -67,7 +82,7 @@ def __init__(
6782
self.name = name
6883
self.unit = unit
6984
self.description = description
70-
self._instrumentation_info = instrumentation_info
85+
self.instrumentation_info = instrumentation_info
7186
self._measurement_consumer = measurement_consumer
7287
super().__init__(name, callback, unit=unit, description=description)
7388

@@ -86,6 +101,13 @@ def callback(self) -> CallbackT:
86101

87102

88103
class Counter(_Synchronous, APICounter):
104+
@property
105+
def _default_aggregation(self) -> _Aggregation:
106+
return _SumAggregation(
107+
instrument_is_monotonic=True,
108+
instrument_temporality=AggregationTemporality.DELTA,
109+
)
110+
89111
def add(
90112
self, amount: Union[int, float], attributes: Dict[str, str] = None
91113
):
@@ -100,6 +122,13 @@ def add(
100122

101123

102124
class UpDownCounter(_Synchronous, APIUpDownCounter):
125+
@property
126+
def _default_aggregation(self) -> _Aggregation:
127+
return _SumAggregation(
128+
instrument_is_monotonic=False,
129+
instrument_temporality=AggregationTemporality.DELTA,
130+
)
131+
103132
def add(
104133
self, amount: Union[int, float], attributes: Dict[str, str] = None
105134
):
@@ -109,14 +138,28 @@ def add(
109138

110139

111140
class ObservableCounter(_Asynchronous, APIObservableCounter):
112-
pass
141+
@property
142+
def _default_aggregation(self) -> _Aggregation:
143+
return _SumAggregation(
144+
instrument_is_monotonic=True,
145+
instrument_temporality=AggregationTemporality.CUMULATIVE,
146+
)
113147

114148

115149
class ObservableUpDownCounter(_Asynchronous, APIObservableUpDownCounter):
116-
pass
150+
@property
151+
def _default_aggregation(self) -> _Aggregation:
152+
return _SumAggregation(
153+
instrument_is_monotonic=False,
154+
instrument_temporality=AggregationTemporality.CUMULATIVE,
155+
)
117156

118157

119158
class Histogram(_Synchronous, APIHistogram):
159+
@property
160+
def _default_aggregation(self) -> _Aggregation:
161+
return _ExplicitBucketHistogramAggregation()
162+
120163
def record(
121164
self, amount: Union[int, float], attributes: Dict[str, str] = None
122165
):
@@ -132,4 +175,6 @@ def record(
132175

133176

134177
class ObservableGauge(_Asynchronous, APIObservableGauge):
135-
pass
178+
@property
179+
def _default_aggregation(self) -> _Aggregation:
180+
return _LastValueAggregation()

opentelemetry-sdk/src/opentelemetry/sdk/_metrics/metric_reader_storage.py

Lines changed: 45 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,11 @@
1515
from threading import RLock
1616
from typing import Dict, Iterable, List
1717

18-
from opentelemetry._metrics.instrument import Counter, Histogram, Instrument
18+
from opentelemetry._metrics.instrument import Instrument
1919
from opentelemetry.sdk._metrics._view_instrument_match import (
2020
_ViewInstrumentMatch,
2121
)
22-
from opentelemetry.sdk._metrics.aggregation import (
23-
AggregationTemporality,
24-
_ExplicitBucketHistogramAggregation,
25-
_LastValueAggregation,
26-
_SumAggregation,
27-
)
22+
from opentelemetry.sdk._metrics.aggregation import AggregationTemporality
2823
from opentelemetry.sdk._metrics.measurement import Measurement
2924
from opentelemetry.sdk._metrics.point import Metric
3025
from opentelemetry.sdk._metrics.sdk_configuration import SdkConfiguration
@@ -46,6 +41,7 @@ def _get_or_init_view_instrument_match(
4641
) -> List["_ViewInstrumentMatch"]:
4742
# Optimistically get the relevant views for the given instrument. Once set for a given
4843
# instrument, the mapping will never change
44+
4945
if instrument in self._view_instrument_match:
5046
return self._view_instrument_match[instrument]
5147

@@ -55,50 +51,59 @@ def _get_or_init_view_instrument_match(
5551
return self._view_instrument_match[instrument]
5652

5753
# not present, hold the lock and add a new mapping
58-
matches = []
54+
view_instrument_matches = []
5955
for view in self._sdk_config.views:
60-
if view.match(instrument):
61-
# Note: if a view matches multiple instruments, this will create a separate
62-
# _ViewInstrumentMatch per instrument. If the user's View configuration includes a
63-
# name, this will cause multiple conflicting output streams.
64-
matches.append(
56+
# pylint: disable=protected-access
57+
if view._match(instrument):
58+
59+
if view._aggregation is not None:
60+
aggregation = view._aggregation._create_aggregation(
61+
instrument
62+
)
63+
else:
64+
aggregation = instrument._default_aggregation
65+
66+
view_instrument_matches.append(
6567
_ViewInstrumentMatch(
66-
name=view.name or instrument.name,
67-
resource=self._sdk_config.resource,
68-
instrumentation_info=None,
69-
aggregation=view.aggregation,
68+
name=view._name or instrument.name,
7069
unit=instrument.unit,
71-
description=view.description,
70+
description=(
71+
view._description or instrument.description
72+
),
73+
aggregation=aggregation,
74+
instrumentation_info=(
75+
instrument.instrumentation_info
76+
),
77+
resource=self._sdk_config.resource,
78+
attribute_keys=view._attribute_keys,
7279
)
7380
)
7481

7582
# if no view targeted the instrument, use the default
76-
if not matches:
77-
# TODO: the logic to select aggregation could be moved
78-
if isinstance(instrument, Counter):
79-
agg = _SumAggregation(True, AggregationTemporality.DELTA)
80-
elif isinstance(instrument, Histogram):
81-
agg = _ExplicitBucketHistogramAggregation()
82-
else:
83-
agg = _LastValueAggregation()
84-
matches.append(
83+
if (
84+
not view_instrument_matches
85+
and self._sdk_config.enable_default_view
86+
):
87+
view_instrument_matches.append(
8588
_ViewInstrumentMatch(
86-
resource=self._sdk_config.resource,
87-
instrumentation_info=None,
88-
aggregation=agg,
89+
name=instrument.name,
8990
unit=instrument.unit,
9091
description=instrument.description,
91-
name=instrument.name,
92+
# pylint: disable=protected-access
93+
aggregation=instrument._default_aggregation,
94+
instrumentation_info=instrument.instrumentation_info,
95+
resource=self._sdk_config.resource,
96+
attribute_keys=set(),
9297
)
9398
)
94-
self._view_instrument_match[instrument] = matches
95-
return matches
99+
self._view_instrument_match[instrument] = view_instrument_matches
100+
return view_instrument_matches
96101

97102
def consume_measurement(self, measurement: Measurement) -> None:
98-
for matches in self._get_or_init_view_instrument_match(
103+
for view_instrument_match in self._get_or_init_view_instrument_match(
99104
measurement.instrument
100105
):
101-
matches.consume_measurement(measurement)
106+
view_instrument_match.consume_measurement(measurement)
102107

103108
def collect(self, temporality: AggregationTemporality) -> Iterable[Metric]:
104109
# use a list instead of yielding to prevent a slow reader from holding SDK locks
@@ -111,9 +116,11 @@ def collect(self, temporality: AggregationTemporality) -> Iterable[Metric]:
111116
# that end times can be slightly skewed among the metric streams produced by the SDK,
112117
# but we still align the output timestamps for a single instrument.
113118
with self._lock:
114-
for matches in self._view_instrument_match.values():
115-
for match in matches:
116-
metrics.extend(match.collect(temporality))
119+
for (
120+
view_instrument_matches
121+
) in self._view_instrument_match.values():
122+
for view_instrument_match in view_instrument_matches:
123+
metrics.extend(view_instrument_match.collect(temporality))
117124

118125
return metrics
119126

opentelemetry-sdk/src/opentelemetry/sdk/_metrics/sdk_configuration.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ class SdkConfiguration:
1111
resource: Resource
1212
metric_readers: Sequence[MetricReader]
1313
views: Sequence[View]
14+
enable_default_view: bool

0 commit comments

Comments
 (0)