From 3588b0166a440acf0095db3d0020a479f53ba56c Mon Sep 17 00:00:00 2001 From: Srikanth Chekuri Date: Thu, 16 Dec 2021 18:19:55 +0530 Subject: [PATCH 1/7] Run mypy checks on sdk --- .../opentelemetry/sdk/util/instrumentation.py | 26 +++++++++---------- tox.ini | 4 ++- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/util/instrumentation.py b/opentelemetry-sdk/src/opentelemetry/sdk/util/instrumentation.py index 7f565ba8ea3..b92781a8249 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/util/instrumentation.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/util/instrumentation.py @@ -28,30 +28,28 @@ def __init__( name: str, version: typing.Optional[str] = None, schema_url: typing.Optional[str] = None, - ): + ) -> None: self._name = name self._version = version self._schema_url = schema_url - def __repr__(self): + def __repr__(self) -> str: return f"{type(self).__name__}({self._name}, {self._version}, {self._schema_url})" - def __hash__(self): + def __hash__(self) -> int: return hash((self._name, self._version, self._schema_url)) - def __eq__(self, value): - return ( - type(value) is type(self) - and ( - self._name, - self._version, - self._schema_url, - ) - == (value._name, value._version, value._schema_url) + def __eq__(self, value: object) -> bool: + if not isinstance(value, InstrumentationInfo): + return NotImplemented + return (self._name, self._version, self._schema_url) == ( + value._name, + value._version, + value._schema_url, ) - def __lt__(self, value): - if type(value) is not type(self): + def __lt__(self, value: object) -> bool: + if not isinstance(value, InstrumentationInfo): return NotImplemented return (self._name, self._version, self._schema_url) < ( value._name, diff --git a/tox.ini b/tox.ini index 2999b7fe434..a56db6f3190 100644 --- a/tox.ini +++ b/tox.ini @@ -79,7 +79,7 @@ setenv = ; i.e: CONTRIB_REPO_SHA=dde62cebffe519c35875af6d06fae053b3be65ec tox -e CONTRIB_REPO_SHA={env:CONTRIB_REPO_SHA:"main"} CONTRIB_REPO="git+https://github.com/open-telemetry/opentelemetry-python-contrib.git@{env:CONTRIB_REPO_SHA}" - mypy: MYPYPATH={toxinidir}/opentelemetry-api/src/:{toxinidir}/tests/opentelemetry-test-utils/src/ + mypy: MYPYPATH={toxinidir}/opentelemetry-api/src/:{toxinidir}/opentelemetry-sdk/src/:{toxinidir}/tests/opentelemetry-test-utils/src/ changedir = api: opentelemetry-api/tests @@ -165,6 +165,8 @@ commands = coverage: {toxinidir}/scripts/coverage.sh mypy: mypy --namespace-packages --explicit-package-bases opentelemetry-api/src/opentelemetry/ + mypy: mypy --namespace-packages --explicit-package-bases opentelemetry-sdk/src/opentelemetry/ + ; For test code, we don't want to enforce the full mypy strictness mypy: mypy --namespace-packages --config-file=mypy-relaxed.ini opentelemetry-api/tests/ From f6a5b4781aab94a4a2e18cea64d83de14cf6ff5d Mon Sep 17 00:00:00 2001 From: Srikanth Chekuri Date: Fri, 18 Mar 2022 16:00:50 +0530 Subject: [PATCH 2/7] Add more api type --- .../src/opentelemetry/_metrics/__init__.py | 158 +++++++++++++----- .../src/opentelemetry/_metrics/instrument.py | 133 ++++++++++++--- .../src/opentelemetry/util/_providers.py | 5 +- 3 files changed, 232 insertions(+), 64 deletions(-) diff --git a/opentelemetry-api/src/opentelemetry/_metrics/__init__.py b/opentelemetry-api/src/opentelemetry/_metrics/__init__.py index 4fd4b22988c..d9f5e78142e 100644 --- a/opentelemetry-api/src/opentelemetry/_metrics/__init__.py +++ b/opentelemetry-api/src/opentelemetry/_metrics/__init__.py @@ -13,7 +13,6 @@ # limitations under the License. # pylint: disable=too-many-ancestors -# type: ignore # FIXME enhance the documentation of this module """ @@ -26,9 +25,10 @@ from logging import getLogger from os import environ from threading import Lock -from typing import List, Optional, cast +from typing import List, Optional, Set, Union, cast from opentelemetry._metrics.instrument import ( + CallbackT, Counter, DefaultCounter, DefaultHistogram, @@ -37,6 +37,8 @@ DefaultObservableUpDownCounter, DefaultUpDownCounter, Histogram, + Instrument, + InstrumentT, ObservableCounter, ObservableGauge, ObservableUpDownCounter, @@ -62,9 +64,9 @@ class MeterProvider(ABC): @abstractmethod def get_meter( self, - name, - version=None, - schema_url=None, + name: str, + version: Optional[str] = None, + schema_url: Optional[str] = None, ) -> "Meter": pass @@ -72,9 +74,9 @@ def get_meter( 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": super().get_meter(name, version=version, schema_url=schema_url) return NoOpMeter(name, version=version, schema_url=schema_url) @@ -88,9 +90,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: @@ -110,40 +112,57 @@ def on_set_meter_provider(self, meter_provider: MeterProvider) -> None: class Meter(ABC): - def __init__(self, name, version=None, schema_url=None): + def __init__( + self, + name: str, + version: Optional[str] = None, + schema_url: Optional[str] = None, + ) -> None: super().__init__() self._name = name self._version = version self._schema_url = schema_url - self._instrument_names = set() + self._instrument_names: Set[str] = set() @property - def name(self): + def name(self) -> str: return self._name @property - def version(self): + def version(self) -> Optional[str]: return self._version @property - def schema_url(self): + def schema_url(self) -> Optional[str]: return self._schema_url # FIXME check that the instrument name has not been used already @abstractmethod - def create_counter(self, name, unit="", description="") -> Counter: + def create_counter( + self, + name: str, + unit: Optional[str] = "", + description: Optional[str] = "", + ) -> Counter: pass @abstractmethod def create_up_down_counter( - self, name, unit="", description="" + self, + name: str, + unit: Optional[str] = "", + description: Optional[str] = "", ) -> UpDownCounter: pass @abstractmethod def create_observable_counter( - self, name, callback, unit="", description="" + self, + name: str, + callback: CallbackT, + unit: Optional[str] = "", + description: Optional[str] = "", ) -> ObservableCounter: """Creates an observable counter instrument @@ -225,18 +244,31 @@ def cpu_time_callback(states_to_include: set[str]) -> Iterable[Iterable[Measurem """ @abstractmethod - def create_histogram(self, name, unit="", description="") -> Histogram: + def create_histogram( + self, + name: str, + unit: Optional[str] = "", + description: Optional[str] = "", + ) -> Histogram: pass @abstractmethod def create_observable_gauge( - self, name, callback, unit="", description="" + self, + name: str, + callback: CallbackT, + unit: Optional[str] = "", + description: Optional[str] = "", ) -> ObservableGauge: pass @abstractmethod def create_observable_up_down_counter( - self, name, callback, unit="", description="" + self, + name: str, + callback: CallbackT, + unit: Optional[str] = "", + description: Optional[str] = "", ) -> ObservableUpDownCounter: pass @@ -244,10 +276,10 @@ 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] = [] @@ -270,7 +302,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: Optional[str] = "", + description: Optional[str] = "", + ) -> Counter: with self._lock: if self._real_meter: return self._real_meter.create_counter(name, unit, description) @@ -279,7 +316,10 @@ def create_counter(self, name, unit="", description="") -> Counter: return proxy def create_up_down_counter( - self, name, unit="", description="" + self, + name: str, + unit: Optional[str] = "", + description: Optional[str] = "", ) -> UpDownCounter: with self._lock: if self._real_meter: @@ -291,7 +331,11 @@ def create_up_down_counter( return proxy def create_observable_counter( - self, name, callback, unit="", description="" + self, + name: str, + callback: CallbackT, + unit: Optional[str] = "", + description: Optional[str] = "", ) -> ObservableCounter: with self._lock: if self._real_meter: @@ -304,7 +348,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: Optional[str] = "", + description: Optional[str] = "", + ) -> Histogram: with self._lock: if self._real_meter: return self._real_meter.create_histogram( @@ -315,7 +364,11 @@ def create_histogram(self, name, unit="", description="") -> Histogram: return proxy def create_observable_gauge( - self, name, callback, unit="", description="" + self, + name: str, + callback: CallbackT, + unit: Optional[str] = "", + description: Optional[str] = "", ) -> ObservableGauge: with self._lock: if self._real_meter: @@ -329,7 +382,11 @@ def create_observable_gauge( return proxy def create_observable_up_down_counter( - self, name, callback, unit="", description="" + self, + name: str, + callback: CallbackT, + unit: Optional[str] = "", + description: Optional[str] = "", ) -> ObservableUpDownCounter: with self._lock: if self._real_meter: @@ -347,12 +404,20 @@ def create_observable_up_down_counter( class NoOpMeter(Meter): - def create_counter(self, name, unit="", description="") -> Counter: + def create_counter( + self, + name: str, + unit: Optional[str] = "", + description: Optional[str] = "", + ) -> Counter: super().create_counter(name, unit=unit, description=description) return DefaultCounter(name, unit=unit, description=description) def create_up_down_counter( - self, name, unit="", description="" + self, + name: str, + unit: Optional[str] = "", + description: Optional[str] = "", ) -> UpDownCounter: super().create_up_down_counter( name, unit=unit, description=description @@ -360,7 +425,11 @@ def create_up_down_counter( return DefaultUpDownCounter(name, unit=unit, description=description) def create_observable_counter( - self, name, callback, unit="", description="" + self, + name: str, + callback: CallbackT, + unit: Optional[str] = "", + description: Optional[str] = "", ) -> ObservableCounter: super().create_observable_counter( name, callback, unit=unit, description=description @@ -372,12 +441,21 @@ def create_observable_counter( description=description, ) - def create_histogram(self, name, unit="", description="") -> Histogram: + def create_histogram( + self, + name: str, + unit: Optional[str] = "", + description: Optional[str] = "", + ) -> Histogram: super().create_histogram(name, unit=unit, description=description) return DefaultHistogram(name, unit=unit, description=description) def create_observable_gauge( - self, name, callback, unit="", description="" + self, + name: str, + callback: CallbackT, + unit: Optional[str] = "", + description: Optional[str] = "", ) -> ObservableGauge: super().create_observable_gauge( name, callback, unit=unit, description=description @@ -390,7 +468,11 @@ def create_observable_gauge( ) def create_observable_up_down_counter( - self, name, callback, unit="", description="" + self, + name: str, + callback: CallbackT, + unit: Optional[str] = "", + description: Optional[str] = "", ) -> ObservableUpDownCounter: super().create_observable_up_down_counter( name, callback, unit=unit, description=description diff --git a/opentelemetry-api/src/opentelemetry/_metrics/instrument.py b/opentelemetry-api/src/opentelemetry/_metrics/instrument.py index d6f86331735..265c0fd95bc 100644 --- a/opentelemetry-api/src/opentelemetry/_metrics/instrument.py +++ b/opentelemetry-api/src/opentelemetry/_metrics/instrument.py @@ -14,7 +14,6 @@ # pylint: disable=too-many-ancestors -# type: ignore from abc import ABC, abstractmethod from logging import getLogger @@ -31,6 +30,7 @@ # pylint: disable=unused-import; needed for typing and sphinx from opentelemetry import _metrics as metrics from opentelemetry._metrics.measurement import Measurement +from opentelemetry.util.types import Attributes InstrumentT = TypeVar("InstrumentT", bound="Instrument") CallbackT = Union[ @@ -44,7 +44,12 @@ class Instrument(ABC): @abstractmethod - def __init__(self, name, unit="", description=""): + def __init__( + self, + name: str, + unit: Optional[str] = "", + description: Optional[str] = "", + ) -> None: pass # FIXME check that the instrument name is valid @@ -53,7 +58,12 @@ def __init__(self, name, unit="", description=""): class _ProxyInstrument(ABC, Generic[InstrumentT]): - def __init__(self, name, unit, description) -> None: + def __init__( + self, + name: str, + unit: Optional[str] = "", + description: Optional[str] = "", + ) -> None: self._name = name self._unit = unit self._description = description @@ -73,7 +83,13 @@ def _create_real_instrument(self, meter: "metrics.Meter") -> InstrumentT: class _ProxyAsynchronousInstrument(_ProxyInstrument[InstrumentT]): - def __init__(self, name, callback, unit, description) -> None: + def __init__( + self, + name: str, + callback: CallbackT, + unit: Optional[str] = "", + description: Optional[str] = "", + ) -> None: super().__init__(name, unit, description) self._callback = callback @@ -86,11 +102,11 @@ class Asynchronous(Instrument): @abstractmethod def __init__( self, - name, - callback, - unit="", - description="", - ): + name: str, + callback: CallbackT, + unit: Optional[str] = "", + description: Optional[str] = "", + ) -> None: super().__init__(name, unit=unit, description=description) @@ -112,21 +128,38 @@ class _NonMonotonic(_Adding): class Counter(_Monotonic, Synchronous): @abstractmethod - def add(self, amount, attributes=None): + def add( + self, + amount: Union[int, float], + attributes: Optional[Attributes] = None, + ) -> None: # FIXME check that the amount is non negative pass class DefaultCounter(Counter): - def __init__(self, name, unit="", description=""): + def __init__( + self, + name: str, + unit: Optional[str] = "", + description: Optional[str] = "", + ) -> None: super().__init__(name, unit=unit, description=description) - def add(self, amount, attributes=None): + def add( + self, + amount: Union[int, float], + attributes: Optional[Attributes] = None, + ) -> None: return super().add(amount, attributes=attributes) class _ProxyCounter(_ProxyInstrument[Counter], Counter): - def add(self, amount, attributes=None): + def add( + self, + amount: Union[int, float], + attributes: Optional[Attributes] = None, + ) -> None: if self._real_instrument: self._real_instrument.add(amount, attributes) @@ -136,20 +169,37 @@ def _create_real_instrument(self, meter: "metrics.Meter") -> Counter: class UpDownCounter(_NonMonotonic, Synchronous): @abstractmethod - def add(self, amount, attributes=None): + def add( + self, + amount: Union[int, float], + attributes: Optional[Attributes] = None, + ) -> None: pass class DefaultUpDownCounter(UpDownCounter): - def __init__(self, name, unit="", description=""): + def __init__( + self, + name: str, + unit: Optional[str] = "", + description: Optional[str] = "", + ) -> None: super().__init__(name, unit=unit, description=description) - def add(self, amount, attributes=None): + def add( + self, + amount: Union[int, float], + attributes: Optional[Attributes] = None, + ) -> None: return super().add(amount, attributes=attributes) class _ProxyUpDownCounter(_ProxyInstrument[UpDownCounter], UpDownCounter): - def add(self, amount, attributes=None): + def add( + self, + amount: Union[int, float], + attributes: Optional[Attributes] = None, + ) -> None: if self._real_instrument: self._real_instrument.add(amount, attributes) @@ -164,7 +214,13 @@ class ObservableCounter(_Monotonic, Asynchronous): class DefaultObservableCounter(ObservableCounter): - def __init__(self, name, callback, unit="", description=""): + def __init__( + self, + name: str, + callback: CallbackT, + unit: Optional[str] = "", + description: Optional[str] = "", + ) -> None: super().__init__(name, callback, unit=unit, description=description) @@ -184,7 +240,13 @@ class ObservableUpDownCounter(_NonMonotonic, Asynchronous): class DefaultObservableUpDownCounter(ObservableUpDownCounter): - def __init__(self, name, callback, unit="", description=""): + def __init__( + self, + name: str, + callback: CallbackT, + unit: Optional[str] = "", + description: Optional[str] = "", + ) -> None: super().__init__(name, callback, unit=unit, description=description) @@ -202,20 +264,37 @@ def _create_real_instrument( class Histogram(_Grouping, Synchronous): @abstractmethod - def record(self, amount, attributes=None): + def record( + self, + amount: Union[int, float], + attributes: Optional[Attributes] = None, + ) -> None: pass class DefaultHistogram(Histogram): - def __init__(self, name, unit="", description=""): + def __init__( + self, + name: str, + unit: Optional[str] = "", + description: Optional[str] = "", + ) -> None: super().__init__(name, unit=unit, description=description) - def record(self, amount, attributes=None): + def record( + self, + amount: Union[int, float], + attributes: Optional[Attributes] = None, + ) -> None: return super().record(amount, attributes=attributes) class _ProxyHistogram(_ProxyInstrument[Histogram], Histogram): - def record(self, amount, attributes=None): + def record( + self, + amount: Union[int, float], + attributes: Optional[Attributes] = None, + ) -> None: if self._real_instrument: self._real_instrument.record(amount, attributes) @@ -230,7 +309,13 @@ class ObservableGauge(_Grouping, Asynchronous): class DefaultObservableGauge(ObservableGauge): - def __init__(self, name, callback, unit="", description=""): + def __init__( + self, + name: str, + callback: CallbackT, + unit: Optional[str] = "", + description: Optional[str] = "", + ) -> None: super().__init__(name, callback, unit=unit, description=description) diff --git a/opentelemetry-api/src/opentelemetry/util/_providers.py b/opentelemetry-api/src/opentelemetry/util/_providers.py index 98c75ed06bd..46c059a26b8 100644 --- a/opentelemetry-api/src/opentelemetry/util/_providers.py +++ b/opentelemetry-api/src/opentelemetry/util/_providers.py @@ -14,14 +14,15 @@ from logging import getLogger from os import environ -from typing import TYPE_CHECKING, Union, cast +from typing import TYPE_CHECKING, TypeVar, Union, cast from pkg_resources import iter_entry_points if TYPE_CHECKING: + from opentelemetry._metrics import MeterProvider from opentelemetry.trace import TracerProvider -Provider = Union["TracerProvider"] +Provider = TypeVar("Provider", "TracerProvider", "MeterProvider") logger = getLogger(__name__) From a195cfded2e78114399fb194d3bcdea6693caa7b Mon Sep 17 00:00:00 2001 From: Srikanth Chekuri Date: Thu, 21 Apr 2022 15:50:49 +0530 Subject: [PATCH 3/7] Update callbacks --- .../src/opentelemetry/_metrics/__init__.py | 18 +++++++++--------- .../src/opentelemetry/_metrics/instrument.py | 10 +++++----- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/opentelemetry-api/src/opentelemetry/_metrics/__init__.py b/opentelemetry-api/src/opentelemetry/_metrics/__init__.py index c4874af6116..c318142400a 100644 --- a/opentelemetry-api/src/opentelemetry/_metrics/__init__.py +++ b/opentelemetry-api/src/opentelemetry/_metrics/__init__.py @@ -261,7 +261,7 @@ def create_up_down_counter( def create_observable_counter( self, name: str, - callbacks: Sequence[CallbackT], + callbacks: Optional[Sequence[CallbackT]] = None, unit: str = "", description: str = "", ) -> ObservableCounter: @@ -365,7 +365,7 @@ def create_histogram( def create_observable_gauge( self, name: str, - callbacks: Sequence[CallbackT], + callbacks: Optional[Sequence[CallbackT]] = None, unit: str = "", description: str = "", ) -> ObservableGauge: @@ -386,7 +386,7 @@ def create_observable_gauge( def create_observable_up_down_counter( self, name: str, - callbacks: Sequence[CallbackT], + callbacks: Optional[Sequence[CallbackT]] = None, unit: str = "", description: str = "", ) -> ObservableUpDownCounter: @@ -464,7 +464,7 @@ def create_up_down_counter( def create_observable_counter( self, name: str, - callbacks: Sequence[CallbackT], + callbacks: Optional[Sequence[CallbackT]] = None, unit: str = "", description: str = "", ) -> ObservableCounter: @@ -497,7 +497,7 @@ def create_histogram( def create_observable_gauge( self, name: str, - callbacks: Sequence[CallbackT], + callbacks: Optional[Sequence[CallbackT]] = None, unit: str = "", description: str = "", ) -> ObservableGauge: @@ -515,7 +515,7 @@ def create_observable_gauge( def create_observable_up_down_counter( self, name: str, - callbacks: Sequence[CallbackT], + callbacks: Optional[Sequence[CallbackT]] = None, unit: str = "", description: str = "", ) -> ObservableUpDownCounter: @@ -580,7 +580,7 @@ def create_up_down_counter( def create_observable_counter( self, name: str, - callbacks: Sequence[CallbackT], + callbacks: Optional[Sequence[CallbackT]] = None, unit: str = "", description: str = "", ) -> ObservableCounter: @@ -628,7 +628,7 @@ def create_histogram( def create_observable_gauge( self, name: str, - callbacks: Sequence[CallbackT], + callbacks: Optional[Sequence[CallbackT]] = None, unit: str = "", description: str = "", ) -> ObservableGauge: @@ -657,7 +657,7 @@ def create_observable_gauge( def create_observable_up_down_counter( self, name: str, - callbacks: Sequence[CallbackT], + callbacks: Optional[Sequence[CallbackT]] = None, unit: str = "", description: str = "", ) -> ObservableUpDownCounter: diff --git a/opentelemetry-api/src/opentelemetry/_metrics/instrument.py b/opentelemetry-api/src/opentelemetry/_metrics/instrument.py index 240da253e6f..238b678478a 100644 --- a/opentelemetry-api/src/opentelemetry/_metrics/instrument.py +++ b/opentelemetry-api/src/opentelemetry/_metrics/instrument.py @@ -87,7 +87,7 @@ class _ProxyAsynchronousInstrument(_ProxyInstrument[InstrumentT]): def __init__( self, name: str, - callbacks: Sequence[CallbackT], + callbacks: Optional[Sequence[CallbackT]] = None, unit: str = "", description: str = "", ) -> None: @@ -104,7 +104,7 @@ class Asynchronous(Instrument): def __init__( self, name: str, - callbacks: Sequence[CallbackT], + callbacks: Optional[Sequence[CallbackT]] = None, unit: str = "", description: str = "", ) -> None: @@ -224,7 +224,7 @@ class NoOpObservableCounter(ObservableCounter): def __init__( self, name: str, - callbacks: Sequence[CallbackT], + callbacks: Optional[Sequence[CallbackT]] = None, unit: str = "", description: str = "", ) -> None: @@ -253,7 +253,7 @@ class NoOpObservableUpDownCounter(ObservableUpDownCounter): def __init__( self, name: str, - callbacks: Sequence[CallbackT], + callbacks: Optional[Sequence[CallbackT]] = None, unit: str = "", description: str = "", ) -> None: @@ -330,7 +330,7 @@ class NoOpObservableGauge(ObservableGauge): def __init__( self, name: str, - callbacks: Sequence[CallbackT], + callbacks: Optional[Sequence[CallbackT]] = None, unit: str = "", description: str = "", ) -> None: From 25bb41c0318b14577f0335f9ef4850b8d2422d05 Mon Sep 17 00:00:00 2001 From: Srikanth Chekuri Date: Thu, 21 Apr 2022 16:03:01 +0530 Subject: [PATCH 4/7] Fix conflicts --- .../src/opentelemetry/_metrics/__init__.py | 18 ++++++++------ .../src/opentelemetry/_metrics/instrument.py | 24 +++++++++---------- .../src/opentelemetry/util/_providers.py | 4 ++-- tox.ini | 3 +-- 4 files changed, 26 insertions(+), 23 deletions(-) diff --git a/opentelemetry-api/src/opentelemetry/_metrics/__init__.py b/opentelemetry-api/src/opentelemetry/_metrics/__init__.py index d2dc7491a32..9f55af7e363 100644 --- a/opentelemetry-api/src/opentelemetry/_metrics/__init__.py +++ b/opentelemetry-api/src/opentelemetry/_metrics/__init__.py @@ -162,19 +162,18 @@ def on_set_meter_provider(self, meter_provider: MeterProvider) -> None: class Meter(ABC): + """Handles instrument creation. + + This class provides methods for creating instruments which are then + used to produce measurements. + """ + def __init__( self, name: str, version: Optional[str] = None, schema_url: Optional[str] = None, ) -> None: - - """Handles instrument creation. - - This class provides methods for creating instruments which are then - used to produce measurements. - """ - super().__init__() self._name = name self._version = version @@ -535,6 +534,11 @@ def create_observable_up_down_counter( class NoOpMeter(Meter): + """The default Meter used when no Meter implementation is available. + + All operations are no-op. + """ + def create_counter( self, name: str, diff --git a/opentelemetry-api/src/opentelemetry/_metrics/instrument.py b/opentelemetry-api/src/opentelemetry/_metrics/instrument.py index ee44056658d..1a855621f4d 100644 --- a/opentelemetry-api/src/opentelemetry/_metrics/instrument.py +++ b/opentelemetry-api/src/opentelemetry/_metrics/instrument.py @@ -143,14 +143,14 @@ def add( class NoOpCounter(Counter): + """No-op implementation of `Counter`.""" + def __init__( self, name: str, unit: str = "", description: str = "", ) -> None: - """No-op implementation of `Counter`.""" - super().__init__(name, unit=unit, description=description) def add( @@ -187,14 +187,14 @@ def add( class NoOpUpDownCounter(UpDownCounter): + """No-op implementation of `UpDownCounter`.""" + def __init__( self, name: str, unit: str = "", description: str = "", ) -> None: - """No-op implementation of `UpDownCounter`.""" - super().__init__(name, unit=unit, description=description) def add( @@ -227,6 +227,8 @@ class ObservableCounter(_Monotonic, Asynchronous): class NoOpObservableCounter(ObservableCounter): + """No-op implementation of `ObservableCounter`.""" + def __init__( self, name: str, @@ -234,8 +236,6 @@ def __init__( unit: str = "", description: str = "", ) -> None: - """No-op implementation of `ObservableCounter`.""" - super().__init__(name, callbacks, unit=unit, description=description) @@ -258,6 +258,8 @@ class ObservableUpDownCounter(_NonMonotonic, Asynchronous): class NoOpObservableUpDownCounter(ObservableUpDownCounter): + """No-op implementation of `ObservableUpDownCounter`.""" + def __init__( self, name: str, @@ -265,8 +267,6 @@ def __init__( unit: str = "", description: str = "", ) -> None: - """No-op implementation of `ObservableUpDownCounter`.""" - super().__init__(name, callbacks, unit=unit, description=description) @@ -298,14 +298,14 @@ def record( class NoOpHistogram(Histogram): + """No-op implementation of `Histogram`.""" + def __init__( self, name: str, unit: str = "", description: str = "", ) -> None: - """No-op implementation of `Histogram`.""" - super().__init__(name, unit=unit, description=description) def record( @@ -339,6 +339,8 @@ class ObservableGauge(_Grouping, Asynchronous): class NoOpObservableGauge(ObservableGauge): + """No-op implementation of `ObservableGauge`.""" + def __init__( self, name: str, @@ -346,8 +348,6 @@ def __init__( unit: str = "", description: str = "", ) -> None: - """No-op implementation of `ObservableGauge`.""" - super().__init__(name, callbacks, unit=unit, description=description) diff --git a/opentelemetry-api/src/opentelemetry/util/_providers.py b/opentelemetry-api/src/opentelemetry/util/_providers.py index 46c059a26b8..208ca64c1f0 100644 --- a/opentelemetry-api/src/opentelemetry/util/_providers.py +++ b/opentelemetry-api/src/opentelemetry/util/_providers.py @@ -14,7 +14,7 @@ from logging import getLogger from os import environ -from typing import TYPE_CHECKING, TypeVar, Union, cast +from typing import TYPE_CHECKING, Union, cast from pkg_resources import iter_entry_points @@ -22,7 +22,7 @@ from opentelemetry._metrics import MeterProvider from opentelemetry.trace import TracerProvider -Provider = TypeVar("Provider", "TracerProvider", "MeterProvider") +Provider = Union["TracerProvider"] logger = getLogger(__name__) diff --git a/tox.ini b/tox.ini index d14a1118c6a..73df1f69285 100644 --- a/tox.ini +++ b/tox.ini @@ -83,7 +83,7 @@ setenv = ; i.e: CONTRIB_REPO_SHA=dde62cebffe519c35875af6d06fae053b3be65ec tox -e CONTRIB_REPO_SHA={env:CONTRIB_REPO_SHA:"main"} CONTRIB_REPO="git+https://github.com/open-telemetry/opentelemetry-python-contrib.git@{env:CONTRIB_REPO_SHA}" - mypy: MYPYPATH={toxinidir}/opentelemetry-api/src/:{toxinidir}/opentelemetry-sdk/src/:{toxinidir}/tests/opentelemetry-test-utils/src/ + mypy: MYPYPATH={toxinidir}/opentelemetry-api/src/:{toxinidir}/tests/opentelemetry-test-utils/src/ changedir = api: opentelemetry-api/tests @@ -172,7 +172,6 @@ commands = coverage: {toxinidir}/scripts/coverage.sh mypy: mypy --install-types --non-interactive --namespace-packages --explicit-package-bases opentelemetry-api/src/opentelemetry/ - mypy: mypy --install-types --non-interactive --namespace-packages --explicit-package-bases opentelemetry-sdk/src/opentelemetry/ ; For test code, we don't want to enforce the full mypy strictness mypy: mypy --install-types --non-interactive --namespace-packages --config-file=mypy-relaxed.ini opentelemetry-api/tests/ From 6cc847b4e01c63b3ee6fedaac72bcc57ab16209f Mon Sep 17 00:00:00 2001 From: Srikanth Chekuri Date: Thu, 21 Apr 2022 16:14:03 +0530 Subject: [PATCH 5/7] Fix incorrect comparision --- .../src/opentelemetry/sdk/util/instrumentation.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/util/instrumentation.py b/opentelemetry-sdk/src/opentelemetry/sdk/util/instrumentation.py index 81459a3daf0..a489c207a03 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/util/instrumentation.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/util/instrumentation.py @@ -98,7 +98,7 @@ def __hash__(self) -> int: return hash((self._name, self._version, self._schema_url)) def __eq__(self, value: object) -> bool: - if not isinstance(value, InstrumentationInfo): + if not isinstance(value, InstrumentationScope): return NotImplemented return (self._name, self._version, self._schema_url) == ( value._name, @@ -107,7 +107,7 @@ def __eq__(self, value: object) -> bool: ) def __lt__(self, value: object) -> bool: - if not isinstance(value, InstrumentationInfo): + if not isinstance(value, InstrumentationScope): return NotImplemented return (self._name, self._version, self._schema_url) < ( value._name, From 2fbf2272c7ee4c235a5f7a243abf4e886f5138d6 Mon Sep 17 00:00:00 2001 From: Srikanth Chekuri Date: Fri, 22 Apr 2022 22:19:15 +0530 Subject: [PATCH 6/7] Fix lint --- .../src/opentelemetry/_metrics/__init__.py | 14 ++++++++++++-- .../src/opentelemetry/util/_providers.py | 4 ++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/opentelemetry-api/src/opentelemetry/_metrics/__init__.py b/opentelemetry-api/src/opentelemetry/_metrics/__init__.py index 9f55af7e363..c634bd77b7c 100644 --- a/opentelemetry-api/src/opentelemetry/_metrics/__init__.py +++ b/opentelemetry-api/src/opentelemetry/_metrics/__init__.py @@ -78,6 +78,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. @@ -412,7 +422,7 @@ def __init__( ) -> 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: @@ -740,7 +750,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) diff --git a/opentelemetry-api/src/opentelemetry/util/_providers.py b/opentelemetry-api/src/opentelemetry/util/_providers.py index 208ca64c1f0..f19c32ee86d 100644 --- a/opentelemetry-api/src/opentelemetry/util/_providers.py +++ b/opentelemetry-api/src/opentelemetry/util/_providers.py @@ -14,7 +14,7 @@ from logging import getLogger from os import environ -from typing import TYPE_CHECKING, Union, cast +from typing import TYPE_CHECKING, TypeVar, cast from pkg_resources import iter_entry_points @@ -22,7 +22,7 @@ from opentelemetry._metrics import MeterProvider from opentelemetry.trace import TracerProvider -Provider = Union["TracerProvider"] +Provider = TypeVar("Provider", "TracerProvider", "MeterProvider") logger = getLogger(__name__) From 9f4c39e6cbb762f3805f08b47320f883b93a5823 Mon Sep 17 00:00:00 2001 From: Srikanth Chekuri Date: Fri, 22 Apr 2022 22:36:31 +0530 Subject: [PATCH 7/7] Fix lint --- opentelemetry-api/src/opentelemetry/_metrics/__init__.py | 1 - 1 file changed, 1 deletion(-) diff --git a/opentelemetry-api/src/opentelemetry/_metrics/__init__.py b/opentelemetry-api/src/opentelemetry/_metrics/__init__.py index ec7ba2a92df..6447135743b 100644 --- a/opentelemetry-api/src/opentelemetry/_metrics/__init__.py +++ b/opentelemetry-api/src/opentelemetry/_metrics/__init__.py @@ -63,7 +63,6 @@ UpDownCounter, _ProxyCounter, _ProxyHistogram, - _ProxyInstrument, _ProxyObservableCounter, _ProxyObservableGauge, _ProxyObservableUpDownCounter,