Skip to content

Commit

Permalink
Move Advisory types to metrics _internal and export the public one
Browse files Browse the repository at this point in the history
  • Loading branch information
xrmx committed Jan 21, 2025
1 parent 0b6fc1f commit 5cd650a
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 39 deletions.
2 changes: 1 addition & 1 deletion docs/examples/metrics/instruments/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
)
from opentelemetry.metrics import (
CallbackOptions,
MetricsHistogramAdvisory,
Observation,
get_meter_provider,
set_meter_provider,
)
from opentelemetry.sdk.metrics import MeterProvider
from opentelemetry.sdk.metrics.export import PeriodicExportingMetricReader
from opentelemetry.util.types import MetricsHistogramAdvisory

exporter = OTLPMetricExporter(insecure=True)
reader = PeriodicExportingMetricReader(exporter)
Expand Down
4 changes: 4 additions & 0 deletions opentelemetry-api/src/opentelemetry/metrics/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@
Counter,
Histogram,
Instrument,
MetricsCommonAdvisory,
MetricsHistogramAdvisory,
NoOpCounter,
NoOpHistogram,
NoOpObservableCounter,
Expand Down Expand Up @@ -129,4 +131,6 @@
"Observation",
"CallbackT",
"NoOpMeter",
"MetricsCommonAdvisory",
"MetricsHistogramAdvisory",
]
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
Counter,
Gauge,
Histogram,
MetricsCommonAdvisory,
MetricsHistogramAdvisory,
NoOpCounter,
NoOpGauge,
NoOpHistogram,
Expand All @@ -65,6 +67,7 @@
ObservableGauge,
ObservableUpDownCounter,
UpDownCounter,
_MetricsInstrumentAdvisory,
_ProxyCounter,
_ProxyGauge,
_ProxyHistogram,
Expand All @@ -77,9 +80,6 @@
from opentelemetry.util._providers import _load_provider
from opentelemetry.util.types import (
Attributes,
MetricsCommonAdvisory,
MetricsHistogramAdvisory,
MetricsInstrumentAdvisory,
)

_logger = getLogger(__name__)
Expand Down Expand Up @@ -188,7 +188,7 @@ class _InstrumentRegistrationStatus:
instrument_id: str
already_registered: bool
conflict: bool
current_advisory: Optional[MetricsInstrumentAdvisory]
current_advisory: Optional[_MetricsInstrumentAdvisory]


class Meter(ABC):
Expand All @@ -209,7 +209,7 @@ def __init__(
self._version = version
self._schema_url = schema_url
self._instrument_ids: Dict[
str, Optional[MetricsInstrumentAdvisory]
str, Optional[_MetricsInstrumentAdvisory]
] = {}
self._instrument_ids_lock = Lock()

Expand Down Expand Up @@ -240,7 +240,7 @@ def _register_instrument(
type_: type,
unit: str,
description: str,
advisory: Optional[MetricsInstrumentAdvisory] = None,
advisory: Optional[_MetricsInstrumentAdvisory] = None,
) -> _InstrumentRegistrationStatus:
"""
Register an instrument with the name, type, unit and description as
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,6 @@
from opentelemetry.metrics._internal.observation import Observation
from opentelemetry.util.types import (
Attributes,
MetricsCommonAdvisory,
MetricsHistogramAdvisory,
MetricsInstrumentAdvisory,
)

_logger = getLogger(__name__)
Expand All @@ -49,6 +46,21 @@
_unit_regex = re_compile(r"[\x00-\x7F]{0,63}")


@dataclass(frozen=True)
class MetricsHistogramAdvisory:
explicit_bucket_boundaries: Optional[Sequence[float]] = None


@dataclass(frozen=True)
class MetricsCommonAdvisory:
pass


_MetricsInstrumentAdvisory = Union[
MetricsCommonAdvisory, MetricsHistogramAdvisory
]


@dataclass(frozen=True)
class CallbackOptions:
"""Options for the callback
Expand Down Expand Up @@ -78,7 +90,7 @@ def __init__(
name: str,
unit: str = "",
description: str = "",
advisory: Optional[MetricsInstrumentAdvisory] = None,
advisory: Optional[_MetricsInstrumentAdvisory] = None,
) -> None:
pass

Expand Down Expand Up @@ -124,7 +136,7 @@ def __init__(
name: str,
unit: str = "",
description: str = "",
advisory: Optional[MetricsInstrumentAdvisory] = None,
advisory: Optional[_MetricsInstrumentAdvisory] = None,
) -> None:
self._name = name
self._unit = unit
Expand Down Expand Up @@ -152,7 +164,7 @@ def __init__(
callbacks: Optional[Sequence[CallbackT]] = None,
unit: str = "",
description: str = "",
advisory: Optional[MetricsInstrumentAdvisory] = None,
advisory: Optional[_MetricsInstrumentAdvisory] = None,
) -> None:
super().__init__(name, unit, description, advisory=advisory)
self._callbacks = callbacks
Expand All @@ -172,7 +184,7 @@ def __init__(
callbacks: Optional[Sequence[CallbackT]] = None,
unit: str = "",
description: str = "",
advisory: Optional[MetricsInstrumentAdvisory] = None,
advisory: Optional[_MetricsInstrumentAdvisory] = None,
) -> None:
super().__init__(
name, unit=unit, description=description, advisory=advisory
Expand Down
16 changes: 0 additions & 16 deletions opentelemetry-api/src/opentelemetry/util/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from dataclasses import dataclass
from typing import Mapping, Optional, Sequence, Tuple, Union

# This is the implementation of the "Any" type as specified by the specifications of OpenTelemetry data model for logs.
Expand Down Expand Up @@ -56,18 +55,3 @@
],
...,
]


