Skip to content

Commit

Permalink
Rename Measurement to Observation
Browse files Browse the repository at this point in the history
  • Loading branch information
ocelotl committed Apr 18, 2022
1 parent 1b00f31 commit 4e90ca3
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 53 deletions.
6 changes: 3 additions & 3 deletions opentelemetry-api/src/opentelemetry/_metrics/instrument.py
Original file line number Diff line number Diff line change
Expand Up @@ -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],
]


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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})"
22 changes: 11 additions & 11 deletions opentelemetry-api/tests/metrics/test_measurement.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"}),
)
42 changes: 21 additions & 21 deletions opentelemetry-sdk/tests/metrics/integration_test/test_cpu_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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"}
)

Expand All @@ -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 = []
Expand All @@ -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"},
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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"})
Expand Down
26 changes: 13 additions & 13 deletions opentelemetry-sdk/tests/metrics/test_instrument.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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),
]


Expand Down

0 comments on commit 4e90ca3

Please sign in to comment.