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

Skip to content

Commit 3323ce2

Browse files
ocelotllzchen
andauthored
Rename Measurement to Observation (open-telemetry#2617)
* Rename Measurement to Observation Fixes open-telemetry#2451 * Fix docs * Update CHANGELOG.md Co-authored-by: Leighton Chen <[email protected]> * Update CHANGELOG.md Co-authored-by: Leighton Chen <[email protected]> * Fix examples Co-authored-by: Leighton Chen <[email protected]>
1 parent 782ac3e commit 3323ce2

File tree

12 files changed

+100
-98
lines changed

12 files changed

+100
-98
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1717

1818
## [1.11.0-0.30b0](https://github.com/open-telemetry/opentelemetry-python/releases/tag/v1.11.0-0.30b0) - 2022-04-18
1919

20+
- Rename API Measurement for async instruments to Observation
21+
([#2617](https://github.com/open-telemetry/opentelemetry-python/pull/2617))
2022
- Add support for zero or more callbacks
2123
([#2602](https://github.com/open-telemetry/opentelemetry-python/pull/2602))
2224
- Fix parsing of trace flags when extracting traceparent
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
opentelemetry._metrics.measurement
1+
opentelemetry._metrics.observation
22
==================================
33

4-
.. automodule:: opentelemetry._metrics.measurement
4+
.. automodule:: opentelemetry._metrics.observation
55
:members:
66
:undoc-members:
77
:show-inheritance:

docs/api/metrics.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Submodules
1414
.. toctree::
1515

1616
metrics.instrument
17-
metrics.measurement
17+
metrics.observation
1818

1919
Module contents
2020
---------------

docs/examples/metrics/example.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from typing import Iterable
22

33
from opentelemetry._metrics import get_meter_provider, set_meter_provider
4-
from opentelemetry._metrics.measurement import Measurement
4+
from opentelemetry._metrics.observation import Observation
55
from opentelemetry.exporter.otlp.proto.grpc._metric_exporter import (
66
OTLPMetricExporter,
77
)
@@ -14,16 +14,16 @@
1414
set_meter_provider(provider)
1515

1616

17-
def observable_counter_func() -> Iterable[Measurement]:
18-
yield Measurement(1, {})
17+
def observable_counter_func() -> Iterable[Observation]:
18+
yield Observation(1, {})
1919

2020

21-
def observable_up_down_counter_func() -> Iterable[Measurement]:
22-
yield Measurement(-10, {})
21+
def observable_up_down_counter_func() -> Iterable[Observation]:
22+
yield Observation(-10, {})
2323

2424

25-
def observable_gauge_func() -> Iterable[Measurement]:
26-
yield Measurement(9, {})
25+
def observable_gauge_func() -> Iterable[Observation]:
26+
yield Observation(9, {})
2727

2828

2929
meter = get_meter_provider().get_meter("getting-started", "0.1.2")

docs/getting_started/metrics_example.py

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

2020
from opentelemetry._metrics import get_meter_provider, set_meter_provider
21-
from opentelemetry._metrics.measurement import Measurement
21+
from opentelemetry._metrics.observation import Observation
2222
from opentelemetry.sdk._metrics import MeterProvider
2323
from opentelemetry.sdk._metrics.export import (
2424
ConsoleMetricExporter,
@@ -31,16 +31,16 @@
3131
set_meter_provider(provider)
3232

3333

34-
def observable_counter_func() -> Iterable[Measurement]:
35-
yield Measurement(1, {})
34+
def observable_counter_func() -> Iterable[Observation]:
35+
yield Observation(1, {})
3636

3737

38-
def observable_up_down_counter_func() -> Iterable[Measurement]:
39-
yield Measurement(-10, {})
38+
def observable_up_down_counter_func() -> Iterable[Observation]:
39+
yield Observation(-10, {})
4040

4141

42-
def observable_gauge_func() -> Iterable[Measurement]:
43-
yield Measurement(9, {})
42+
def observable_gauge_func() -> Iterable[Observation]:
43+
yield Observation(9, {})
4444

4545

4646
meter = get_meter_provider().get_meter("getting-started", "0.1.2")

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

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ def create_counter(
227227
228228
Args:
229229
name: The name of the instrument to be created
230-
unit: The unit for measurements this instrument reports. For
230+
unit: The unit for observations this instrument reports. For
231231
example, ``By`` for bytes. UCUM units are recommended.
232232
description: A description for this instrument and what it measures.
233233
"""
@@ -240,7 +240,7 @@ def create_up_down_counter(
240240
241241
Args:
242242
name: The name of the instrument to be created
243-
unit: The unit for measurements this instrument reports. For
243+
unit: The unit for observations this instrument reports. For
244244
example, ``By`` for bytes. UCUM units are recommended.
245245
description: A description for this instrument and what it measures.
246246
"""
@@ -253,23 +253,23 @@ def create_observable_counter(
253253
254254
An observable counter observes a monotonically increasing count by
255255
calling provided callbacks which returns multiple
256-
:class:`~opentelemetry._metrics.measurement.Measurement`.
256+
:class:`~opentelemetry._metrics.observation.Observation`.
257257
258258
For example, an observable counter could be used to report system CPU
259259
time periodically. Here is a basic implementation::
260260
261-
def cpu_time_callback() -> Iterable[Measurement]:
262-
measurements = []
261+
def cpu_time_callback() -> Iterable[Observation]:
262+
observations = []
263263
with open("/proc/stat") as procstat:
264264
procstat.readline() # skip the first line
265265
for line in procstat:
266266
if not line.startswith("cpu"): break
267267
cpu, *states = line.split()
268-
measurements.append(Measurement(int(states[0]) // 100, {"cpu": cpu, "state": "user"}))
269-
measurements.append(Measurement(int(states[1]) // 100, {"cpu": cpu, "state": "nice"}))
270-
measurements.append(Measurement(int(states[2]) // 100, {"cpu": cpu, "state": "system"}))
268+
observations.append(Observation(int(states[0]) // 100, {"cpu": cpu, "state": "user"}))
269+
observations.append(Observation(int(states[1]) // 100, {"cpu": cpu, "state": "nice"}))
270+
observations.append(Observation(int(states[2]) // 100, {"cpu": cpu, "state": "system"}))
271271
# ... other states
272-
return measurements
272+
return observations
273273
274274
meter.create_observable_counter(
275275
"system.cpu.time",
@@ -281,34 +281,34 @@ def cpu_time_callback() -> Iterable[Measurement]:
281281
To reduce memory usage, you can use generator callbacks instead of
282282
building the full list::
283283
284-
def cpu_time_callback() -> Iterable[Measurement]:
284+
def cpu_time_callback() -> Iterable[Observation]:
285285
with open("/proc/stat") as procstat:
286286
procstat.readline() # skip the first line
287287
for line in procstat:
288288
if not line.startswith("cpu"): break
289289
cpu, *states = line.split()
290-
yield Measurement(int(states[0]) // 100, {"cpu": cpu, "state": "user"})
291-
yield Measurement(int(states[1]) // 100, {"cpu": cpu, "state": "nice"})
290+
yield Observation(int(states[0]) // 100, {"cpu": cpu, "state": "user"})
291+
yield Observation(int(states[1]) // 100, {"cpu": cpu, "state": "nice"})
292292
# ... other states
293293
294294
Alternatively, you can pass a sequence of generators directly instead
295295
of a sequence of callbacks, which each should return iterables of
296-
:class:`~opentelemetry._metrics.measurement.Measurement`::
296+
:class:`~opentelemetry._metrics.observation.Observation`::
297297
298-
def cpu_time_callback(states_to_include: set[str]) -> Iterable[Iterable[Measurement]]:
298+
def cpu_time_callback(states_to_include: set[str]) -> Iterable[Iterable[Observation]]:
299299
while True:
300-
measurements = []
300+
observations = []
301301
with open("/proc/stat") as procstat:
302302
procstat.readline() # skip the first line
303303
for line in procstat:
304304
if not line.startswith("cpu"): break
305305
cpu, *states = line.split()
306306
if "user" in states_to_include:
307-
measurements.append(Measurement(int(states[0]) // 100, {"cpu": cpu, "state": "user"}))
307+
observations.append(Observation(int(states[0]) // 100, {"cpu": cpu, "state": "user"}))
308308
if "nice" in states_to_include:
309-
measurements.append(Measurement(int(states[1]) // 100, {"cpu": cpu, "state": "nice"}))
309+
observations.append(Observation(int(states[1]) // 100, {"cpu": cpu, "state": "nice"}))
310310
# ... other states
311-
yield measurements
311+
yield observations
312312
313313
meter.create_observable_counter(
314314
"system.cpu.time",
@@ -320,11 +320,11 @@ def cpu_time_callback(states_to_include: set[str]) -> Iterable[Iterable[Measurem
320320
Args:
321321
name: The name of the instrument to be created
322322
callbacks: A sequence of callbacks that return an iterable of
323-
:class:`~opentelemetry._metrics.measurement.Measurement`.
323+
:class:`~opentelemetry._metrics.observation.Observation`.
324324
Alternatively, can be a sequence of generators that each yields
325325
iterables of
326-
:class:`~opentelemetry._metrics.measurement.Measurement`.
327-
unit: The unit for measurements this instrument reports. For
326+
:class:`~opentelemetry._metrics.observation.Observation`.
327+
unit: The unit for observations this instrument reports. For
328328
example, ``By`` for bytes. UCUM units are recommended.
329329
description: A description for this instrument and what it measures.
330330
"""
@@ -335,7 +335,7 @@ def create_histogram(self, name, unit="", description="") -> Histogram:
335335
336336
Args:
337337
name: The name of the instrument to be created
338-
unit: The unit for measurements this instrument reports. For
338+
unit: The unit for observations this instrument reports. For
339339
example, ``By`` for bytes. UCUM units are recommended.
340340
description: A description for this instrument and what it measures.
341341
"""
@@ -349,10 +349,10 @@ def create_observable_gauge(
349349
Args:
350350
name: The name of the instrument to be created
351351
callbacks: A sequence of callbacks that return an iterable of
352-
:class:`~opentelemetry._metrics.measurement.Measurement`.
352+
:class:`~opentelemetry._metrics.observation.Observation`.
353353
Alternatively, can be a generator that yields iterables of
354-
:class:`~opentelemetry._metrics.measurement.Measurement`.
355-
unit: The unit for measurements this instrument reports. For
354+
:class:`~opentelemetry._metrics.observation.Observation`.
355+
unit: The unit for observations this instrument reports. For
356356
example, ``By`` for bytes. UCUM units are recommended.
357357
description: A description for this instrument and what it measures.
358358
"""
@@ -366,10 +366,10 @@ def create_observable_up_down_counter(
366366
Args:
367367
name: The name of the instrument to be created
368368
callbacks: A sequence of callbacks that return an iterable of
369-
:class:`~opentelemetry._metrics.measurement.Measurement`.
369+
:class:`~opentelemetry._metrics.observation.Observation`.
370370
Alternatively, can be a generator that yields iterables of
371-
:class:`~opentelemetry._metrics.measurement.Measurement`.
372-
unit: The unit for measurements this instrument reports. For
371+
:class:`~opentelemetry._metrics.observation.Observation`.
372+
unit: The unit for observations this instrument reports. For
373373
example, ``By`` for bytes. UCUM units are recommended.
374374
description: A description for this instrument and what it measures.
375375
"""

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@
3030

3131
# pylint: disable=unused-import; needed for typing and sphinx
3232
from opentelemetry import _metrics as metrics
33-
from opentelemetry._metrics.measurement import Measurement
33+
from opentelemetry._metrics.observation import Observation
3434

3535
InstrumentT = TypeVar("InstrumentT", bound="Instrument")
3636
CallbackT = Union[
37-
Callable[[], Iterable[Measurement]],
38-
Generator[Iterable[Measurement], None, None],
37+
Callable[[], Iterable[Observation]],
38+
Generator[Iterable[Observation], None, None],
3939
]
4040

4141

opentelemetry-api/src/opentelemetry/_metrics/measurement.py renamed to opentelemetry-api/src/opentelemetry/_metrics/observation.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from opentelemetry.util.types import Attributes
1818

1919

20-
class Measurement:
20+
class Observation:
2121
"""A measurement observed in an asynchronous instrument
2222
2323
Return/yield instances of this class from asynchronous instrument callbacks.
@@ -43,10 +43,10 @@ def attributes(self) -> Attributes:
4343

4444
def __eq__(self, other: object) -> bool:
4545
return (
46-
isinstance(other, Measurement)
46+
isinstance(other, Observation)
4747
and self.value == other.value
4848
and self.attributes == other.attributes
4949
)
5050

5151
def __repr__(self) -> str:
52-
return f"Measurement(value={self.value}, attributes={self.attributes})"
52+
return f"Observation(value={self.value}, attributes={self.attributes})"

opentelemetry-api/tests/metrics/test_measurement.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,33 +14,33 @@
1414

1515
from unittest import TestCase
1616

17-
from opentelemetry._metrics.measurement import Measurement
17+
from opentelemetry._metrics.observation import Observation
1818

1919

20-
class TestMeasurement(TestCase):
20+
class TestObservation(TestCase):
2121
def test_measurement_init(self):
2222
try:
2323
# int
24-
Measurement(321, {"hello": "world"})
24+
Observation(321, {"hello": "world"})
2525

2626
# float
27-
Measurement(321.321, {"hello": "world"})
27+
Observation(321.321, {"hello": "world"})
2828
except Exception: # pylint: disable=broad-except
2929
self.fail(
30-
"Unexpected exception raised when instantiating Measurement"
30+
"Unexpected exception raised when instantiating Observation"
3131
)
3232

3333
def test_measurement_equality(self):
3434
self.assertEqual(
35-
Measurement(321, {"hello": "world"}),
36-
Measurement(321, {"hello": "world"}),
35+
Observation(321, {"hello": "world"}),
36+
Observation(321, {"hello": "world"}),
3737
)
3838

3939
self.assertNotEqual(
40-
Measurement(321, {"hello": "world"}),
41-
Measurement(321.321, {"hello": "world"}),
40+
Observation(321, {"hello": "world"}),
41+
Observation(321.321, {"hello": "world"}),
4242
)
4343
self.assertNotEqual(
44-
Measurement(321, {"baz": "world"}),
45-
Measurement(321, {"hello": "world"}),
44+
Observation(321, {"baz": "world"}),
45+
Observation(321, {"hello": "world"}),
4646
)

0 commit comments

Comments
 (0)