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

Skip to content

Commit 4413869

Browse files
aabmassocelotllzchensrikanthccv
authored
Remove enable_default_view option from sdk MeterProvider (open-telemetry#2547)
* Remove enable_default_view option from sdk MeterProvider * document rest of MeterProvider constructor params * fix docs build * remove unused function Co-authored-by: Diego Hurtado <[email protected]> Co-authored-by: Leighton Chen <[email protected]> Co-authored-by: Srikanth Chekuri <[email protected]>
1 parent 3fb9399 commit 4413869

15 files changed

+140
-67
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2020
([#2540](https://github.com/open-telemetry/opentelemetry-python/pull/2540))
2121
- Drop the usage of name field from log model in OTLP
2222
([#2565](https://github.com/open-telemetry/opentelemetry-python/pull/2565))
23+
- Remove `enable_default_view` option from sdk MeterProvider
24+
([#2547](https://github.com/open-telemetry/opentelemetry-python/pull/2547))
2325

2426
## [1.10.0-0.29b0](https://github.com/open-telemetry/opentelemetry-python/releases/tag/v1.10.0-0.29b0) - 2022-03-10
2527

docs/sdk/metrics.aggregation.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
opentelemetry.sdk._metrics.aggregation
2+
==========================================
3+
4+
.. automodule:: opentelemetry.sdk._metrics.aggregation
5+
:members:
6+
:undoc-members:
7+
:show-inheritance:

docs/sdk/metrics.metric_reader.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
opentelemetry.sdk._metrics.metric_reader
2+
==========================================
3+
4+
.. automodule:: opentelemetry.sdk._metrics.metric_reader
5+
:members:
6+
:undoc-members:
7+
:show-inheritance:

docs/sdk/metrics.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ opentelemetry.sdk._metrics package
1111
Submodules
1212
----------
1313

14+
.. toctree::
15+
16+
metrics.view
17+
metrics.aggregation
18+
metrics.metric_reader
19+
1420
.. automodule:: opentelemetry.sdk._metrics
1521
:members:
1622
:undoc-members:

docs/sdk/metrics.view.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
opentelemetry.sdk._metrics.view
2+
==========================================
3+
4+
.. automodule:: opentelemetry.sdk._metrics.view
5+
:members:
6+
:undoc-members:
7+
:show-inheritance:

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

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,15 +148,41 @@ def create_observable_up_down_counter(
148148

149149

150150
class MeterProvider(APIMeterProvider):
151-
"""See `opentelemetry._metrics.MeterProvider`."""
151+
r"""See `opentelemetry._metrics.MeterProvider`.
152+
153+
Args:
154+
metric_readers: Register metric readers to collect metrics from the SDK on demand. Each
155+
`MetricReader` is completely independent and will collect separate streams of
156+
metrics. TODO: reference ``PeriodicExportingMetricReader`` usage with push
157+
exporters here.
158+
resource: The resource representing what the metrics emitted from the SDK pertain to.
159+
shutdown_on_exit: If true, registers an `atexit` handler to call
160+
`MeterProvider.shutdown`
161+
views: The views to configure the metric output the SDK
162+
163+
By default, instruments which do not match any `View` (or if no `View`\ s are provided)
164+
will report metrics with the default aggregation for the instrument's kind. To disable
165+
instruments by default, configure a match-all `View` with `DropAggregation` and then create
166+
`View`\ s to re-enable individual instruments:
167+
168+
.. code-block:: python
169+
:caption: Disable default views
170+
171+
MeterProvider(
172+
views=[
173+
View(instrument_name="*", aggregation=DropAggregation()),
174+
View(instrument_name="mycounter"),
175+
],
176+
# ...
177+
)
178+
"""
152179

153180
def __init__(
154181
self,
155182
metric_readers: Sequence[MetricReader] = (),
156183
resource: Resource = Resource.create({}),
157184
shutdown_on_exit: bool = True,
158185
views: Sequence[View] = (),
159-
enable_default_view: bool = True,
160186
):
161187
self._lock = Lock()
162188
self._meter_lock = Lock()
@@ -165,7 +191,6 @@ def __init__(
165191
resource=resource,
166192
metric_readers=metric_readers,
167193
views=views,
168-
enable_default_view=enable_default_view,
169194
)
170195
self._measurement_consumer = SynchronousMeasurementConsumer(
171196
sdk_config=self._sdk_config

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
"""
16+
.. Explicitly document private _AggregationFactory
17+
.. autoclass:: _AggregationFactory
18+
"""
19+
1520
from abc import ABC, abstractmethod
1621
from bisect import bisect_left
1722
from dataclasses import replace

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@
2424

2525

2626
class MetricReader(ABC):
27+
"""
28+
.. document protected _receive_metrics which is a intended to be overriden by subclass
29+
.. automethod:: _receive_metrics
30+
"""
31+
2732
def __init__(
2833
self,
2934
preferred_temporality: AggregationTemporality = AggregationTemporality.CUMULATIVE,
@@ -66,5 +71,8 @@ def shutdown(self) -> bool:
6671
only be shutdown once, any subsequent calls are ignored and return
6772
failure status.
6873
69-
When a `MetricReader` is registered on a `MeterProvider`, `MeterProvider.shutdown` will invoke this automatically.
74+
When a `MetricReader` is registered on a
75+
:class:`~opentelemetry.sdk._metrics.MeterProvider`,
76+
:meth:`~opentelemetry.sdk._metrics.MeterProvider.shutdown` will invoke this
77+
automatically.
7078
"""

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

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,7 @@ def _get_or_init_view_instrument_match(
6666
)
6767

6868
# if no view targeted the instrument, use the default
69-
if (
70-
not view_instrument_matches
71-
and self._sdk_config.enable_default_view
72-
):
69+
if not view_instrument_matches:
7370
view_instrument_matches.append(
7471
_ViewInstrumentMatch(
7572
view=_DEFAULT_VIEW,
@@ -104,8 +101,3 @@ def collect(self, temporality: AggregationTemporality) -> Iterable[Metric]:
104101
metrics.extend(view_instrument_match.collect(temporality))
105102

106103
return metrics
107-
108-
109-
def default_view(instrument: Instrument) -> View:
110-
# TODO: #2247
111-
return View()

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

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

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

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,35 +44,33 @@ class View:
4444
instrument must be to match the view.
4545
4646
instrument_name: This is an instrument matching attribute: the name the
47-
instrument must have to match the view. Wild card characters are
48-
supported. Wild card characters should not be used with this
49-
attribute if the view has also a ``name`` defined.
47+
instrument must have to match the view. Wild card characters are supported. Wild
48+
card characters should not be used with this attribute if the view has also a
49+
``name`` defined.
5050
5151
meter_name: This is an instrument matching attribute: the name the
5252
instrument meter must have to match the view.
5353
54-
meter_version : This is an instrument matching attribute: the version
54+
meter_version: This is an instrument matching attribute: the version
5555
the instrument meter must have to match the view.
5656
57-
meter_schema URL : This is an instrument matching attribute: the schema
57+
meter_schema_url: This is an instrument matching attribute: the schema
5858
URL the instrument meter must have to match the view.
5959
6060
name: This is a metric stream customizing attribute: the name of the
6161
metric stream. If `None`, the name of the instrument will be used.
6262
6363
description: This is a metric stream customizing attribute: the
64-
description of the metric stream. If `None`, the description of the
65-
instrument will be used.
64+
description of the metric stream. If `None`, the description of the instrument will
65+
be used.
6666
6767
attribute_keys: This is a metric stream customizing attribute: this is
68-
a set of attribute keys. If not `None` then only the measurement
69-
attributes that are in `attribute_keys` will be used to identify the
70-
metric stream.
68+
a set of attribute keys. If not `None` then only the measurement attributes that
69+
are in ``attribute_keys`` will be used to identify the metric stream.
7170
7271
aggregation: This is a metric stream customizing attribute: the
73-
aggregatation instance to use when data is aggregated for the
74-
corresponding metrics stream. If `None` the default aggregation of
75-
the instrument will be used.
72+
aggregatation instance to use when data is aggregated for the corresponding metrics
73+
stream. If `None` the default aggregation of the instrument will be used.
7674
7775
This class is not intended to be subclassed by the user.
7876
"""
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Copyright The OpenTelemetry Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from unittest import TestCase
16+
17+
from opentelemetry.sdk._metrics import MeterProvider
18+
from opentelemetry.sdk._metrics.aggregation import DropAggregation
19+
from opentelemetry.sdk._metrics.export import InMemoryMetricReader
20+
from opentelemetry.sdk._metrics.view import View
21+
22+
23+
class TestDisableDefaultViews(TestCase):
24+
def test_disable_default_views(self):
25+
reader = InMemoryMetricReader()
26+
meter_provider = MeterProvider(
27+
metric_readers=[reader],
28+
views=[View(instrument_name="*", aggregation=DropAggregation())],
29+
)
30+
meter = meter_provider.get_meter("testmeter")
31+
counter = meter.create_counter("testcounter")
32+
counter.add(10, {"label": "value1"})
33+
counter.add(10, {"label": "value2"})
34+
counter.add(10, {"label": "value3"})
35+
36+
self.assertEqual(reader.get_metrics(), [])
37+
38+
def test_disable_default_views_add_custom(self):
39+
reader = InMemoryMetricReader()
40+
meter_provider = MeterProvider(
41+
metric_readers=[reader],
42+
views=[
43+
View(instrument_name="*", aggregation=DropAggregation()),
44+
View(instrument_name="testhist"),
45+
],
46+
)
47+
meter = meter_provider.get_meter("testmeter")
48+
counter = meter.create_counter("testcounter")
49+
histogram = meter.create_histogram("testhist")
50+
counter.add(10, {"label": "value1"})
51+
counter.add(10, {"label": "value2"})
52+
counter.add(10, {"label": "value3"})
53+
histogram.record(12, {"label": "value"})
54+
55+
metrics = reader.get_metrics()
56+
self.assertEqual(len(metrics), 1)
57+
self.assertEqual(metrics[0].name, "testhist")

opentelemetry-sdk/tests/metrics/test_measurement_consumer.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ def test_creates_metric_reader_storages(self, MockMetricReaderStorage):
3939
resource=Mock(),
4040
metric_readers=reader_mocks,
4141
views=Mock(),
42-
enable_default_view=Mock(),
4342
)
4443
)
4544
self.assertEqual(len(MockMetricReaderStorage.mock_calls), 5)
@@ -56,7 +55,6 @@ def test_measurements_passed_to_each_reader_storage(
5655
resource=Mock(),
5756
metric_readers=reader_mocks,
5857
views=Mock(),
59-
enable_default_view=Mock(),
6058
)
6159
)
6260
measurement_mock = Mock()
@@ -78,7 +76,6 @@ def test_collect_passed_to_reader_stage(self, MockMetricReaderStorage):
7876
resource=Mock(),
7977
metric_readers=reader_mocks,
8078
views=Mock(),
81-
enable_default_view=Mock(),
8279
)
8380
)
8481
for r_mock, rs_mock in zip(reader_mocks, reader_storage_mocks):
@@ -99,7 +96,6 @@ def test_collect_calls_async_instruments(self, MockMetricReaderStorage):
9996
resource=Mock(),
10097
metric_readers=[reader_mock],
10198
views=Mock(),
102-
enable_default_view=Mock(),
10399
)
104100
)
105101
async_instrument_mocks = [MagicMock() for _ in range(5)]

opentelemetry-sdk/tests/metrics/test_metric_reader_storage.py

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ def test_creates_view_instrument_matches(
5656
resource=Mock(),
5757
metric_readers=(),
5858
views=(view1, view2),
59-
enable_default_view=True,
6059
)
6160
)
6261

@@ -101,7 +100,6 @@ def test_forwards_calls_to_view_instrument_match(
101100
resource=Mock(),
102101
metric_readers=(),
103102
views=(view1, view2),
104-
enable_default_view=True,
105103
)
106104
)
107105

@@ -149,7 +147,6 @@ def test_race_concurrent_measurements(self, MockViewInstrumentMatch: Mock):
149147
resource=Mock(),
150148
metric_readers=(),
151149
views=(view1,),
152-
enable_default_view=True,
153150
)
154151
)
155152

