Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
from time import time_ns
from typing import Dict, List, Optional, Sequence

from opentelemetry.metrics import Instrument
from opentelemetry.sdk.metrics._internal.aggregation import (
Aggregation,
AggregationTemporality,
DefaultAggregation,
_Aggregation,
_SumAggregation,
)
from opentelemetry.sdk.metrics._internal.instrument import _Instrument
from opentelemetry.sdk.metrics._internal.measurement import Measurement
from opentelemetry.sdk.metrics._internal.point import DataPointT
from opentelemetry.sdk.metrics._internal.view import View
Expand All @@ -37,7 +37,7 @@ class _ViewInstrumentMatch:
def __init__(
self,
view: View,
instrument: Instrument,
instrument: _Instrument,
instrument_class_aggregation: Dict[type, Aggregation],
):
self._view = view
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
Asynchronous,
Counter,
Histogram,
Instrument,
ObservableCounter,
ObservableGauge,
ObservableUpDownCounter,
Expand All @@ -59,6 +58,7 @@
from opentelemetry.sdk.metrics._internal.exponential_histogram.mapping.logarithm_mapping import (
LogarithmMapping,
)
from opentelemetry.sdk.metrics._internal.instrument import _Instrument
from opentelemetry.sdk.metrics._internal.measurement import Measurement
from opentelemetry.sdk.metrics._internal.point import Buckets as BucketsPoint
from opentelemetry.sdk.metrics._internal.point import (
Expand Down Expand Up @@ -1200,7 +1200,7 @@ class Aggregation(ABC):
@abstractmethod
def _create_aggregation(
self,
instrument: Instrument,
instrument: _Instrument,
attributes: Attributes,
reservoir_factory: Callable[
[Type[_Aggregation]], ExemplarReservoirBuilder
Expand Down Expand Up @@ -1231,7 +1231,7 @@ class DefaultAggregation(Aggregation):

def _create_aggregation(
self,
instrument: Instrument,
instrument: _Instrument,
attributes: Attributes,
reservoir_factory: Callable[
[Type[_Aggregation]], ExemplarReservoirBuilder
Expand Down Expand Up @@ -1323,7 +1323,7 @@ def __init__(

def _create_aggregation(
self,
instrument: Instrument,
instrument: _Instrument,
attributes: Attributes,
reservoir_factory: Callable[
[Type[_Aggregation]], ExemplarReservoirBuilder
Expand Down Expand Up @@ -1372,7 +1372,7 @@ def __init__(

def _create_aggregation(
self,
instrument: Instrument,
instrument: _Instrument,
attributes: Attributes,
reservoir_factory: Callable[
[Type[_Aggregation]], ExemplarReservoirBuilder
Expand Down Expand Up @@ -1416,7 +1416,7 @@ class SumAggregation(Aggregation):

def _create_aggregation(
self,
instrument: Instrument,
instrument: _Instrument,
attributes: Attributes,
reservoir_factory: Callable[
[Type[_Aggregation]], ExemplarReservoirBuilder
Expand Down Expand Up @@ -1450,7 +1450,7 @@ class LastValueAggregation(Aggregation):

def _create_aggregation(
self,
instrument: Instrument,
instrument: _Instrument,
attributes: Attributes,
reservoir_factory: Callable[
[Type[_Aggregation]], ExemplarReservoirBuilder
Expand All @@ -1468,7 +1468,7 @@ class DropAggregation(Aggregation):

def _create_aggregation(
self,
instrument: Instrument,
instrument: _Instrument,
attributes: Attributes,
reservoir_factory: Callable[
[Type[_Aggregation]], ExemplarReservoirBuilder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,21 @@

from logging import getLogger
from time import time_ns
from typing import TYPE_CHECKING, Generator, Iterable, List, Sequence, Union
from typing import (
TYPE_CHECKING,
Generator,
Iterable,
List,
Protocol,
Sequence,
Union,
cast,
runtime_checkable,
)

# This kind of import is needed to avoid Sphinx errors.
from opentelemetry.context import Context, get_current
from opentelemetry.metrics import CallbackT
from opentelemetry.metrics import Asynchronous, CallbackT, Synchronous
from opentelemetry.metrics import Counter as APICounter
from opentelemetry.metrics import Histogram as APIHistogram
from opentelemetry.metrics import ObservableCounter as APIObservableCounter
Expand Down Expand Up @@ -55,7 +65,15 @@
)


class _Synchronous:
@runtime_checkable
class _Instrument(Protocol):
name: str
unit: str
description: str
instrumentation_scope: InstrumentationScope


class _Synchronous(_Instrument, Synchronous):
def __init__(
self,
name: str,
Expand All @@ -79,7 +97,7 @@ def __init__(

name = result["name"]
unit = result["unit"]
description = result["description"]
description = cast(str, result["description"])

self.name = name.lower()
self.unit = unit
Expand All @@ -93,13 +111,13 @@ def _is_enabled(self) -> bool:
return self._meter_config is None or self._meter_config.is_enabled


class _Asynchronous:
class _Asynchronous(_Instrument, Asynchronous):
def __init__(
self,
name: str,
instrumentation_scope: InstrumentationScope,
measurement_consumer: MeasurementConsumer,
callbacks: Iterable[CallbackT] | None = None,
callbacks: Sequence[CallbackT] | None = None,
unit: str = "",
description: str = "",
*,
Expand All @@ -118,7 +136,7 @@ def __init__(

name = result["name"]
unit = result["unit"]
description = result["description"]
description = cast(str, result["description"])

self.name = name.lower()
self.unit = unit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,17 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import annotations

from dataclasses import dataclass
from typing import Union
from typing import TYPE_CHECKING, Union

from opentelemetry.context import Context
from opentelemetry.metrics import Instrument
from opentelemetry.util.types import Attributes

if TYPE_CHECKING:
from opentelemetry.sdk.metrics._internal.instrument import _Instrument


@dataclass(frozen=True)
class Measurement:
Expand All @@ -35,6 +39,6 @@ class Measurement:

value: Union[int, float]
time_unix_nano: int
instrument: Instrument
instrument: _Instrument
context: Context
attributes: Attributes = None
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
from opentelemetry.metrics import (
Asynchronous,
Counter,
Instrument,
ObservableCounter,
)
from opentelemetry.sdk.metrics._internal._view_instrument_match import (
Expand All @@ -36,6 +35,7 @@
_LastValueAggregation,
_SumAggregation,
)
from opentelemetry.sdk.metrics._internal.instrument import _Instrument
from opentelemetry.sdk.metrics._internal.measurement import Measurement
from opentelemetry.sdk.metrics._internal.point import (
ExponentialHistogram,
Expand Down Expand Up @@ -70,13 +70,13 @@ def __init__(
self._lock = RLock()
self._sdk_config = sdk_config
self._instrument_view_instrument_matches: Dict[
Instrument, List[_ViewInstrumentMatch]
_Instrument, List[_ViewInstrumentMatch]
] = {}
self._instrument_class_temporality = instrument_class_temporality
self._instrument_class_aggregation = instrument_class_aggregation

def _get_or_init_view_instrument_match(
self, instrument: Instrument
self, instrument: _Instrument
) -> List[_ViewInstrumentMatch]:
# Optimistically get the relevant views for the given instrument. Once set for a given
# instrument, the mapping will never change
Expand Down Expand Up @@ -253,7 +253,7 @@ def collect(self) -> Optional[MetricsData]:

def _handle_view_instrument_match(
self,
instrument: Instrument,
instrument: _Instrument,
view_instrument_matches: List["_ViewInstrumentMatch"],
) -> None:
for view in self._sdk_config.views:
Expand Down Expand Up @@ -292,7 +292,7 @@ def _handle_view_instrument_match(

@staticmethod
def _check_view_instrument_compatibility(
view: View, instrument: Instrument
view: View, instrument: _Instrument
) -> bool:
"""
Checks if a view and an instrument are compatible.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
ExemplarReservoirBuilder,
SimpleFixedSizeExemplarReservoir,
)
from opentelemetry.sdk.metrics._internal.instrument import _Instrument

_logger = getLogger(__name__)

Expand Down Expand Up @@ -164,7 +165,7 @@ def __init__(

# pylint: disable=too-many-return-statements
# pylint: disable=too-many-branches
def _match(self, instrument: Instrument) -> bool:
def _match(self, instrument: _Instrument) -> bool:
if self._instrument_type is not None:
if not isinstance(instrument, self._instrument_type):
return False
Expand Down
Loading