Skip to content

Commit f43484c

Browse files
committed
Move Metrics API behind internal package
1 parent fd0e18e commit f43484c

File tree

24 files changed

+199
-136
lines changed

24 files changed

+199
-136
lines changed

.github/workflows/test.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ env:
1010
# Otherwise, set variable to the commit of your branch on
1111
# opentelemetry-python-contrib which is compatible with these Core repo
1212
# changes.
13-
CONTRIB_REPO_SHA: 4cfca481f8e2c8af5f7cfd032997fac692995f67
13+
CONTRIB_REPO_SHA: 008cd2370dcd3e87cca8c0ddbb0b820681fd7346
1414
# This is needed because we do not clone the core repo in contrib builds anymore.
1515
# When running contrib builds as part of core builds, we use actions/checkout@v2 which
1616
# does not set an environment variable (simply just runs tox), which is different when

CHANGELOG.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased](https://github.com/open-telemetry/opentelemetry-python/compare/v1.11.1-0.30b1...HEAD)
99

10-
## [1.11.1-0.30b1](https://github.com/open-telemetry/opentelemetry-python/releases/tag/v1.11.1-0.30b1) - 2022-04-21
10+
- Move Metrics API behind internal package
11+
([#2651](https://github.com/open-telemetry/opentelemetry-python/pull/2651))
1112

13+
## [1.11.1-0.30b1](https://github.com/open-telemetry/opentelemetry-python/releases/tag/v1.11.1-0.30b1) - 2022-04-21
1214

1315
- Add parameter to MetricReader constructor to select aggregation per instrument kind
1416
([#2638](https://github.com/open-telemetry/opentelemetry-python/pull/2638))

docs/api/metrics.instrument.rst

-8
This file was deleted.

docs/api/metrics.observation.rst

-7
This file was deleted.

docs/api/metrics.rst

-5
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,8 @@ opentelemetry._metrics package
88

99
Once metrics become stable, this package will be be renamed to ``opentelemetry.metrics``.
1010

11-
Submodules
12-
----------
13-
1411
.. toctree::
1512

16-
metrics.instrument
17-
metrics.observation
1813

1914
Module contents
2015
---------------

docs/conf.py

+1-21
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,6 @@
9797
# https://github.com/sphinx-doc/sphinx/pull/3744
9898
nitpick_ignore = [
9999
("py:class", "ValueT"),
100-
("py:class", "MetricT"),
101-
("py:class", "InstrumentT"),
102-
("py:obj", "opentelemetry._metrics.instrument.InstrumentT"),
103100
# Even if wrapt is added to intersphinx_mapping, sphinx keeps failing
104101
# with "class reference target not found: ObjectProxy".
105102
("py:class", "ObjectProxy"),
@@ -142,24 +139,7 @@
142139
"examples/error_handler/error_handler_1",
143140
]
144141

145-
_exclude_members = [
146-
"_ProxyObservableUpDownCounter",
147-
"_ProxyHistogram",
148-
"_ProxyObservableGauge",
149-
"_ProxyInstrument",
150-
"_ProxyAsynchronousInstrument",
151-
"_ProxyCounter",
152-
"_ProxyUpDownCounter",
153-
"_ProxyObservableCounter",
154-
"_ProxyObservableGauge",
155-
"_abc_impl",
156-
"_Adding",
157-
"_Grouping",
158-
"_Monotonic",
159-
"_NonMonotonic",
160-
"Synchronous",
161-
"Asynchronous",
162-
]
142+
_exclude_members = ["_abc_impl"]
163143

164144
autodoc_default_options = {
165145
"members": True,

docs/examples/metrics/example.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
from typing import Iterable
22

3-
from opentelemetry._metrics import get_meter_provider, set_meter_provider
4-
from opentelemetry._metrics.observation import Observation
3+
from opentelemetry._metrics import (
4+
Observation,
5+
get_meter_provider,
6+
set_meter_provider,
7+
)
58
from opentelemetry.exporter.otlp.proto.grpc._metric_exporter import (
69
OTLPMetricExporter,
710
)

docs/getting_started/metrics_example.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@
1717

1818
from typing import Iterable
1919

20-
from opentelemetry._metrics import get_meter_provider, set_meter_provider
21-
from opentelemetry._metrics.observation import Observation
20+
from opentelemetry._metrics import (
21+
Observation,
22+
get_meter_provider,
23+
set_meter_provider,
24+
)
2225
from opentelemetry.sdk._metrics import MeterProvider
2326
from opentelemetry.sdk._metrics.export import (
2427
ConsoleMetricExporter,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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+
"""
16+
The OpenTelemetry metrics API describes the classes used to generate
17+
metrics.
18+
19+
The :class:`.MeterProvider` provides users access to the :class:`.Meter` which in
20+
turn is used to create :class:`.Instrument` objects. The :class:`.Instrument` objects are
21+
used to record measurements.
22+
23+
This module provides abstract (i.e. unimplemented) classes required for
24+
metrics, and a concrete no-op implementation :class:`.NoOpMeter` that allows applications
25+
to use the API package alone without a supporting implementation.
26+
27+
To get a meter, you need to provide the package name from which you are
28+
calling the meter APIs to OpenTelemetry by calling `MeterProvider.get_meter`
29+
with the calling instrumentation name and the version of your package.
30+
31+
The following code shows how to obtain a meter using the global :class:`.MeterProvider`::
32+
33+
from opentelemetry._metrics import get_meter
34+
35+
meter = get_meter("example-meter")
36+
counter = meter.create_counter("example-counter")
37+
38+
.. versionadded:: 1.10.0
39+
"""
40+
41+
from opentelemetry._metrics._internal import (
42+
Meter,
43+
MeterProvider,
44+
NoOpMeter,
45+
NoOpMeterProvider,
46+
get_meter,
47+
get_meter_provider,
48+
set_meter_provider,
49+
)
50+
from opentelemetry._metrics._internal.instrument import (
51+
Asynchronous,
52+
CallbackT,
53+
Counter,
54+
Histogram,
55+
Instrument,
56+
NoOpCounter,
57+
NoOpHistogram,
58+
NoOpObservableCounter,
59+
NoOpObservableGauge,
60+
NoOpObservableUpDownCounter,
61+
NoOpUpDownCounter,
62+
ObservableCounter,
63+
ObservableGauge,
64+
ObservableUpDownCounter,
65+
Synchronous,
66+
UpDownCounter,
67+
)
68+
from opentelemetry._metrics._internal.observation import Observation
69+
70+
for obj in [
71+
Counter,
72+
Synchronous,
73+
Asynchronous,
74+
get_meter_provider,
75+
get_meter,
76+
Histogram,
77+
Meter,
78+
MeterProvider,
79+
Instrument,
80+
NoOpCounter,
81+
NoOpHistogram,
82+
NoOpMeter,
83+
NoOpMeterProvider,
84+
NoOpObservableCounter,
85+
NoOpObservableGauge,
86+
NoOpObservableUpDownCounter,
87+
NoOpUpDownCounter,
88+
ObservableCounter,
89+
ObservableGauge,
90+
ObservableUpDownCounter,
91+
Observation,
92+
set_meter_provider,
93+
UpDownCounter,
94+
]:
95+
obj.__module__ = __name__
96+
97+
__all__ = [
98+
"MeterProvider",
99+
"NoOpMeterProvider",
100+
"Meter",
101+
"Counter",
102+
"NoOpCounter",
103+
"UpDownCounter",
104+
"NoOpUpDownCounter",
105+
"Histogram",
106+
"NoOpHistogram",
107+
"ObservableCounter",
108+
"NoOpObservableCounter",
109+
"ObservableUpDownCounter",
110+
"Instrument",
111+
"Synchronous",
112+
"Asynchronous",
113+
"NoOpObservableGauge",
114+
"ObservableGauge",
115+
"NoOpObservableUpDownCounter",
116+
"get_meter",
117+
"get_meter_provider",
118+
"set_meter_provider",
119+
"Observation",
120+
"CallbackT",
121+
"NoOpMeter",
122+
]

opentelemetry-api/src/opentelemetry/_metrics/_internal/__init__.py

+15-21
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
from threading import Lock
4848
from typing import List, Optional, Sequence, Set, Tuple, Union, cast
4949

50-
from opentelemetry._metrics.instrument import (
50+
from opentelemetry._metrics._internal.instrument import (
5151
CallbackT,
5252
Counter,
5353
Histogram,
@@ -77,7 +77,7 @@
7777
_logger = getLogger(__name__)
7878

7979

80-
ProxyInstrumentT = Union[
80+
_ProxyInstrumentT = Union[
8181
_ProxyCounter,
8282
_ProxyHistogram,
8383
_ProxyObservableCounter,
@@ -279,9 +279,8 @@ def create_observable_counter(
279279
) -> ObservableCounter:
280280
"""Creates an `ObservableCounter` instrument
281281
282-
An observable counter observes a monotonically increasing count by
283-
calling provided callbacks which returns multiple
284-
:class:`~opentelemetry._metrics.observation.Observation`.
282+
An observable counter observes a monotonically increasing count by calling provided
283+
callbacks which returns multiple :class:`~opentelemetry._metrics.Observation`.
285284
286285
For example, an observable counter could be used to report system CPU
287286
time periodically. Here is a basic implementation::
@@ -319,9 +318,8 @@ def cpu_time_callback() -> Iterable[Observation]:
319318
yield Observation(int(states[1]) // 100, {"cpu": cpu, "state": "nice"})
320319
# ... other states
321320
322-
Alternatively, you can pass a sequence of generators directly instead
323-
of a sequence of callbacks, which each should return iterables of
324-
:class:`~opentelemetry._metrics.observation.Observation`::
321+
Alternatively, you can pass a sequence of generators directly instead of a sequence of
322+
callbacks, which each should return iterables of :class:`~opentelemetry._metrics.Observation`::
325323
326324
def cpu_time_callback(states_to_include: set[str]) -> Iterable[Iterable[Observation]]:
327325
while True:
@@ -348,10 +346,8 @@ def cpu_time_callback(states_to_include: set[str]) -> Iterable[Iterable[Observat
348346
Args:
349347
name: The name of the instrument to be created
350348
callbacks: A sequence of callbacks that return an iterable of
351-
:class:`~opentelemetry._metrics.observation.Observation`.
352-
Alternatively, can be a sequence of generators that each yields
353-
iterables of
354-
:class:`~opentelemetry._metrics.observation.Observation`.
349+
:class:`~opentelemetry._metrics.Observation`. Alternatively, can be a sequence of generators that each
350+
yields iterables of :class:`~opentelemetry._metrics.Observation`.
355351
unit: The unit for observations this instrument reports. For
356352
example, ``By`` for bytes. UCUM units are recommended.
357353
description: A description for this instrument and what it measures.
@@ -364,7 +360,7 @@ def create_histogram(
364360
unit: str = "",
365361
description: str = "",
366362
) -> Histogram:
367-
"""Creates a `opentelemetry._metrics.instrument.Histogram` instrument
363+
"""Creates a :class:`~opentelemetry._metrics.Histogram` instrument
368364
369365
Args:
370366
name: The name of the instrument to be created
@@ -386,9 +382,8 @@ def create_observable_gauge(
386382
Args:
387383
name: The name of the instrument to be created
388384
callbacks: A sequence of callbacks that return an iterable of
389-
:class:`~opentelemetry._metrics.observation.Observation`.
390-
Alternatively, can be a generator that yields iterables of
391-
:class:`~opentelemetry._metrics.observation.Observation`.
385+
:class:`~opentelemetry._metrics.Observation`. Alternatively, can be a generator that yields iterables
386+
of :class:`~opentelemetry._metrics.Observation`.
392387
unit: The unit for observations this instrument reports. For
393388
example, ``By`` for bytes. UCUM units are recommended.
394389
description: A description for this instrument and what it measures.
@@ -407,9 +402,8 @@ def create_observable_up_down_counter(
407402
Args:
408403
name: The name of the instrument to be created
409404
callbacks: A sequence of callbacks that return an iterable of
410-
:class:`~opentelemetry._metrics.observation.Observation`.
411-
Alternatively, can be a generator that yields iterables of
412-
:class:`~opentelemetry._metrics.observation.Observation`.
405+
:class:`~opentelemetry._metrics.Observation`. Alternatively, can be a generator that yields iterables
406+
of :class:`~opentelemetry._metrics.Observation`.
413407
unit: The unit for observations this instrument reports. For
414408
example, ``By`` for bytes. UCUM units are recommended.
415409
description: A description for this instrument and what it measures.
@@ -425,7 +419,7 @@ def __init__(
425419
) -> None:
426420
super().__init__(name, version=version, schema_url=schema_url)
427421
self._lock = Lock()
428-
self._instruments: List[ProxyInstrumentT] = []
422+
self._instruments: List[_ProxyInstrumentT] = []
429423
self._real_meter: Optional[Meter] = None
430424

431425
def on_set_meter_provider(self, meter_provider: MeterProvider) -> None:
@@ -718,7 +712,7 @@ def get_meter(
718712
"""Returns a `Meter` for use by the given instrumentation library.
719713
720714
This function is a convenience wrapper for
721-
opentelemetry.trace.MeterProvider.get_meter.
715+
`opentelemetry._metrics.MeterProvider.get_meter`.
722716
723717
If meter_provider is omitted the current configured one is used.
724718
"""

0 commit comments

Comments
 (0)