From 4e90ca34a7031353566e5047d9018e866451444c Mon Sep 17 00:00:00 2001 From: Diego Hurtado Date: Mon, 18 Apr 2022 14:31:03 -0600 Subject: [PATCH] Rename Measurement to Observation Fixes #2451 --- .../src/opentelemetry/_metrics/instrument.py | 6 +-- .../{measurement.py => observation.py} | 6 +-- .../tests/metrics/test_measurement.py | 22 +++++----- .../metrics/integration_test/test_cpu_time.py | 42 +++++++++---------- .../metrics/test_in_memory_metric_reader.py | 4 +- .../tests/metrics/test_instrument.py | 26 ++++++------ 6 files changed, 53 insertions(+), 53 deletions(-) rename opentelemetry-api/src/opentelemetry/_metrics/{measurement.py => observation.py} (92%) diff --git a/opentelemetry-api/src/opentelemetry/_metrics/instrument.py b/opentelemetry-api/src/opentelemetry/_metrics/instrument.py index f707b05e9fd..e5ab7fa4959 100644 --- a/opentelemetry-api/src/opentelemetry/_metrics/instrument.py +++ b/opentelemetry-api/src/opentelemetry/_metrics/instrument.py @@ -30,12 +30,12 @@ # pylint: disable=unused-import; needed for typing and sphinx from opentelemetry import _metrics as metrics -from opentelemetry._metrics.measurement import Measurement +from opentelemetry._metrics.observation import Observation InstrumentT = TypeVar("InstrumentT", bound="Instrument") CallbackT = Union[ - Callable[[], Iterable[Measurement]], - Generator[Iterable[Measurement], None, None], + Callable[[], Iterable[Observation]], + Generator[Iterable[Observation], None, None], ] diff --git a/opentelemetry-api/src/opentelemetry/_metrics/measurement.py b/opentelemetry-api/src/opentelemetry/_metrics/observation.py similarity index 92% rename from opentelemetry-api/src/opentelemetry/_metrics/measurement.py rename to opentelemetry-api/src/opentelemetry/_metrics/observation.py index e8fd9725bb8..7aa24e3342d 100644 --- a/opentelemetry-api/src/opentelemetry/_metrics/measurement.py +++ b/opentelemetry-api/src/opentelemetry/_metrics/observation.py @@ -17,7 +17,7 @@ from opentelemetry.util.types import Attributes -class Measurement: +class Observation: """A measurement observed in an asynchronous instrument Return/yield instances of this class from asynchronous instrument callbacks. @@ -43,10 +43,10 @@ def attributes(self) -> Attributes: def __eq__(self, other: object) -> bool: return ( - isinstance(other, Measurement) + isinstance(other, Observation) and self.value == other.value and self.attributes == other.attributes ) def __repr__(self) -> str: - return f"Measurement(value={self.value}, attributes={self.attributes})" + return f"Observation(value={self.value}, attributes={self.attributes})" diff --git a/opentelemetry-api/tests/metrics/test_measurement.py b/opentelemetry-api/tests/metrics/test_measurement.py index 8791bc1a2fa..295c5b9a6be 100644 --- a/opentelemetry-api/tests/metrics/test_measurement.py +++ b/opentelemetry-api/tests/metrics/test_measurement.py @@ -14,33 +14,33 @@ from unittest import TestCase -from opentelemetry._metrics.measurement import Measurement +from opentelemetry._metrics.observation import Observation -class TestMeasurement(TestCase): +class TestObservation(TestCase): def test_measurement_init(self): try: # int - Measurement(321, {"hello": "world"}) + Observation(321, {"hello": "world"}) # float - Measurement(321.321, {"hello": "world"}) + Observation(321.321, {"hello": "world"}) except Exception: # pylint: disable=broad-except self.fail( - "Unexpected exception raised when instantiating Measurement" + "Unexpected exception raised when instantiating Observation" ) def test_measurement_equality(self): self.assertEqual( - Measurement(321, {"hello": "world"}), - Measurement(321, {"hello": "world"}), + Observation(321, {"hello": "world"}), + Observation(321, {"hello": "world"}), ) self.assertNotEqual( - Measurement(321, {"hello": "world"}), - Measurement(321.321, {"hello": "world"}), + Observation(321, {"hello": "world"}), + Observation(321.321, {"hello": "world"}), ) self.assertNotEqual( - Measurement(321, {"baz": "world"}), - Measurement(321, {"hello": "world"}), + Observation(321, {"baz": "world"}), + Observation(321, {"hello": "world"}), ) diff --git a/opentelemetry-sdk/tests/metrics/integration_test/test_cpu_time.py b/opentelemetry-sdk/tests/metrics/integration_test/test_cpu_time.py index b21091c3c8f..e8c541c090d 100644 --- a/opentelemetry-sdk/tests/metrics/integration_test/test_cpu_time.py +++ b/opentelemetry-sdk/tests/metrics/integration_test/test_cpu_time.py @@ -18,7 +18,7 @@ from unittest import TestCase from opentelemetry._metrics.instrument import Instrument -from opentelemetry._metrics.measurement import Measurement as APIMeasurement +from opentelemetry._metrics.observation import Observation from opentelemetry.sdk._metrics import MeterProvider from opentelemetry.sdk._metrics.measurement import Measurement @@ -139,38 +139,38 @@ def create_measurements_expected( ] def test_cpu_time_callback(self): - def cpu_time_callback() -> Iterable[APIMeasurement]: + def cpu_time_callback() -> Iterable[Observation]: procstat = io.StringIO(self.procstat_str) procstat.readline() # skip the first line for line in procstat: if not line.startswith("cpu"): break cpu, *states = line.split() - yield APIMeasurement( + yield Observation( int(states[0]) / 100, {"cpu": cpu, "state": "user"} ) - yield APIMeasurement( + yield Observation( int(states[1]) / 100, {"cpu": cpu, "state": "nice"} ) - yield APIMeasurement( + yield Observation( int(states[2]) / 100, {"cpu": cpu, "state": "system"} ) - yield APIMeasurement( + yield Observation( int(states[3]) / 100, {"cpu": cpu, "state": "idle"} ) - yield APIMeasurement( + yield Observation( int(states[4]) / 100, {"cpu": cpu, "state": "iowait"} ) - yield APIMeasurement( + yield Observation( int(states[5]) / 100, {"cpu": cpu, "state": "irq"} ) - yield APIMeasurement( + yield Observation( int(states[6]) / 100, {"cpu": cpu, "state": "softirq"} ) - yield APIMeasurement( + yield Observation( int(states[7]) / 100, {"cpu": cpu, "state": "guest"} ) - yield APIMeasurement( + yield Observation( int(states[8]) / 100, {"cpu": cpu, "state": "guest_nice"} ) @@ -188,7 +188,7 @@ def cpu_time_callback() -> Iterable[APIMeasurement]: def test_cpu_time_generator(self): def cpu_time_generator() -> Generator[ - Iterable[APIMeasurement], None, None + Iterable[Observation], None, None ]: while True: measurements = [] @@ -199,54 +199,54 @@ def cpu_time_generator() -> Generator[ break cpu, *states = line.split() measurements.append( - APIMeasurement( + Observation( int(states[0]) / 100, {"cpu": cpu, "state": "user"}, ) ) measurements.append( - APIMeasurement( + Observation( int(states[1]) / 100, {"cpu": cpu, "state": "nice"}, ) ) measurements.append( - APIMeasurement( + Observation( int(states[2]) / 100, {"cpu": cpu, "state": "system"}, ) ) measurements.append( - APIMeasurement( + Observation( int(states[3]) / 100, {"cpu": cpu, "state": "idle"}, ) ) measurements.append( - APIMeasurement( + Observation( int(states[4]) / 100, {"cpu": cpu, "state": "iowait"}, ) ) measurements.append( - APIMeasurement( + Observation( int(states[5]) / 100, {"cpu": cpu, "state": "irq"} ) ) measurements.append( - APIMeasurement( + Observation( int(states[6]) / 100, {"cpu": cpu, "state": "softirq"}, ) ) measurements.append( - APIMeasurement( + Observation( int(states[7]) / 100, {"cpu": cpu, "state": "guest"}, ) ) measurements.append( - APIMeasurement( + Observation( int(states[8]) / 100, {"cpu": cpu, "state": "guest_nice"}, ) diff --git a/opentelemetry-sdk/tests/metrics/test_in_memory_metric_reader.py b/opentelemetry-sdk/tests/metrics/test_in_memory_metric_reader.py index fcafea38db9..89d39b5e2d4 100644 --- a/opentelemetry-sdk/tests/metrics/test_in_memory_metric_reader.py +++ b/opentelemetry-sdk/tests/metrics/test_in_memory_metric_reader.py @@ -15,7 +15,7 @@ from unittest import TestCase from unittest.mock import Mock -from opentelemetry._metrics.measurement import Measurement +from opentelemetry._metrics.observation import Observation from opentelemetry.sdk._metrics import MeterProvider from opentelemetry.sdk._metrics.export import InMemoryMetricReader from opentelemetry.sdk._metrics.point import ( @@ -70,7 +70,7 @@ def test_integration(self): meter = MeterProvider(metric_readers=[reader]).get_meter("test_meter") counter1 = meter.create_counter("counter1") meter.create_observable_gauge( - "observable_gauge1", callbacks=[lambda: [Measurement(value=12)]] + "observable_gauge1", callbacks=[lambda: [Observation(value=12)]] ) counter1.add(1, {"foo": "1"}) counter1.add(1, {"foo": "2"}) diff --git a/opentelemetry-sdk/tests/metrics/test_instrument.py b/opentelemetry-sdk/tests/metrics/test_instrument.py index b52216560f9..ba792842a1e 100644 --- a/opentelemetry-sdk/tests/metrics/test_instrument.py +++ b/opentelemetry-sdk/tests/metrics/test_instrument.py @@ -15,7 +15,7 @@ from unittest import TestCase from unittest.mock import Mock -from opentelemetry._metrics.measurement import Measurement as APIMeasurement +from opentelemetry._metrics.observation import Observation from opentelemetry.sdk._metrics.instrument import ( Counter, Histogram, @@ -60,33 +60,33 @@ def test_add_non_monotonic(self): def callable_callback_0(): return [ - APIMeasurement(1, attributes=TEST_ATTRIBUTES), - APIMeasurement(2, attributes=TEST_ATTRIBUTES), - APIMeasurement(3, attributes=TEST_ATTRIBUTES), + Observation(1, attributes=TEST_ATTRIBUTES), + Observation(2, attributes=TEST_ATTRIBUTES), + Observation(3, attributes=TEST_ATTRIBUTES), ] def callable_callback_1(): return [ - APIMeasurement(4, attributes=TEST_ATTRIBUTES), - APIMeasurement(5, attributes=TEST_ATTRIBUTES), - APIMeasurement(6, attributes=TEST_ATTRIBUTES), + Observation(4, attributes=TEST_ATTRIBUTES), + Observation(5, attributes=TEST_ATTRIBUTES), + Observation(6, attributes=TEST_ATTRIBUTES), ] def generator_callback_0(): yield [ - APIMeasurement(1, attributes=TEST_ATTRIBUTES), - APIMeasurement(2, attributes=TEST_ATTRIBUTES), - APIMeasurement(3, attributes=TEST_ATTRIBUTES), + Observation(1, attributes=TEST_ATTRIBUTES), + Observation(2, attributes=TEST_ATTRIBUTES), + Observation(3, attributes=TEST_ATTRIBUTES), ] def generator_callback_1(): yield [ - APIMeasurement(4, attributes=TEST_ATTRIBUTES), - APIMeasurement(5, attributes=TEST_ATTRIBUTES), - APIMeasurement(6, attributes=TEST_ATTRIBUTES), + Observation(4, attributes=TEST_ATTRIBUTES), + Observation(5, attributes=TEST_ATTRIBUTES), + Observation(6, attributes=TEST_ATTRIBUTES), ]