@dataclass
class MetricsHistogramAdvisory:
explicit_bucket_boundaries: Optional[Sequence[float]] = None


@dataclass
class MetricsCommonAdvisory:
pass


MetricsInstrumentAdvisory = Union[
MetricsCommonAdvisory, MetricsHistogramAdvisory
]
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from opentelemetry.metrics import Histogram as APIHistogram
from opentelemetry.metrics import Meter as APIMeter
from opentelemetry.metrics import MeterProvider as APIMeterProvider
from opentelemetry.metrics import NoOpMeter
from opentelemetry.metrics import MetricsHistogramAdvisory, NoOpMeter
from opentelemetry.metrics import ObservableCounter as APIObservableCounter
from opentelemetry.metrics import ObservableGauge as APIObservableGauge
from opentelemetry.metrics import (
Expand Down Expand Up @@ -66,7 +66,6 @@
from opentelemetry.util._once import Once
from opentelemetry.util.types import (
Attributes,
MetricsHistogramAdvisory,
)

_logger = getLogger(__name__)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
# This kind of import is needed to avoid Sphinx errors.
import opentelemetry.sdk.metrics
from opentelemetry.context import Context, get_current
from opentelemetry.metrics import CallbackT
from opentelemetry.metrics import CallbackT, MetricsHistogramAdvisory
from opentelemetry.metrics import Counter as APICounter
from opentelemetry.metrics import Histogram as APIHistogram
from opentelemetry.metrics import ObservableCounter as APIObservableCounter
Expand All @@ -34,7 +34,6 @@
from opentelemetry.metrics._internal.instrument import CallbackOptions
from opentelemetry.sdk.metrics._internal.measurement import Measurement
from opentelemetry.sdk.util.instrumentation import InstrumentationScope
from opentelemetry.util.types import MetricsInstrumentAdvisory

_logger = getLogger(__name__)

Expand Down Expand Up @@ -227,7 +226,7 @@ def __init__(
measurement_consumer: "opentelemetry.sdk.metrics.MeasurementConsumer",
unit: str = "",
description: str = "",
advisory: Optional[MetricsInstrumentAdvisory] = None,
advisory: Optional[MetricsHistogramAdvisory] = None,
):
super().__init__(
name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@

from unittest import TestCase

from opentelemetry.metrics import MetricsHistogramAdvisory
from opentelemetry.sdk.metrics import MeterProvider
from opentelemetry.sdk.metrics.export import InMemoryMetricReader
from opentelemetry.sdk.metrics.view import (
ExplicitBucketHistogramAggregation,
View,
)
from opentelemetry.util.types import MetricsHistogramAdvisory


class TestHistogramAdvisory(TestCase):
Expand Down
3 changes: 2 additions & 1 deletion opentelemetry-sdk/tests/metrics/test_aggregation.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from unittest.mock import Mock

from opentelemetry.context import Context
from opentelemetry.metrics import MetricsHistogramAdvisory
from opentelemetry.sdk.metrics._internal.aggregation import (
_ExplicitBucketHistogramAggregation,
_LastValueAggregation,
Expand Down Expand Up @@ -51,7 +52,7 @@
LastValueAggregation,
SumAggregation,
)
from opentelemetry.util.types import Attributes, MetricsHistogramAdvisory
from opentelemetry.util.types import Attributes


def measurement(
Expand Down
3 changes: 1 addition & 2 deletions opentelemetry-sdk/tests/metrics/test_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from unittest.mock import MagicMock, Mock, patch

from opentelemetry.attributes import BoundedAttributes
from opentelemetry.metrics import NoOpMeter
from opentelemetry.metrics import MetricsHistogramAdvisory, NoOpMeter
from opentelemetry.sdk.environment_variables import OTEL_SDK_DISABLED
from opentelemetry.sdk.metrics import (
Counter,
Expand All @@ -46,7 +46,6 @@
from opentelemetry.sdk.resources import Resource
from opentelemetry.test import TestCase
from opentelemetry.test.concurrency_test import ConcurrencyTestBase, MockFunc
from opentelemetry.util.types import MetricsHistogramAdvisory


class DummyMetricReader(MetricReader):
Expand Down

0 comments on commit 5cd650a

Please sign in to comment.