@@ -175,7 +172,6 @@ def test_default_view_enabled(self, MockViewInstrumentMatch: Mock):
175172
resource=Mock(),
176173
metric_readers=(),
177174
views=(),
178-
enable_default_view=True,
179175
)
180176
)
181177

@@ -192,35 +188,6 @@ def test_default_view_enabled(self, MockViewInstrumentMatch: Mock):
192188
storage.consume_measurement(Measurement(1, instrument2))
193189
self.assertEqual(len(MockViewInstrumentMatch.call_args_list), 1)
194190

195-
@patch(
196-
"opentelemetry.sdk._metrics.metric_reader_storage._ViewInstrumentMatch"
197-
)
198-
def test_default_view_disabled(self, MockViewInstrumentMatch: Mock):
199-
"""Instruments should not be matched with default views when disabled"""
200-
instrument1 = Mock(name="instrument1")
201-
instrument2 = Mock(name="instrument2")
202-
storage = MetricReaderStorage(
203-
SdkConfiguration(
204-
resource=Mock(),
205-
metric_readers=(),
206-
views=(),
207-
enable_default_view=False,
208-
)
209-
)
210-
211-
storage.consume_measurement(Measurement(1, instrument1))
212-
self.assertEqual(
213-
len(MockViewInstrumentMatch.call_args_list),
214-
0,
215-
MockViewInstrumentMatch.mock_calls,
216-
)
217-
storage.consume_measurement(Measurement(1, instrument1))
218-
self.assertEqual(len(MockViewInstrumentMatch.call_args_list), 0)
219-
220-
MockViewInstrumentMatch.call_args_list.clear()
221-
storage.consume_measurement(Measurement(1, instrument2))
222-
self.assertEqual(len(MockViewInstrumentMatch.call_args_list), 0)
223-
224191
def test_drop_aggregation(self):
225192

226193
counter = Counter("name", Mock(), Mock())
@@ -233,7 +200,6 @@ def test_drop_aggregation(self):
233200
instrument_name="name", aggregation=DropAggregation()
234201
),
235202
),
236-
enable_default_view=False,
237203
)
238204
)
239205
metric_reader_storage.consume_measurement(Measurement(1, counter))

opentelemetry-sdk/tests/metrics/test_view_instrument_match.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ def test_consume_measurement(self):
4646
resource=self.mock_resource,
4747
metric_readers=[],
4848
views=[],
49-
enable_default_view=True,
5049
)
5150
view_instrument_match = _ViewInstrumentMatch(
5251
view=View(
@@ -164,7 +163,6 @@ def test_collect(self):
164163
resource=self.mock_resource,
165164
metric_readers=[],
166165
views=[],
167-
enable_default_view=True,
168166
)
169167
view_instrument_match = _ViewInstrumentMatch(
170168
view=View(

0 commit comments

Comments
 (0)