Skip to content
Merged
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
165 changes: 126 additions & 39 deletions opentelemetry-api/src/opentelemetry/_metrics/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
# limitations under the License.

# pylint: disable=too-many-ancestors
# type: ignore

"""
The OpenTelemetry metrics API describes the classes used to generate
Expand Down Expand Up @@ -46,9 +45,10 @@
from logging import getLogger
from os import environ
from threading import Lock
from typing import List, Optional, Set, Tuple, cast
from typing import List, Optional, Sequence, Set, Tuple, Union, cast

from opentelemetry._metrics.instrument import (
CallbackT,
Counter,
Histogram,
NoOpCounter,
Expand All @@ -63,7 +63,6 @@
UpDownCounter,
_ProxyCounter,
_ProxyHistogram,
_ProxyInstrument,
_ProxyObservableCounter,
_ProxyObservableGauge,
_ProxyObservableUpDownCounter,
Expand All @@ -78,6 +77,16 @@
_logger = getLogger(__name__)


ProxyInstrumentT = Union[
_ProxyCounter,
_ProxyHistogram,
_ProxyObservableCounter,
_ProxyObservableGauge,
_ProxyObservableUpDownCounter,
_ProxyUpDownCounter,
]


class MeterProvider(ABC):
"""
MeterProvider is the entry point of the API. It provides access to `Meter` instances.
Expand All @@ -87,8 +96,8 @@ class MeterProvider(ABC):
def get_meter(
self,
name: str,
version: str = None,
schema_url: str = None,
version: Optional[str] = None,
schema_url: Optional[str] = None,
) -> "Meter":
"""Returns a `Meter` for use by the given instrumentation library.

Expand Down Expand Up @@ -123,9 +132,9 @@ class NoOpMeterProvider(MeterProvider):

def get_meter(
self,
name,
version=None,
schema_url=None,
name: str,
version: Optional[str] = None,
schema_url: Optional[str] = None,
) -> "Meter":
"""Returns a NoOpMeter."""
super().get_meter(name, version=version, schema_url=schema_url)
Expand All @@ -140,9 +149,9 @@ def __init__(self) -> None:

def get_meter(
self,
name,
version=None,
schema_url=None,
name: str,
version: Optional[str] = None,
schema_url: Optional[str] = None,
) -> "Meter":
with self._lock:
if self._real_meter_provider is not None:
Expand All @@ -168,7 +177,12 @@ class Meter(ABC):
used to produce measurements.
"""

def __init__(self, name: str, version: str = None, schema_url: str = None):
def __init__(
self,
name: str,
version: Optional[str] = None,
schema_url: Optional[str] = None,
) -> None:
super().__init__()
self._name = name
self._version = version
Expand All @@ -177,21 +191,21 @@ def __init__(self, name: str, version: str = None, schema_url: str = None):
self._instrument_ids_lock = Lock()

@property
def name(self):
def name(self) -> str:
"""
The name of the instrumenting module.
"""
return self._name

@property
def version(self):
def version(self) -> Optional[str]:
"""
The version string of the instrumenting library.
"""
return self._version

