From 3ab4dc7c2af640410b199d2ed2de28abdc3230f9 Mon Sep 17 00:00:00 2001 From: PrabhakarJuzgar Date: Mon, 16 Dec 2024 18:39:14 +0000 Subject: [PATCH 01/10] Running mypy on sdk resources #773 --- .../sdk/error_handler/__init__.py | 27 ++++++++++--------- .../_internal/exemplar/exemplar_reservoir.py | 16 +++++------ 2 files changed, 22 insertions(+), 21 deletions(-) diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/error_handler/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/error_handler/__init__.py index 3e0e778f1a..619c24be2d 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/error_handler/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/error_handler/__init__.py @@ -61,6 +61,7 @@ def _handle(self, error: Exception, *args, **kwargs): from abc import ABC, abstractmethod from logging import getLogger +from typing import Optional from opentelemetry.util._importlib_metadata import entry_points @@ -69,7 +70,7 @@ def _handle(self, error: Exception, *args, **kwargs): class ErrorHandler(ABC): @abstractmethod - def _handle(self, error: Exception, *args, **kwargs): + def _handle(self, error: Exception, *args, **kwargs) -> None: # type: ignore """ Handle an exception """ @@ -83,7 +84,7 @@ class _DefaultErrorHandler(ErrorHandler): """ # pylint: disable=useless-return - def _handle(self, error: Exception, *args, **kwargs): + def _handle(self, error: Exception, *args, **kwargs) -> None: # type: ignore logger.exception("Error handled by default error handler: ") return None @@ -105,26 +106,26 @@ def __new__(cls) -> "GlobalErrorHandler": return cls._instance - def __enter__(self): + def __enter__(self) -> None: pass # pylint: disable=no-self-use - def __exit__(self, exc_type, exc_value, traceback): - if exc_value is None: + def __exit__(self, exc_type, exc_value, traceback) -> Optional[bool]: # type: ignore + if exc_value is None: # type: ignore return None plugin_handled = False - error_handler_entry_points = entry_points( + error_handler_entry_points = entry_points( # type: ignore group="opentelemetry_error_handler" ) - for error_handler_entry_point in error_handler_entry_points: - error_handler_class = error_handler_entry_point.load() + for error_handler_entry_point in error_handler_entry_points: # type: ignore + error_handler_class = error_handler_entry_point.load() # type: ignore - if issubclass(error_handler_class, exc_value.__class__): + if issubclass(error_handler_class, exc_value.__class__): # type: ignore try: - error_handler_class()._handle(exc_value) + error_handler_class()._handle(exc_value) # type: ignore plugin_handled = True # pylint: disable=broad-exception-caught @@ -133,11 +134,11 @@ def __exit__(self, exc_type, exc_value, traceback): "%s error while handling error" " %s by error handler %s", error_handling_error.__class__.__name__, - exc_value.__class__.__name__, - error_handler_class.__name__, + exc_value.__class__.__name__, # type: ignore + error_handler_class.__name__, # type: ignore ) if not plugin_handled: - _DefaultErrorHandler()._handle(exc_value) + _DefaultErrorHandler()._handle(exc_value) # type: ignore return True diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/exemplar/exemplar_reservoir.py b/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/exemplar/exemplar_reservoir.py index 22d1ee9f75..d4feb877c2 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/exemplar/exemplar_reservoir.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/exemplar/exemplar_reservoir.py @@ -129,7 +129,7 @@ def collect(self, point_attributes: Attributes) -> Optional[Exemplar]: { k: v for k, v in self.__attributes.items() - if k not in point_attributes + if k not in point_attributes # type: ignore } if self.__attributes else None @@ -162,8 +162,8 @@ class BucketIndexError(ValueError): class FixedSizeExemplarReservoirABC(ExemplarReservoir): """Abstract class for a reservoir with fixed size.""" - def __init__(self, size: int, **kwargs) -> None: - super().__init__(**kwargs) + def __init__(self, size: int, **kwargs) -> None: # type: ignore + super().__init__(**kwargs) # type: ignore self._size: int = size self._reservoir_storage: Mapping[int, ExemplarBucket] = defaultdict( ExemplarBucket @@ -184,7 +184,7 @@ def collect(self, point_attributes: Attributes) -> List[Exemplar]: exemplars = [ e for e in ( - bucket.collect(point_attributes) + bucket.collect(point_attributes) # type: ignore for _, bucket in sorted(self._reservoir_storage.items()) ) if e is not None @@ -257,8 +257,8 @@ class SimpleFixedSizeExemplarReservoir(FixedSizeExemplarReservoirABC): https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/sdk.md#simplefixedsizeexemplarreservoir """ - def __init__(self, size: int = 1, **kwargs) -> None: - super().__init__(size, **kwargs) + def __init__(self, size: int = 1, **kwargs) -> None: # type: ignore + super().__init__(size, **kwargs) # type: ignore self._measurements_seen: int = 0 def _reset(self) -> None: @@ -292,8 +292,8 @@ class AlignedHistogramBucketExemplarReservoir(FixedSizeExemplarReservoirABC): https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/sdk.md#alignedhistogrambucketexemplarreservoir """ - def __init__(self, boundaries: Sequence[float], **kwargs) -> None: - super().__init__(len(boundaries) + 1, **kwargs) + def __init__(self, boundaries: Sequence[float], **kwargs) -> None: # type: ignore + super().__init__(len(boundaries) + 1, **kwargs) # type: ignore self._boundaries: Sequence[float] = boundaries def offer( From c38154e43c70b4a6fd95597ba9287d8b8afd5a8f Mon Sep 17 00:00:00 2001 From: PrabhakarJuzgar Date: Mon, 16 Dec 2024 21:05:16 +0000 Subject: [PATCH 02/10] Update changelog --- CHANGELOG.md | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f8b1a254db..6161d38e70 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Unreleased +- Run mypy on SDK resources + ([#4360](https://github.com/open-telemetry/opentelemetry-python/pull/4360)) + ## Version 1.29.0/0.50b0 (2024-12-11) - Fix crash exporting a log record with None body @@ -126,20 +129,20 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Improve resource field structure for LogRecords ([#3972](https://github.com/open-telemetry/opentelemetry-python/pull/3972)) - Update Semantic Conventions code generation scripts: - - fix namespace exclusion that resulted in dropping `os` and `net` namespaces. + - fix namespace exclusion that resulted in dropping `os` and `net` namespaces. - add `Final` decorator to constants to prevent collisions - enable mypy and fix detected issues - allow to drop specific attributes in preparation for Semantic Conventions v1.26.0 - ([#3973](https://github.com/open-telemetry/opentelemetry-python/pull/3966)) + ([#3973](https://github.com/open-telemetry/opentelemetry-python/pull/3966)) - Update semantic conventions to version 1.26.0. ([#3964](https://github.com/open-telemetry/opentelemetry-python/pull/3964)) - Use semconv exception attributes for record exceptions in spans ([#3979](https://github.com/open-telemetry/opentelemetry-python/pull/3979)) -- Fix _encode_events assumes events.attributes.dropped exists +- Fix \_encode_events assumes events.attributes.dropped exists ([#3965](https://github.com/open-telemetry/opentelemetry-python/pull/3965)) - Validate links at span creation ([#3991](https://github.com/open-telemetry/opentelemetry-python/pull/3991)) -- Add attributes field in `MeterProvider.get_meter` and `InstrumentationScope` +- Add attributes field in `MeterProvider.get_meter` and `InstrumentationScope` ([#4015](https://github.com/open-telemetry/opentelemetry-python/pull/4015)) - Fix inaccessible `SCHEMA_URL` constants in `opentelemetry-semantic-conventions` ([#4069](https://github.com/open-telemetry/opentelemetry-python/pull/4069)) @@ -169,10 +172,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `SpanAttributes`, `ResourceAttributes`, and `MetricInstruments` are deprecated. - Attribute and metric definitions are now grouped by the namespace. - Stable attributes and metrics are moved to `opentelemetry.semconv.attributes` - and `opentelemetry.semconv.metrics` modules. + and `opentelemetry.semconv.metrics` modules. - Stable and experimental attributes and metrics are defined under - `opentelemetry.semconv._incubating` import path. - ([#3586](https://github.com/open-telemetry/opentelemetry-python/pull/3586)) + `opentelemetry.semconv._incubating` import path. + ([#3586](https://github.com/open-telemetry/opentelemetry-python/pull/3586)) - Rename test objects to avoid pytest warnings ([#3823] (https://github.com/open-telemetry/opentelemetry-python/pull/3823)) - Add span flags to OTLP spans and links @@ -184,11 +187,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Fix prometheus metric name and unit conversion ([#3924](https://github.com/open-telemetry/opentelemetry-python/pull/3924)) - this is a breaking change to prometheus metric names so they comply with the - [specification](https://github.com/open-telemetry/opentelemetry-specification/blob/v1.33.0/specification/compatibility/prometheus_and_openmetrics.md#otlp-metric-points-to-prometheus). + [specification](https://github.com/open-telemetry/opentelemetry-specification/blob/v1.33.0/specification/compatibility/prometheus_and_openmetrics.md#otlp-metric-points-to-prometheus). - you can temporarily opt-out of the unit normalization by setting the environment variable - `OTEL_PYTHON_EXPERIMENTAL_DISABLE_PROMETHEUS_UNIT_NORMALIZATION=true` + `OTEL_PYTHON_EXPERIMENTAL_DISABLE_PROMETHEUS_UNIT_NORMALIZATION=true` - common unit abbreviations are converted to Prometheus conventions (`s` -> `seconds`), - following the [collector's implementation](https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/c0b51136575aa7ba89326d18edb4549e7e1bbdb9/pkg/translator/prometheus/normalize_name.go#L108) + following the [collector's implementation](https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/c0b51136575aa7ba89326d18edb4549e7e1bbdb9/pkg/translator/prometheus/normalize_name.go#L108) - repeated `_` are replaced with a single `_` - unit annotations (enclosed in curly braces like `{requests}`) are stripped away - units with slash are converted e.g. `m/s` -> `meters_per_second`. @@ -274,7 +277,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Version 1.21.0/0.42b0 (2023-11-01) - Fix `SumAggregation` - ([#3390](https://github.com/open-telemetry/opentelemetry-python/pull/3390)) +  ([#3390](https://github.com/open-telemetry/opentelemetry-python/pull/3390)) - Fix handling of empty metric collection cycles ([#3335](https://github.com/open-telemetry/opentelemetry-python/pull/3335)) - Fix error when no LoggerProvider configured for LoggingHandler @@ -294,7 +297,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Implement Process Resource detector ([#3472](https://github.com/open-telemetry/opentelemetry-python/pull/3472)) - ## Version 1.20.0/0.41b0 (2023-09-04) - Modify Prometheus exporter to translate non-monotonic Sums into Gauges @@ -323,7 +325,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Default LogRecord observed_timestamp to current timestamp [#3377](https://github.com/open-telemetry/opentelemetry-python/pull/3377)) - ## Version 1.18.0/0.39b0 (2023-05-19) - Select histogram aggregation with an environment variable @@ -343,7 +344,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Add benchmark tests for metrics ([#3267](https://github.com/open-telemetry/opentelemetry-python/pull/3267)) - ## Version 1.17.0/0.38b0 (2023-03-22) - Implement LowMemory temporality @@ -363,7 +363,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Version 1.16.0/0.37b0 (2023-02-17) -- Change ``__all__`` to be statically defined. +- Change `__all__` to be statically defined. ([#3143](https://github.com/open-telemetry/opentelemetry-python/pull/3143)) - Remove the ability to set a global metric prefix for Prometheus exporter ([#3137](https://github.com/open-telemetry/opentelemetry-python/pull/3137)) @@ -403,7 +403,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ([#3027](https://github.com/open-telemetry/opentelemetry-python/pull/3027)) - Update logging to include logging api as per specification ([#3038](https://github.com/open-telemetry/opentelemetry-python/pull/3038)) -- Fix: Avoid generator in metrics _ViewInstrumentMatch.collect() +- Fix: Avoid generator in metrics \_ViewInstrumentMatch.collect() ([#3035](https://github.com/open-telemetry/opentelemetry-python/pull/3035) - [exporter-otlp-proto-grpc] add user agent string ([#3009](https://github.com/open-telemetry/opentelemetry-python/pull/3009)) @@ -1691,7 +1691,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Remove dependency on 'backoff' library ([#3679](https://github.com/open-telemetry/opentelemetry-python/pull/3679)) - - Make create_gauge non-abstract method ([#3817](https://github.com/open-telemetry/opentelemetry-python/pull/3817)) - Make `tracer.start_as_current_span()` decorator work with async functions From 5c6d89b9fe3348667ef97034000c23ebcebf25e1 Mon Sep 17 00:00:00 2001 From: PrabhakarJuzgar Date: Wed, 18 Dec 2024 09:12:25 +0000 Subject: [PATCH 03/10] Remove unicode chars and unwanted ignore --- CHANGELOG.md | 2 +- .../sdk/metrics/_internal/exemplar/exemplar_reservoir.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6161d38e70..3e3ea7e207 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -277,7 +277,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Version 1.21.0/0.42b0 (2023-11-01) - Fix `SumAggregation` -  ([#3390](https://github.com/open-telemetry/opentelemetry-python/pull/3390)) + ([#3390](https://github.com/open-telemetry/opentelemetry-python/pull/3390)) - Fix handling of empty metric collection cycles ([#3335](https://github.com/open-telemetry/opentelemetry-python/pull/3335)) - Fix error when no LoggerProvider configured for LoggingHandler diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/exemplar/exemplar_reservoir.py b/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/exemplar/exemplar_reservoir.py index d4feb877c2..3c2db8d6da 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/exemplar/exemplar_reservoir.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/exemplar/exemplar_reservoir.py @@ -184,7 +184,7 @@ def collect(self, point_attributes: Attributes) -> List[Exemplar]: exemplars = [ e for e in ( - bucket.collect(point_attributes) # type: ignore + bucket.collect(point_attributes) for _, bucket in sorted(self._reservoir_storage.items()) ) if e is not None From fc6c6e504bb79201bb9248f010d1eea9d878b0dd Mon Sep 17 00:00:00 2001 From: PrabhakarJuzgar Date: Wed, 18 Dec 2024 09:28:57 +0000 Subject: [PATCH 04/10] Remove unnecessary changes from CHANGELOG --- CHANGELOG.md | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3e3ea7e207..f1584a89fa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -129,20 +129,20 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Improve resource field structure for LogRecords ([#3972](https://github.com/open-telemetry/opentelemetry-python/pull/3972)) - Update Semantic Conventions code generation scripts: - - fix namespace exclusion that resulted in dropping `os` and `net` namespaces. + - fix namespace exclusion that resulted in dropping `os` and `net` namespaces. - add `Final` decorator to constants to prevent collisions - enable mypy and fix detected issues - allow to drop specific attributes in preparation for Semantic Conventions v1.26.0 - ([#3973](https://github.com/open-telemetry/opentelemetry-python/pull/3966)) + ([#3973](https://github.com/open-telemetry/opentelemetry-python/pull/3966)) - Update semantic conventions to version 1.26.0. ([#3964](https://github.com/open-telemetry/opentelemetry-python/pull/3964)) - Use semconv exception attributes for record exceptions in spans ([#3979](https://github.com/open-telemetry/opentelemetry-python/pull/3979)) -- Fix \_encode_events assumes events.attributes.dropped exists +- Fix _encode_events assumes events.attributes.dropped exists ([#3965](https://github.com/open-telemetry/opentelemetry-python/pull/3965)) - Validate links at span creation ([#3991](https://github.com/open-telemetry/opentelemetry-python/pull/3991)) -- Add attributes field in `MeterProvider.get_meter` and `InstrumentationScope` +- Add attributes field in `MeterProvider.get_meter` and `InstrumentationScope` ([#4015](https://github.com/open-telemetry/opentelemetry-python/pull/4015)) - Fix inaccessible `SCHEMA_URL` constants in `opentelemetry-semantic-conventions` ([#4069](https://github.com/open-telemetry/opentelemetry-python/pull/4069)) @@ -172,10 +172,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `SpanAttributes`, `ResourceAttributes`, and `MetricInstruments` are deprecated. - Attribute and metric definitions are now grouped by the namespace. - Stable attributes and metrics are moved to `opentelemetry.semconv.attributes` - and `opentelemetry.semconv.metrics` modules. + and `opentelemetry.semconv.metrics` modules. - Stable and experimental attributes and metrics are defined under - `opentelemetry.semconv._incubating` import path. - ([#3586](https://github.com/open-telemetry/opentelemetry-python/pull/3586)) + `opentelemetry.semconv._incubating` import path. + ([#3586](https://github.com/open-telemetry/opentelemetry-python/pull/3586)) - Rename test objects to avoid pytest warnings ([#3823] (https://github.com/open-telemetry/opentelemetry-python/pull/3823)) - Add span flags to OTLP spans and links @@ -187,11 +187,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Fix prometheus metric name and unit conversion ([#3924](https://github.com/open-telemetry/opentelemetry-python/pull/3924)) - this is a breaking change to prometheus metric names so they comply with the - [specification](https://github.com/open-telemetry/opentelemetry-specification/blob/v1.33.0/specification/compatibility/prometheus_and_openmetrics.md#otlp-metric-points-to-prometheus). + [specification](https://github.com/open-telemetry/opentelemetry-specification/blob/v1.33.0/specification/compatibility/prometheus_and_openmetrics.md#otlp-metric-points-to-prometheus). - you can temporarily opt-out of the unit normalization by setting the environment variable - `OTEL_PYTHON_EXPERIMENTAL_DISABLE_PROMETHEUS_UNIT_NORMALIZATION=true` + `OTEL_PYTHON_EXPERIMENTAL_DISABLE_PROMETHEUS_UNIT_NORMALIZATION=true` - common unit abbreviations are converted to Prometheus conventions (`s` -> `seconds`), - following the [collector's implementation](https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/c0b51136575aa7ba89326d18edb4549e7e1bbdb9/pkg/translator/prometheus/normalize_name.go#L108) + following the [collector's implementation](https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/c0b51136575aa7ba89326d18edb4549e7e1bbdb9/pkg/translator/prometheus/normalize_name.go#L108) - repeated `_` are replaced with a single `_` - unit annotations (enclosed in curly braces like `{requests}`) are stripped away - units with slash are converted e.g. `m/s` -> `meters_per_second`. @@ -363,7 +363,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Version 1.16.0/0.37b0 (2023-02-17) -- Change `__all__` to be statically defined. +- Change ``__all__`` to be statically defined. ([#3143](https://github.com/open-telemetry/opentelemetry-python/pull/3143)) - Remove the ability to set a global metric prefix for Prometheus exporter ([#3137](https://github.com/open-telemetry/opentelemetry-python/pull/3137)) @@ -403,7 +403,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ([#3027](https://github.com/open-telemetry/opentelemetry-python/pull/3027)) - Update logging to include logging api as per specification ([#3038](https://github.com/open-telemetry/opentelemetry-python/pull/3038)) -- Fix: Avoid generator in metrics \_ViewInstrumentMatch.collect() +- Fix: Avoid generator in metrics _ViewInstrumentMatch.collect() ([#3035](https://github.com/open-telemetry/opentelemetry-python/pull/3035) - [exporter-otlp-proto-grpc] add user agent string ([#3009](https://github.com/open-telemetry/opentelemetry-python/pull/3009)) From 822e8414b651b10a909a0359d0bb733ff41011e8 Mon Sep 17 00:00:00 2001 From: Prabhakar Juzgar Date: Fri, 20 Dec 2024 13:28:54 +0000 Subject: [PATCH 05/10] Change importlib_metadata to importlib.metadata --- opentelemetry-api/src/opentelemetry/context/__init__.py | 8 ++++---- opentelemetry-api/src/opentelemetry/propagate/__init__.py | 4 ++-- .../src/opentelemetry/util/_importlib_metadata.py | 2 +- opentelemetry-api/src/opentelemetry/util/_providers.py | 6 +++--- .../src/opentelemetry/sdk/error_handler/__init__.py | 4 ++-- 5 files changed, 12 insertions(+), 12 deletions(-) diff --git a/opentelemetry-api/src/opentelemetry/context/__init__.py b/opentelemetry-api/src/opentelemetry/context/__init__.py index b9a5a17b2f..e61b4396ad 100644 --- a/opentelemetry-api/src/opentelemetry/context/__init__.py +++ b/opentelemetry-api/src/opentelemetry/context/__init__.py @@ -41,8 +41,8 @@ def _load_runtime_context() -> _RuntimeContext: try: return next( # type: ignore - iter( # type: ignore - entry_points( # type: ignore + iter( + entry_points( group="opentelemetry_context", name=configured_context, ) @@ -55,8 +55,8 @@ def _load_runtime_context() -> _RuntimeContext: default_context, ) return next( # type: ignore - iter( # type: ignore - entry_points( # type: ignore + iter( + entry_points( group="opentelemetry_context", name=default_context, ) diff --git a/opentelemetry-api/src/opentelemetry/propagate/__init__.py b/opentelemetry-api/src/opentelemetry/propagate/__init__.py index afe406fb59..c4afc11e93 100644 --- a/opentelemetry-api/src/opentelemetry/propagate/__init__.py +++ b/opentelemetry-api/src/opentelemetry/propagate/__init__.py @@ -136,8 +136,8 @@ def inject( try: propagators.append( # type: ignore next( # type: ignore - iter( # type: ignore - entry_points( # type: ignore + iter( + entry_points( group="opentelemetry_propagator", name=propagator, ) diff --git a/opentelemetry-api/src/opentelemetry/util/_importlib_metadata.py b/opentelemetry-api/src/opentelemetry/util/_importlib_metadata.py index 2457630ba2..744d61dd91 100644 --- a/opentelemetry-api/src/opentelemetry/util/_importlib_metadata.py +++ b/opentelemetry-api/src/opentelemetry/util/_importlib_metadata.py @@ -14,7 +14,7 @@ # FIXME: Use importlib.metadata when support for 3.11 is dropped if the rest of # the supported versions at that time have the same API. -from importlib_metadata import ( # type: ignore +from importlib.metadata import ( Distribution, EntryPoint, EntryPoints, diff --git a/opentelemetry-api/src/opentelemetry/util/_providers.py b/opentelemetry-api/src/opentelemetry/util/_providers.py index b748eadfe0..08aede519d 100644 --- a/opentelemetry-api/src/opentelemetry/util/_providers.py +++ b/opentelemetry-api/src/opentelemetry/util/_providers.py @@ -38,9 +38,9 @@ def _load_provider( return cast( Provider, - next( # type: ignore - iter( # type: ignore - entry_points( # type: ignore + next( + iter( + entry_points( group=f"opentelemetry_{provider}", name=provider_name, ) diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/error_handler/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/error_handler/__init__.py index 619c24be2d..c10ac1f6ec 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/error_handler/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/error_handler/__init__.py @@ -116,11 +116,11 @@ def __exit__(self, exc_type, exc_value, traceback) -> Optional[bool]: # type: i plugin_handled = False - error_handler_entry_points = entry_points( # type: ignore + error_handler_entry_points = entry_points( group="opentelemetry_error_handler" ) - for error_handler_entry_point in error_handler_entry_points: # type: ignore + for error_handler_entry_point in error_handler_entry_points: error_handler_class = error_handler_entry_point.load() # type: ignore if issubclass(error_handler_class, exc_value.__class__): # type: ignore From 192db0c2bb911726d7d9999bf98efa5af385a23d Mon Sep 17 00:00:00 2001 From: Prabhakar Juzgar Date: Fri, 20 Dec 2024 13:59:11 +0000 Subject: [PATCH 06/10] Address review comment --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f1584a89fa..2a32b59bfb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Unreleased -- Run mypy on SDK resources +- Add type hints to SDK resources ([#4360](https://github.com/open-telemetry/opentelemetry-python/pull/4360)) ## Version 1.29.0/0.50b0 (2024-12-11) From 853c00fbdec9d190d75d06844133230a7d4d0113 Mon Sep 17 00:00:00 2001 From: Prabhakar Juzgar Date: Sat, 21 Dec 2024 10:17:12 +0000 Subject: [PATCH 07/10] Use importlib_metadata if importlib.data is unavailable --- .../opentelemetry/util/_importlib_metadata.py | 33 +++++++++++++------ 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/opentelemetry-api/src/opentelemetry/util/_importlib_metadata.py b/opentelemetry-api/src/opentelemetry/util/_importlib_metadata.py index 744d61dd91..baeed85ba6 100644 --- a/opentelemetry-api/src/opentelemetry/util/_importlib_metadata.py +++ b/opentelemetry-api/src/opentelemetry/util/_importlib_metadata.py @@ -14,16 +14,29 @@ # FIXME: Use importlib.metadata when support for 3.11 is dropped if the rest of # the supported versions at that time have the same API. -from importlib.metadata import ( - Distribution, - EntryPoint, - EntryPoints, - PackageNotFoundError, - distributions, - entry_points, - requires, - version, -) +try: + from importlib.metadata import ( + Distribution, + EntryPoint, + EntryPoints, + PackageNotFoundError, + distributions, + entry_points, + requires, + version, + ) +except Exception: + from importlib_metadata import ( + Distribution, + EntryPoint, + EntryPoints, + PackageNotFoundError, + distributions, + entry_points, + requires, + version, + ) + __all__ = [ "entry_points", From d42a65063214d74b3bca65fbfcfdfb9cdbedca0c Mon Sep 17 00:00:00 2001 From: Prabhakar Juzgar Date: Mon, 23 Dec 2024 18:10:11 +0000 Subject: [PATCH 08/10] Remove unnecessary changes --- .../src/opentelemetry/context/__init__.py | 8 ++--- .../src/opentelemetry/propagate/__init__.py | 4 +-- .../opentelemetry/util/_importlib_metadata.py | 33 ++++++------------- .../sdk/error_handler/__init__.py | 2 +- 4 files changed, 17 insertions(+), 30 deletions(-) diff --git a/opentelemetry-api/src/opentelemetry/context/__init__.py b/opentelemetry-api/src/opentelemetry/context/__init__.py index e61b4396ad..b9a5a17b2f 100644 --- a/opentelemetry-api/src/opentelemetry/context/__init__.py +++ b/opentelemetry-api/src/opentelemetry/context/__init__.py @@ -41,8 +41,8 @@ def _load_runtime_context() -> _RuntimeContext: try: return next( # type: ignore - iter( - entry_points( + iter( # type: ignore + entry_points( # type: ignore group="opentelemetry_context", name=configured_context, ) @@ -55,8 +55,8 @@ def _load_runtime_context() -> _RuntimeContext: default_context, ) return next( # type: ignore - iter( - entry_points( + iter( # type: ignore + entry_points( # type: ignore group="opentelemetry_context", name=default_context, ) diff --git a/opentelemetry-api/src/opentelemetry/propagate/__init__.py b/opentelemetry-api/src/opentelemetry/propagate/__init__.py index c4afc11e93..afe406fb59 100644 --- a/opentelemetry-api/src/opentelemetry/propagate/__init__.py +++ b/opentelemetry-api/src/opentelemetry/propagate/__init__.py @@ -136,8 +136,8 @@ def inject( try: propagators.append( # type: ignore next( # type: ignore - iter( - entry_points( + iter( # type: ignore + entry_points( # type: ignore group="opentelemetry_propagator", name=propagator, ) diff --git a/opentelemetry-api/src/opentelemetry/util/_importlib_metadata.py b/opentelemetry-api/src/opentelemetry/util/_importlib_metadata.py index baeed85ba6..2457630ba2 100644 --- a/opentelemetry-api/src/opentelemetry/util/_importlib_metadata.py +++ b/opentelemetry-api/src/opentelemetry/util/_importlib_metadata.py @@ -14,29 +14,16 @@ # FIXME: Use importlib.metadata when support for 3.11 is dropped if the rest of # the supported versions at that time have the same API. -try: - from importlib.metadata import ( - Distribution, - EntryPoint, - EntryPoints, - PackageNotFoundError, - distributions, - entry_points, - requires, - version, - ) -except Exception: - from importlib_metadata import ( - Distribution, - EntryPoint, - EntryPoints, - PackageNotFoundError, - distributions, - entry_points, - requires, - version, - ) - +from importlib_metadata import ( # type: ignore + Distribution, + EntryPoint, + EntryPoints, + PackageNotFoundError, + distributions, + entry_points, + requires, + version, +) __all__ = [ "entry_points", diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/error_handler/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/error_handler/__init__.py index c10ac1f6ec..6f8266e720 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/error_handler/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/error_handler/__init__.py @@ -116,7 +116,7 @@ def __exit__(self, exc_type, exc_value, traceback) -> Optional[bool]: # type: i plugin_handled = False - error_handler_entry_points = entry_points( + error_handler_entry_points = entry_points( # type: ignore group="opentelemetry_error_handler" ) From 825c727c3f81f571ab985c62f49977207886614d Mon Sep 17 00:00:00 2001 From: Prabhakar Juzgar Date: Mon, 23 Dec 2024 18:26:24 +0000 Subject: [PATCH 09/10] do not change implementation final --- opentelemetry-api/src/opentelemetry/util/_providers.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/opentelemetry-api/src/opentelemetry/util/_providers.py b/opentelemetry-api/src/opentelemetry/util/_providers.py index 08aede519d..b748eadfe0 100644 --- a/opentelemetry-api/src/opentelemetry/util/_providers.py +++ b/opentelemetry-api/src/opentelemetry/util/_providers.py @@ -38,9 +38,9 @@ def _load_provider( return cast( Provider, - next( - iter( - entry_points( + next( # type: ignore + iter( # type: ignore + entry_points( # type: ignore group=f"opentelemetry_{provider}", name=provider_name, ) From ce4c3a7d5ce9a708b25538db8ec57cdf4ba6a8d5 Mon Sep 17 00:00:00 2001 From: Prabhakar Juzgar Date: Tue, 24 Dec 2024 17:26:17 +0000 Subject: [PATCH 10/10] suppress specific warnings --- .../sdk/error_handler/__init__.py | 22 +++++++++---------- .../_internal/exemplar/exemplar_reservoir.py | 14 ++++++------ 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/error_handler/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/error_handler/__init__.py index 6f8266e720..cb731cf0a4 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/error_handler/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/error_handler/__init__.py @@ -70,7 +70,7 @@ def _handle(self, error: Exception, *args, **kwargs): class ErrorHandler(ABC): @abstractmethod - def _handle(self, error: Exception, *args, **kwargs) -> None: # type: ignore + def _handle(self, error: Exception, *args, **kwargs) -> None: # type: ignore[misc, no-untyped-def] """ Handle an exception """ @@ -84,7 +84,7 @@ class _DefaultErrorHandler(ErrorHandler): """ # pylint: disable=useless-return - def _handle(self, error: Exception, *args, **kwargs) -> None: # type: ignore + def _handle(self, error: Exception, *args, **kwargs) -> None: # type: ignore[no-untyped-def] logger.exception("Error handled by default error handler: ") return None @@ -110,22 +110,22 @@ def __enter__(self) -> None: pass # pylint: disable=no-self-use - def __exit__(self, exc_type, exc_value, traceback) -> Optional[bool]: # type: ignore + def __exit__(self, exc_type, exc_value, traceback) -> Optional[bool]: # type: ignore[no-untyped-def] if exc_value is None: # type: ignore return None plugin_handled = False - error_handler_entry_points = entry_points( # type: ignore + error_handler_entry_points = entry_points( # type: ignore[misc] group="opentelemetry_error_handler" ) - for error_handler_entry_point in error_handler_entry_points: - error_handler_class = error_handler_entry_point.load() # type: ignore + for error_handler_entry_point in error_handler_entry_points: # type: ignore[misc] + error_handler_class = error_handler_entry_point.load() # type: ignore[misc] - if issubclass(error_handler_class, exc_value.__class__): # type: ignore + if issubclass(error_handler_class, exc_value.__class__): # type: ignore[misc] try: - error_handler_class()._handle(exc_value) # type: ignore + error_handler_class()._handle(exc_value) # type: ignore[misc] plugin_handled = True # pylint: disable=broad-exception-caught @@ -134,11 +134,11 @@ def __exit__(self, exc_type, exc_value, traceback) -> Optional[bool]: # type: i "%s error while handling error" " %s by error handler %s", error_handling_error.__class__.__name__, - exc_value.__class__.__name__, # type: ignore - error_handler_class.__name__, # type: ignore + exc_value.__class__.__name__, # type: ignore[misc] + error_handler_class.__name__, # type: ignore[misc] ) if not plugin_handled: - _DefaultErrorHandler()._handle(exc_value) # type: ignore + _DefaultErrorHandler()._handle(exc_value) # type: ignore[misc] return True diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/exemplar/exemplar_reservoir.py b/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/exemplar/exemplar_reservoir.py index 3c2db8d6da..7602f18362 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/exemplar/exemplar_reservoir.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/exemplar/exemplar_reservoir.py @@ -129,7 +129,7 @@ def collect(self, point_attributes: Attributes) -> Optional[Exemplar]: { k: v for k, v in self.__attributes.items() - if k not in point_attributes # type: ignore + if k not in point_attributes # type: ignore[operator] } if self.__attributes else None @@ -162,8 +162,8 @@ class BucketIndexError(ValueError): class FixedSizeExemplarReservoirABC(ExemplarReservoir): """Abstract class for a reservoir with fixed size.""" - def __init__(self, size: int, **kwargs) -> None: # type: ignore - super().__init__(**kwargs) # type: ignore + def __init__(self, size: int, **kwargs) -> None: # type: ignore[no-untyped-def] + super().__init__(**kwargs) # type: ignore[misc] self._size: int = size self._reservoir_storage: Mapping[int, ExemplarBucket] = defaultdict( ExemplarBucket @@ -257,8 +257,8 @@ class SimpleFixedSizeExemplarReservoir(FixedSizeExemplarReservoirABC): https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/sdk.md#simplefixedsizeexemplarreservoir """ - def __init__(self, size: int = 1, **kwargs) -> None: # type: ignore - super().__init__(size, **kwargs) # type: ignore + def __init__(self, size: int = 1, **kwargs) -> None: # type: ignore[no-untyped-def] + super().__init__(size, **kwargs) # type: ignore[misc] self._measurements_seen: int = 0 def _reset(self) -> None: @@ -292,8 +292,8 @@ class AlignedHistogramBucketExemplarReservoir(FixedSizeExemplarReservoirABC): https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/sdk.md#alignedhistogrambucketexemplarreservoir """ - def __init__(self, boundaries: Sequence[float], **kwargs) -> None: # type: ignore - super().__init__(len(boundaries) + 1, **kwargs) # type: ignore + def __init__(self, boundaries: Sequence[float], **kwargs) -> None: # type: ignore[no-untyped-def] + super().__init__(len(boundaries) + 1, **kwargs) # type: ignore[misc] self._boundaries: Sequence[float] = boundaries def offer(