@property
def schema_url(self):
def schema_url(self) -> Optional[str]:
"""
Specifies the Schema URL of the emitted telemetry
"""
Expand Down Expand Up @@ -225,7 +239,10 @@ def _is_instrument_registered(

@abstractmethod
def create_counter(
self, name: str, unit: str = "", description: str = ""
self,
name: str,
unit: str = "",
description: str = "",
) -> Counter:
"""Creates a `Counter` instrument

Expand All @@ -238,7 +255,10 @@ def create_counter(

@abstractmethod
def create_up_down_counter(
self, name: str, unit: str = "", description: str = ""
self,
name: str,
unit: str = "",
description: str = "",
) -> UpDownCounter:
"""Creates an `UpDownCounter` instrument

Expand All @@ -251,7 +271,11 @@ def create_up_down_counter(

@abstractmethod
def create_observable_counter(
self, name, callbacks=None, unit="", description=""
self,
name: str,
callbacks: Optional[Sequence[CallbackT]] = None,
unit: str = "",
description: str = "",
) -> ObservableCounter:
"""Creates an `ObservableCounter` instrument

Expand Down Expand Up @@ -334,7 +358,12 @@ def cpu_time_callback(states_to_include: set[str]) -> Iterable[Iterable[Observat
"""

@abstractmethod
def create_histogram(self, name, unit="", description="") -> Histogram:
def create_histogram(
self,
name: str,
unit: str = "",
description: str = "",
) -> Histogram:
"""Creates a `opentelemetry._metrics.instrument.Histogram` instrument

Args:
Expand All @@ -346,7 +375,11 @@ def create_histogram(self, name, unit="", description="") -> Histogram:

@abstractmethod
def create_observable_gauge(
self, name, callbacks=None, unit="", description=""
self,
name: str,
callbacks: Optional[Sequence[CallbackT]] = None,
unit: str = "",
description: str = "",
) -> ObservableGauge:
"""Creates an `ObservableGauge` instrument

Expand All @@ -363,7 +396,11 @@ def create_observable_gauge(

@abstractmethod
def create_observable_up_down_counter(
self, name, callbacks=None, unit="", description=""
self,
name: str,
callbacks: Optional[Sequence[CallbackT]] = None,
unit: str = "",
description: str = "",
) -> ObservableUpDownCounter:
"""Creates an `ObservableUpDownCounter` instrument

Expand All @@ -382,13 +419,13 @@ def create_observable_up_down_counter(
class _ProxyMeter(Meter):
def __init__(
self,
name,
version=None,
schema_url=None,
):
name: str,
version: Optional[str] = None,
schema_url: Optional[str] = None,
) -> None:
super().__init__(name, version=version, schema_url=schema_url)
self._lock = Lock()
self._instruments: List[_ProxyInstrument] = []
self._instruments: List[ProxyInstrumentT] = []
self._real_meter: Optional[Meter] = None

def on_set_meter_provider(self, meter_provider: MeterProvider) -> None:
Expand All @@ -408,7 +445,12 @@ def on_set_meter_provider(self, meter_provider: MeterProvider) -> None:
for instrument in self._instruments:
instrument.on_meter_set(real_meter)

def create_counter(self, name, unit="", description="") -> Counter:
def create_counter(
self,
name: str,
unit: str = "",
description: str = "",
) -> Counter:
with self._lock:
if self._real_meter:
return self._real_meter.create_counter(name, unit, description)
Expand All @@ -417,7 +459,10 @@ def create_counter(self, name, unit="", description="") -> Counter:
return proxy

def create_up_down_counter(
self, name, unit="", description=""
self,
name: str,
unit: str = "",
description: str = "",
) -> UpDownCounter:
with self._lock:
if self._real_meter:
Expand All @@ -429,7 +474,11 @@ def create_up_down_counter(
return proxy

def create_observable_counter(
self, name, callbacks=None, unit="", description=""
self,
name: str,
callbacks: Optional[Sequence[CallbackT]] = None,
unit: str = "",
description: str = "",
) -> ObservableCounter:
with self._lock:
if self._real_meter:
Expand All @@ -442,7 +491,12 @@ def create_observable_counter(
self._instruments.append(proxy)
return proxy

def create_histogram(self, name, unit="", description="") -> Histogram:
def create_histogram(
self,
name: str,
unit: str = "",
description: str = "",
) -> Histogram:
with self._lock:
if self._real_meter:
return self._real_meter.create_histogram(
Expand All @@ -453,7 +507,11 @@ def create_histogram(self, name, unit="", description="") -> Histogram:
return proxy

def create_observable_gauge(
self, name, callbacks=None, unit="", description=""
self,
name: str,
callbacks: Optional[Sequence[CallbackT]] = None,
unit: str = "",
description: str = "",
) -> ObservableGauge:
with self._lock:
if self._real_meter:
Expand All @@ -467,7 +525,11 @@ def create_observable_gauge(
return proxy

def create_observable_up_down_counter(
self, name, callbacks=None, unit="", description=""
self,
name: str,
callbacks: Optional[Sequence[CallbackT]] = None,
unit: str = "",
description: str = "",
) -> ObservableUpDownCounter:
with self._lock:
if self._real_meter:
Expand All @@ -490,7 +552,12 @@ class NoOpMeter(Meter):
All operations are no-op.
"""

def create_counter(self, name, unit="", description="") -> Counter:
def create_counter(
self,
name: str,
unit: str = "",
description: str = "",
) -> Counter:
"""Returns a no-op Counter."""
super().create_counter(name, unit=unit, description=description)
if self._is_instrument_registered(
Expand All @@ -507,7 +574,10 @@ def create_counter(self, name, unit="", description="") -> Counter:
return NoOpCounter(name, unit=unit, description=description)

def create_up_down_counter(
self, name, unit="", description=""
self,
name: str,
unit: str = "",
description: str = "",
) -> UpDownCounter:
"""Returns a no-op UpDownCounter."""
super().create_up_down_counter(
Expand All @@ -527,7 +597,11 @@ def create_up_down_counter(
return NoOpUpDownCounter(name, unit=unit, description=description)

def create_observable_counter(
self, name, callbacks=None, unit="", description=""
self,
name: str,
callbacks: Optional[Sequence[CallbackT]] = None,
unit: str = "",
description: str = "",
) -> ObservableCounter:
"""Returns a no-op ObservableCounter."""
super().create_observable_counter(
Expand All @@ -551,7 +625,12 @@ def create_observable_counter(
description=description,
)

def create_histogram(self, name, unit="", description="") -> Histogram:
def create_histogram(
self,
name: str,
unit: str = "",
description: str = "",
) -> Histogram:
"""Returns a no-op Histogram."""
super().create_histogram(name, unit=unit, description=description)
if self._is_instrument_registered(
Expand All @@ -568,7 +647,11 @@ def create_histogram(self, name, unit="", description="") -> Histogram:
return NoOpHistogram(name, unit=unit, description=description)

def create_observable_gauge(
self, name, callbacks=None, unit="", description=""
self,
name: str,
callbacks: Optional[Sequence[CallbackT]] = None,
unit: str = "",
description: str = "",
) -> ObservableGauge:
"""Returns a no-op ObservableGauge."""
super().create_observable_gauge(
Expand All @@ -593,7 +676,11 @@ def create_observable_gauge(
)

def create_observable_up_down_counter(
self, name, callbacks=None, unit="", description=""
self,
name: str,
callbacks: Optional[Sequence[CallbackT]] = None,
unit: str = "",
description: str = "",
) -> ObservableUpDownCounter:
"""Returns a no-op ObservableUpDownCounter."""
super().create_observable_up_down_counter(
Expand Down Expand Up @@ -670,7 +757,7 @@ def get_meter_provider() -> MeterProvider:
if OTEL_PYTHON_METER_PROVIDER not in environ.keys():
return _PROXY_METER_PROVIDER

meter_provider: MeterProvider = _load_provider(
meter_provider: MeterProvider = _load_provider( # type: ignore
OTEL_PYTHON_METER_PROVIDER, "meter_provider"
)
_set_meter_provider(meter_provider, log=False)
Expand Down
Loading