From bceff8c1c82588d672b2aad85bfece4555409937 Mon Sep 17 00:00:00 2001 From: Diego Hurtado Date: Mon, 21 Nov 2022 17:17:21 +0100 Subject: [PATCH 01/29] Remove usage of pkg_resources Fixes #2927 --- .../pyproject.toml | 1 + .../opentelemetry/exporter/opencensus/util.py | 14 +- .../tests/encoder/test_v1_json.py | 5 +- .../tests/encoder/test_v2_json.py | 5 +- .../tests/encoder/test_v2_protobuf.py | 5 +- .../src/opentelemetry/context/__init__.py | 38 ++- .../metrics/_internal/__init__.py | 2 +- .../src/opentelemetry/propagate/__init__.py | 32 +- .../src/opentelemetry/trace/__init__.py | 2 +- .../src/opentelemetry/util/_providers.py | 39 ++- opentelemetry-api/tests/__init__.py | 9 - .../tests/propagators/test_propagators.py | 91 +++++- opentelemetry-api/tests/trace/test_status.py | 6 +- opentelemetry-api/tests/util/test_re.py | 8 +- opentelemetry-proto/tests/test_proto.py | 7 +- .../sdk/_configuration/__init__.py | 44 ++- .../sdk/error_handler/__init__.py | 24 +- .../opentelemetry/sdk/resources/__init__.py | 14 +- .../tests/error_handler/test_error_handler.py | 90 +++-- opentelemetry-sdk/tests/test_configurator.py | 307 +++++++++++++----- .../tests/test_semconv.py | 7 +- .../test_asyncio.py | 17 +- .../test_threads.py | 17 +- .../test_client_server/test_asyncio.py | 17 +- .../test_client_server/test_threads.py | 17 +- .../request_handler.py | 15 + .../test_asyncio.py | 17 +- .../test_threads.py | 17 +- .../test_late_span_finish/test_asyncio.py | 17 +- .../test_late_span_finish/test_threads.py | 17 +- .../test_listener_per_request/test_asyncio.py | 17 +- .../test_listener_per_request/test_threads.py | 17 +- .../test_multiple_callbacks/test_asyncio.py | 17 +- .../test_multiple_callbacks/test_threads.py | 17 +- .../test_nested_callbacks/test_asyncio.py | 17 +- .../test_nested_callbacks/test_threads.py | 19 +- .../test_asyncio.py | 17 +- .../test_threads.py | 17 +- tox.ini | 2 +- 39 files changed, 811 insertions(+), 230 deletions(-) diff --git a/exporter/opentelemetry-exporter-opencensus/pyproject.toml b/exporter/opentelemetry-exporter-opencensus/pyproject.toml index fe03741dfb9..86a0d921ccb 100644 --- a/exporter/opentelemetry-exporter-opencensus/pyproject.toml +++ b/exporter/opentelemetry-exporter-opencensus/pyproject.toml @@ -32,6 +32,7 @@ dependencies = [ "opentelemetry-sdk ~= 1.3", "protobuf ~= 3.13", "setuptools >= 16.0", + "importlib-metadata >= 5.0.0; python_version=='3.7'" ] [project.optional-dependencies] diff --git a/exporter/opentelemetry-exporter-opencensus/src/opentelemetry/exporter/opencensus/util.py b/exporter/opentelemetry-exporter-opencensus/src/opentelemetry/exporter/opencensus/util.py index e08b884c3fa..8334d7b8a92 100644 --- a/exporter/opentelemetry-exporter-opencensus/src/opentelemetry/exporter/opencensus/util.py +++ b/exporter/opentelemetry-exporter-opencensus/src/opentelemetry/exporter/opencensus/util.py @@ -15,8 +15,16 @@ import os import socket import time +from sys import version_info -import pkg_resources +# FIXME remove when support for 3.7 is dropped. +if version_info.minor == 7: + # pylint: disable=import-error + from importlib_metadata import version +else: + from importlib.metadata import version + +# pylint: disable=wrong-import-position from google.protobuf.timestamp_pb2 import Timestamp from opencensus.proto.agent.common.v1 import common_pb2 from opencensus.proto.trace.v1 import trace_pb2 @@ -26,9 +34,7 @@ ) from opentelemetry.trace import SpanKind -OPENTELEMETRY_VERSION = pkg_resources.get_distribution( - "opentelemetry-api" -).version +OPENTELEMETRY_VERSION = version("opentelemetry-api") def proto_timestamp_from_time_ns(time_ns): diff --git a/exporter/opentelemetry-exporter-zipkin-json/tests/encoder/test_v1_json.py b/exporter/opentelemetry-exporter-zipkin-json/tests/encoder/test_v1_json.py index 59a750eb51f..778ed74e8d7 100644 --- a/exporter/opentelemetry-exporter-zipkin-json/tests/encoder/test_v1_json.py +++ b/exporter/opentelemetry-exporter-zipkin-json/tests/encoder/test_v1_json.py @@ -28,7 +28,10 @@ ) from opentelemetry.trace import TraceFlags, format_span_id, format_trace_id -from .common_tests import TEST_SERVICE_NAME, CommonEncoderTestCases +from .common_tests import ( # pylint: disable=import-error + TEST_SERVICE_NAME, + CommonEncoderTestCases, +) # pylint: disable=protected-access diff --git a/exporter/opentelemetry-exporter-zipkin-json/tests/encoder/test_v2_json.py b/exporter/opentelemetry-exporter-zipkin-json/tests/encoder/test_v2_json.py index 85cc91a0d9d..37a0414fcad 100644 --- a/exporter/opentelemetry-exporter-zipkin-json/tests/encoder/test_v2_json.py +++ b/exporter/opentelemetry-exporter-zipkin-json/tests/encoder/test_v2_json.py @@ -28,7 +28,10 @@ ) from opentelemetry.trace import SpanKind, TraceFlags -from .common_tests import TEST_SERVICE_NAME, CommonEncoderTestCases +from .common_tests import ( # pylint: disable=import-error + TEST_SERVICE_NAME, + CommonEncoderTestCases, +) # pylint: disable=protected-access diff --git a/exporter/opentelemetry-exporter-zipkin-proto-http/tests/encoder/test_v2_protobuf.py b/exporter/opentelemetry-exporter-zipkin-proto-http/tests/encoder/test_v2_protobuf.py index 8ce61a92a1d..2f2c894e4a7 100644 --- a/exporter/opentelemetry-exporter-zipkin-proto-http/tests/encoder/test_v2_protobuf.py +++ b/exporter/opentelemetry-exporter-zipkin-proto-http/tests/encoder/test_v2_protobuf.py @@ -28,7 +28,10 @@ ) from opentelemetry.trace import SpanKind -from .common_tests import TEST_SERVICE_NAME, CommonEncoderTestCases +from .common_tests import ( # pylint: disable=import-error + TEST_SERVICE_NAME, + CommonEncoderTestCases, +) # pylint: disable=protected-access diff --git a/opentelemetry-api/src/opentelemetry/context/__init__.py b/opentelemetry-api/src/opentelemetry/context/__init__.py index 306f0937d53..8c82d42d234 100644 --- a/opentelemetry-api/src/opentelemetry/context/__init__.py +++ b/opentelemetry-api/src/opentelemetry/context/__init__.py @@ -18,9 +18,16 @@ from functools import wraps from os import environ from uuid import uuid4 +from sys import version_info -from pkg_resources import iter_entry_points +# FIXME remove when support for 3.7 is dropped. +if version_info.minor == 7: + # pylint: disable=import-error + from importlib_metadata import entry_points # type: ignore +else: + from importlib.metadata import entry_points +# pylint: disable=wrong-import-position from opentelemetry.context.context import Context, _RuntimeContext from opentelemetry.environment_variables import OTEL_PYTHON_CONTEXT @@ -41,25 +48,38 @@ def _load_runtime_context(func: _F) -> _F: @wraps(func) # type: ignore[misc] def wrapper( # type: ignore[misc] *args: typing.Tuple[typing.Any, typing.Any], - **kwargs: typing.Dict[typing.Any, typing.Any] + **kwargs: typing.Dict[typing.Any, typing.Any], ) -> typing.Optional[typing.Any]: global _RUNTIME_CONTEXT # pylint: disable=global-statement with _RUNTIME_CONTEXT_LOCK: if _RUNTIME_CONTEXT is None: - # FIXME use a better implementation of a configuration manager to avoid having - # to get configuration values straight from environment variables + # FIXME use a better implementation of a configuration manager + # to avoid having to get configuration values straight from + # environment variables default_context = "contextvars_context" configured_context = environ.get( OTEL_PYTHON_CONTEXT, default_context ) # type: str try: - _RUNTIME_CONTEXT = next( - iter_entry_points( - "opentelemetry_context", configured_context - ) - ).load()() + if version_info.minor <= 9: + for entry_point in entry_points()[ # type: ignore + "opentelemetry_context" + ]: + if entry_point.name == configured_context: # type: ignore + _RUNTIME_CONTEXT = entry_point.load()() # type: ignore + break + else: + raise Exception( + f"No entry point found for configured_context:" + f" {configured_context}" + ) + else: + _RUNTIME_CONTEXT = entry_points( # type: ignore + group="opentelemetry_context", + name=configured_context, + )[0].load()() except Exception: # pylint: disable=broad-except logger.error( "Failed to load context: %s", configured_context diff --git a/opentelemetry-api/src/opentelemetry/metrics/_internal/__init__.py b/opentelemetry-api/src/opentelemetry/metrics/_internal/__init__.py index ccde6900d88..630e9c4053d 100644 --- a/opentelemetry-api/src/opentelemetry/metrics/_internal/__init__.py +++ b/opentelemetry-api/src/opentelemetry/metrics/_internal/__init__.py @@ -119,7 +119,7 @@ def get_meter( version: Optional. The version string of the instrumenting library. Usually this should be the same as - ``pkg_resources.get_distribution(instrumenting_library_name).version``. + ``importlib.metadata.version(instrumenting_library_name)``. schema_url: Optional. Specifies the Schema URL of the emitted telemetry. """ diff --git a/opentelemetry-api/src/opentelemetry/propagate/__init__.py b/opentelemetry-api/src/opentelemetry/propagate/__init__.py index a39d8a44d1f..ef10844ebd0 100644 --- a/opentelemetry-api/src/opentelemetry/propagate/__init__.py +++ b/opentelemetry-api/src/opentelemetry/propagate/__init__.py @@ -71,13 +71,19 @@ def example_route(): import typing from logging import getLogger from os import environ - -from pkg_resources import iter_entry_points +from sys import version_info from opentelemetry.context.context import Context from opentelemetry.environment_variables import OTEL_PROPAGATORS from opentelemetry.propagators import composite, textmap +# FIXME remove when support for 3.7 is dropped. +if version_info.minor == 7: + # pylint: disable=import-error + from importlib_metadata import entry_points # type: ignore +else: + from importlib.metadata import entry_points + logger = getLogger(__name__) @@ -129,14 +135,26 @@ def inject( "tracecontext,baggage", ) + for propagator in environ_propagators.split(","): propagator = propagator.strip() try: - propagators.append( # type: ignore - next( # type: ignore - iter_entry_points("opentelemetry_propagator", propagator) - ).load()() - ) + + if version_info.minor <= 9: + + for entry_point in entry_points().get( # type: ignore + "opentelemetry_propagator", [] + ): + if entry_point.name == propagator: # type: ignore + propagators.append(entry_point.load()()) # type: ignore + else: + + propagators.append( # type: ignore + entry_points( # type: ignore + group="opentelemetry_propagator", name=propagator + )[0].load()() + ) + except Exception: # pylint: disable=broad-except logger.exception( "Failed to load configured propagator `%s`", propagator diff --git a/opentelemetry-api/src/opentelemetry/trace/__init__.py b/opentelemetry-api/src/opentelemetry/trace/__init__.py index f0e92f14bb8..304df227542 100644 --- a/opentelemetry-api/src/opentelemetry/trace/__init__.py +++ b/opentelemetry-api/src/opentelemetry/trace/__init__.py @@ -216,7 +216,7 @@ def get_tracer( instrumenting_library_version: Optional. The version string of the instrumenting library. Usually this should be the same as - ``pkg_resources.get_distribution(instrumenting_library_name).version``. + ``importlib.metadata.version(instrumenting_library_name)``. schema_url: Optional. Specifies the Schema URL of the emitted telemetry. """ diff --git a/opentelemetry-api/src/opentelemetry/util/_providers.py b/opentelemetry-api/src/opentelemetry/util/_providers.py index d8feb88d62d..9f700a88df4 100644 --- a/opentelemetry-api/src/opentelemetry/util/_providers.py +++ b/opentelemetry-api/src/opentelemetry/util/_providers.py @@ -14,9 +14,15 @@ from logging import getLogger from os import environ +from sys import version_info from typing import TYPE_CHECKING, TypeVar, cast -from pkg_resources import iter_entry_points +# FIXME remove when support for 3.7 is dropped. +if version_info.minor == 7: + # pylint: disable=import-error + from importlib_metadata import entry_points # type: ignore +else: + from importlib.metadata import entry_points if TYPE_CHECKING: from opentelemetry.metrics import MeterProvider @@ -30,10 +36,27 @@ def _load_provider( provider_environment_variable: str, provider: str ) -> Provider: + try: - entry_point = next( - iter_entry_points( - f"opentelemetry_{provider}", + + if version_info.minor <= 9: + + provider_name = cast( + str, + environ.get( + provider_environment_variable, f"default_{provider}" + ), + ) + + for entry_point in entry_points()[f"opentelemetry_{provider}"]: # type: ignore + if entry_point.name == provider_name: # type: ignore + return cast(Provider, entry_point.load()()) # type: ignore + raise Exception(f"Provider {provider_name} not found") + + return cast( + Provider, + entry_points( # type: ignore + group=f"opentelemetry_{provider}", name=cast( str, environ.get( @@ -41,12 +64,8 @@ def _load_provider( f"default_{provider}", ), ), - ) - ) - return cast( - Provider, - entry_point.load()(), + )[0].load()(), ) except Exception: # pylint: disable=broad-except - logger.error("Failed to load configured provider %s", provider) + logger.exception("Failed to load configured provider %s", provider) raise diff --git a/opentelemetry-api/tests/__init__.py b/opentelemetry-api/tests/__init__.py index bc48946761e..b0a6f428417 100644 --- a/opentelemetry-api/tests/__init__.py +++ b/opentelemetry-api/tests/__init__.py @@ -11,12 +11,3 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. -import pkg_resources - -# naming the tests module as a namespace package ensures that -# relative imports will resolve properly for other test packages, -# as it enables searching for a composite of multiple test modules. -# -# only the opentelemetry-api directory needs this code, as it is -# the first tests module found by pylint during eachdist.py lint -pkg_resources.declare_namespace(__name__) diff --git a/opentelemetry-api/tests/propagators/test_propagators.py b/opentelemetry-api/tests/propagators/test_propagators.py index a8fed620be8..8e06ce2535c 100644 --- a/opentelemetry-api/tests/propagators/test_propagators.py +++ b/opentelemetry-api/tests/propagators/test_propagators.py @@ -17,6 +17,7 @@ from importlib import reload from logging import ERROR from os import environ +from sys import version_info from unittest import TestCase from unittest.mock import Mock, patch @@ -51,31 +52,87 @@ def test_propagators(propagators): reload(opentelemetry.propagate) + if version_info.minor == 7: + entry_points_path = "importlib_metadata.entry_points" + else: + entry_points_path = "importlib.metadata.entry_points" + @patch.dict(environ, {OTEL_PROPAGATORS: "a, b, c "}) @patch("opentelemetry.propagators.composite.CompositePropagator") - @patch("pkg_resources.iter_entry_points") + @patch(entry_points_path) def test_non_default_propagators( - self, mock_iter_entry_points, mock_compositehttppropagator + self, mock_entry_points, mock_compositehttppropagator ): - def iter_entry_points_mock(_, propagator): - return iter( - [ - Mock( - **{ - "load.side_effect": [ - Mock(**{"side_effect": [propagator]}) - ] - } - ) - ] + + if version_info.minor <= 9: + + mock_a = Mock() + mock_a.configure_mock( + **{ + "name": "a", + "load.return_value": Mock(**{"return_value": "a"}), + } + ) + mock_b = Mock() + mock_b.configure_mock( + **{ + "name": "b", + "load.return_value": Mock(**{"return_value": "b"}), + } + ) + mock_c = Mock() + mock_c.configure_mock( + **{ + "name": "c", + "load.return_value": Mock(**{"return_value": "c"}), + } ) - mock_iter_entry_points.configure_mock( - **{"side_effect": iter_entry_points_mock} - ) + mock_entry_points.configure_mock( + **{ + "return_value": { + "opentelemetry_propagator": [mock_a, mock_b, mock_c] + } + } + ) - def test_propagators(propagators): + else: + + mock_entry_points.configure_mock( + **{ + "side_effect": [ + [ + Mock( + **{ + "load.return_value": Mock( + **{"return_value": "a"} + ) + } + ), + ], + [ + Mock( + **{ + "load.return_value": Mock( + **{"return_value": "b"} + ) + } + ) + ], + [ + Mock( + **{ + "load.return_value": Mock( + **{"return_value": "c"} + ) + } + ) + ], + ] + } + ) + def test_propagators(propagators): self.assertEqual(propagators, ["a", "b", "c"]) mock_compositehttppropagator.configure_mock( diff --git a/opentelemetry-api/tests/trace/test_status.py b/opentelemetry-api/tests/trace/test_status.py index 74da78d6c73..6388ae98049 100644 --- a/opentelemetry-api/tests/trace/test_status.py +++ b/opentelemetry-api/tests/trace/test_status.py @@ -35,7 +35,7 @@ def test_invalid_description(self): self.assertEqual(status.description, None) self.assertIn( "Invalid status description type, expected str", - warning.output[0], + warning.output[0], # type: ignore ) def test_description_and_non_error_status(self): @@ -47,7 +47,7 @@ def test_description_and_non_error_status(self): self.assertEqual(status.description, None) self.assertIn( "description should only be set when status_code is set to StatusCode.ERROR", - warning.output[0], + warning.output[0], # type: ignore ) with self.assertLogs(level=WARNING) as warning: @@ -58,7 +58,7 @@ def test_description_and_non_error_status(self): self.assertEqual(status.description, None) self.assertIn( "description should only be set when status_code is set to StatusCode.ERROR", - warning.output[0], + warning.output[0], # type: ignore ) status = Status( diff --git a/opentelemetry-api/tests/util/test_re.py b/opentelemetry-api/tests/util/test_re.py index 99d79ba9d24..85515846ec1 100644 --- a/opentelemetry-api/tests/util/test_re.py +++ b/opentelemetry-api/tests/util/test_re.py @@ -59,11 +59,11 @@ def test_parse_env_headers(self): True, ), ] - for case in inp: - s, expected, warn = case + for case_ in inp: + headers, expected, warn = case_ if warn: with self.assertLogs(level="WARNING") as cm: - self.assertEqual(parse_env_headers(s), dict(expected)) + self.assertEqual(parse_env_headers(headers), dict(expected)) self.assertTrue( "Header format invalid! Header values in environment " "variables must be URL encoded per the OpenTelemetry " @@ -71,4 +71,4 @@ def test_parse_env_headers(self): in cm.records[0].message, ) else: - self.assertEqual(parse_env_headers(s), dict(expected)) + self.assertEqual(parse_env_headers(headers), dict(expected)) diff --git a/opentelemetry-proto/tests/test_proto.py b/opentelemetry-proto/tests/test_proto.py index 6551e4640f3..9670be46276 100644 --- a/opentelemetry-proto/tests/test_proto.py +++ b/opentelemetry-proto/tests/test_proto.py @@ -13,15 +13,12 @@ # limitations under the License. # type: ignore +from importlib.util import find_spec from unittest import TestCase -from pkg_resources import DistributionNotFound, require - class TestInstrumentor(TestCase): def test_proto(self): - try: - require(["opentelemetry-proto"]) - except DistributionNotFound: + if find_spec("opentelemetry.proto") is None: self.fail("opentelemetry-proto not installed") diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/_configuration/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/_configuration/__init__.py index a1ef3b76a27..da137420836 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/_configuration/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/_configuration/__init__.py @@ -21,9 +21,9 @@ import os from abc import ABC, abstractmethod from os import environ +from sys import version_info from typing import Callable, Dict, List, Optional, Sequence, Tuple, Type -from pkg_resources import iter_entry_points from typing_extensions import Literal from opentelemetry._logs import set_logger_provider @@ -58,6 +58,13 @@ from opentelemetry.semconv.resource import ResourceAttributes from opentelemetry.trace import set_tracer_provider +# FIXME remove when support for 3.7 is dropped. +if version_info.minor == 7: + # pylint: disable=import-error + from importlib_metadata import entry_points +else: + from importlib.metadata import entry_points + _EXPORTER_OTLP = "otlp" _EXPORTER_OTLP_PROTO_GRPC = "otlp_proto_grpc" _EXPORTER_OTLP_PROTO_HTTP = "otlp_proto_http" @@ -90,21 +97,30 @@ def _import_config_components( selected_components: List[str], entry_point_name: str ) -> Sequence[Tuple[str, object]]: - component_entry_points = { - ep.name: ep for ep in iter_entry_points(entry_point_name) - } - component_impls = [] - for selected_component in selected_components: - entry_point = component_entry_points.get(selected_component, None) - if not entry_point: - raise RuntimeError( - f"Requested component '{selected_component}' not found in entry points for '{entry_point_name}'" - ) - component_impl = entry_point.load() - component_impls.append((selected_component, component_impl)) + component_implementations = [] + + if version_info.minor <= 9: + + for entry_point in entry_points()[entry_point_name]: + for selected_component in selected_components: + if entry_point.name == selected_component: + component_implementations.append( + (selected_component, entry_point.load()) + ) + else: + + for selected_component in selected_components: + component_implementations.append( + ( + selected_component, + entry_points( + group=entry_point_name, name=selected_component + )[0].load(), + ) + ) - return component_impls + return component_implementations def _get_sampler() -> Optional[str]: diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/error_handler/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/error_handler/__init__.py index 781f42e41a3..66dfae8f239 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/error_handler/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/error_handler/__init__.py @@ -61,8 +61,14 @@ def _handle(self, error: Exception, *args, **kwargs): from abc import ABC, abstractmethod from logging import getLogger +from sys import version_info -from pkg_resources import iter_entry_points +# FIXME remove when support for 3.7 is dropped. +if version_info.minor == 7: + # pylint: disable=import-error + from importlib_metadata import entry_points +else: + from importlib.metadata import entry_points logger = getLogger(__name__) @@ -118,9 +124,19 @@ def __exit__(self, exc_type, exc_value, traceback): plugin_handled = False - for error_handler_entry_point in iter_entry_points( - "opentelemetry_error_handler" - ): + if version_info.minor <= 9: + + error_handler_entry_points = entry_points()[ + "opentelemetry_error_handler" + ] + + else: + + error_handler_entry_points = entry_points( + group="opentelemetry_error_handler" + ) + + for error_handler_entry_point in error_handler_entry_points: error_handler_class = error_handler_entry_point.load() diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py index 56707415e5b..030965e9664 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py @@ -63,8 +63,7 @@ import typing from json import dumps from urllib import parse - -import pkg_resources +from sys import version_info from opentelemetry.attributes import BoundedAttributes from opentelemetry.sdk.environment_variables import ( @@ -74,6 +73,13 @@ from opentelemetry.semconv.resource import ResourceAttributes from opentelemetry.util.types import AttributeValue +# FIXME remove when support for 3.7 is dropped. +if version_info.minor == 7: + # pylint: disable=import-error + from importlib_metadata import version +else: + from importlib.metadata import version + LabelValue = AttributeValue Attributes = typing.Dict[str, LabelValue] logger = logging.getLogger(__name__) @@ -136,9 +142,7 @@ TELEMETRY_SDK_LANGUAGE = ResourceAttributes.TELEMETRY_SDK_LANGUAGE -_OPENTELEMETRY_SDK_VERSION = pkg_resources.get_distribution( - "opentelemetry-sdk" -).version +_OPENTELEMETRY_SDK_VERSION = version("opentelemetry-sdk") class Resource: diff --git a/opentelemetry-sdk/tests/error_handler/test_error_handler.py b/opentelemetry-sdk/tests/error_handler/test_error_handler.py index 1712894464b..bdab5095e17 100644 --- a/opentelemetry-sdk/tests/error_handler/test_error_handler.py +++ b/opentelemetry-sdk/tests/error_handler/test_error_handler.py @@ -14,6 +14,7 @@ # pylint: disable=broad-except from logging import ERROR +from sys import version_info from unittest import TestCase from unittest.mock import Mock, patch @@ -25,16 +26,16 @@ class TestErrorHandler(TestCase): - @patch("opentelemetry.sdk.error_handler.iter_entry_points") - def test_default_error_handler(self, mock_iter_entry_points): + @patch("opentelemetry.sdk.error_handler.entry_points") + def test_default_error_handler(self, mock_entry_points): with self.assertLogs(logger, ERROR): with GlobalErrorHandler(): raise Exception("some exception") # pylint: disable=no-self-use - @patch("opentelemetry.sdk.error_handler.iter_entry_points") - def test_plugin_error_handler(self, mock_iter_entry_points): + @patch("opentelemetry.sdk.error_handler.entry_points") + def test_plugin_error_handler(self, mock_entry_points): class ZeroDivisionErrorHandler(ErrorHandler, ZeroDivisionError): # pylint: disable=arguments-differ @@ -54,14 +55,29 @@ class AssertionErrorHandler(ErrorHandler, AssertionError): **{"load.return_value": AssertionErrorHandler} ) - mock_iter_entry_points.configure_mock( - **{ - "return_value": [ - mock_entry_point_zero_division_error_handler, - mock_entry_point_assertion_error_handler, - ] - } - ) + if version_info.minor <= 9: + + mock_entry_points.configure_mock( + **{ + "return_value": { + "opentelemetry_error_handler": [ + mock_entry_point_zero_division_error_handler, + mock_entry_point_assertion_error_handler, + ] + } + } + ) + + else: + + mock_entry_points.configure_mock( + **{ + "return_value": [ + mock_entry_point_zero_division_error_handler, + mock_entry_point_assertion_error_handler, + ] + } + ) error = ZeroDivisionError() @@ -78,8 +94,8 @@ class AssertionErrorHandler(ErrorHandler, AssertionError): AssertionErrorHandler._handle.assert_called_with(error) - @patch("opentelemetry.sdk.error_handler.iter_entry_points") - def test_error_in_handler(self, mock_iter_entry_points): + @patch("opentelemetry.sdk.error_handler.entry_points") + def test_error_in_handler(self, mock_entry_points): class ErrorErrorHandler(ErrorHandler, ZeroDivisionError): # pylint: disable=arguments-differ @@ -91,9 +107,23 @@ def _handle(self, error: Exception): **{"load.return_value": ErrorErrorHandler} ) - mock_iter_entry_points.configure_mock( - **{"return_value": [mock_entry_point_error_error_handler]} - ) + if version_info.minor <= 9: + + mock_entry_points.configure_mock( + **{ + "return_value": { + "opentelemetry_error_handler": [ + mock_entry_point_error_error_handler + ] + } + } + ) + + else: + + mock_entry_points.configure_mock( + **{"return_value": [mock_entry_point_error_error_handler]} + ) error = ZeroDivisionError() @@ -102,10 +132,8 @@ def _handle(self, error: Exception): raise error # pylint: disable=no-self-use - @patch("opentelemetry.sdk.error_handler.iter_entry_points") - def test_plugin_error_handler_context_manager( - self, mock_iter_entry_points - ): + @patch("opentelemetry.sdk.error_handler.entry_points") + def test_plugin_error_handler_context_manager(self, mock_entry_points): mock_error_handler_instance = Mock() @@ -118,9 +146,23 @@ def __new__(cls): **{"load.return_value": MockErrorHandlerClass} ) - mock_iter_entry_points.configure_mock( - **{"return_value": [mock_entry_point_error_handler]} - ) + if version_info.minor <= 9: + + mock_entry_points.configure_mock( + **{ + "return_value": { + "opentelemetry_error_handler": [ + mock_entry_point_error_handler + ] + } + } + ) + + else: + + mock_entry_points.configure_mock( + **{"return_value": [mock_entry_point_error_handler]} + ) error = IndexError() diff --git a/opentelemetry-sdk/tests/test_configurator.py b/opentelemetry-sdk/tests/test_configurator.py index a27c7a49a1d..183e5dee064 100644 --- a/opentelemetry-sdk/tests/test_configurator.py +++ b/opentelemetry-sdk/tests/test_configurator.py @@ -16,6 +16,7 @@ import logging from os import environ +from sys import version_info from typing import Dict, Iterable, Optional, Sequence from unittest import TestCase from unittest.mock import patch @@ -349,13 +350,28 @@ def test_trace_init_otlp(self): @patch.dict(environ, {OTEL_PYTHON_ID_GENERATOR: "custom_id_generator"}) @patch("opentelemetry.sdk._configuration.IdGenerator", new=IdGenerator) - @patch("opentelemetry.sdk._configuration.iter_entry_points") - def test_trace_init_custom_id_generator(self, mock_iter_entry_points): - mock_iter_entry_points.configure_mock( - return_value=[ - IterEntryPoint("custom_id_generator", CustomIdGenerator) - ] - ) + @patch("opentelemetry.sdk._configuration.entry_points") + def test_trace_init_custom_id_generator(self, mock_entry_points): + if version_info.minor <= 9: + + mock_entry_points.configure_mock( + return_value={ + "opentelemetry_id_generator": ( + IterEntryPoint( + "custom_id_generator", CustomIdGenerator + ), + ) + } + ) + + else: + + mock_entry_points.configure_mock( + return_value=[ + IterEntryPoint("custom_id_generator", CustomIdGenerator) + ] + ) + id_generator_name = _get_id_generator() id_generator = _import_id_generator(id_generator_name) _init_tracing({}, id_generator=id_generator) @@ -372,43 +388,77 @@ def test_trace_init_custom_sampler_with_env_non_existent_entry_point(self): provider = self.set_provider_mock.call_args[0][0] self.assertIsNone(provider.sampler) - @patch("opentelemetry.sdk._configuration.iter_entry_points") + @patch("opentelemetry.sdk._configuration.entry_points") @patch.dict("os.environ", {OTEL_TRACES_SAMPLER: "custom_sampler_factory"}) - def test_trace_init_custom_sampler_with_env(self, mock_iter_entry_points): - mock_iter_entry_points.configure_mock( - return_value=[ - IterEntryPoint( - "custom_sampler_factory", - CustomSamplerFactory.get_custom_sampler, - ) - ] - ) + def test_trace_init_custom_sampler_with_env(self, mock_entry_points): + + if version_info.minor <= 9: + + mock_entry_points.configure_mock( + return_value={ + "opentelemetry_traces_sampler": ( + IterEntryPoint( + "custom_sampler_factory", + CustomSamplerFactory.get_custom_sampler, + ), + ) + } + ) + + else: + + mock_entry_points.configure_mock( + return_value=[ + IterEntryPoint( + "custom_sampler_factory", + CustomSamplerFactory.get_custom_sampler, + ) + ] + ) + sampler_name = _get_sampler() sampler = _import_sampler(sampler_name) _init_tracing({}, sampler=sampler) provider = self.set_provider_mock.call_args[0][0] self.assertIsInstance(provider.sampler, CustomSampler) - @patch("opentelemetry.sdk._configuration.iter_entry_points") + @patch("opentelemetry.sdk._configuration.entry_points") @patch.dict("os.environ", {OTEL_TRACES_SAMPLER: "custom_sampler_factory"}) def test_trace_init_custom_sampler_with_env_bad_factory( - self, mock_iter_entry_points + self, mock_entry_points ): - mock_iter_entry_points.configure_mock( - return_value=[ - IterEntryPoint( - "custom_sampler_factory", - CustomSamplerFactory.empty_get_custom_sampler, - ) - ] - ) + + if version_info.minor <= 9: + + mock_entry_points.configure_mock( + return_value={ + "opentelemetry_traces_sampler": ( + IterEntryPoint( + "custom_sampler_factory", + CustomSamplerFactory.empty_get_custom_sampler, + ), + ) + } + ) + + else: + + mock_entry_points.configure_mock( + return_value=[ + IterEntryPoint( + "custom_sampler_factory", + CustomSamplerFactory.empty_get_custom_sampler, + ) + ] + ) + sampler_name = _get_sampler() sampler = _import_sampler(sampler_name) _init_tracing({}, sampler=sampler) provider = self.set_provider_mock.call_args[0][0] self.assertIsNone(provider.sampler) - @patch("opentelemetry.sdk._configuration.iter_entry_points") + @patch("opentelemetry.sdk._configuration.entry_points") @patch.dict( "os.environ", { @@ -417,23 +467,40 @@ def test_trace_init_custom_sampler_with_env_bad_factory( }, ) def test_trace_init_custom_sampler_with_env_unused_arg( - self, mock_iter_entry_points + self, mock_entry_points ): - mock_iter_entry_points.configure_mock( - return_value=[ - IterEntryPoint( - "custom_sampler_factory", - CustomSamplerFactory.get_custom_sampler, - ) - ] - ) + + if version_info.minor <= 9: + + mock_entry_points.configure_mock( + return_value={ + "opentelemetry_traces_sampler": ( + IterEntryPoint( + "custom_sampler_factory", + CustomSamplerFactory.get_custom_sampler, + ), + ) + } + ) + + else: + + mock_entry_points.configure_mock( + return_value=[ + IterEntryPoint( + "custom_sampler_factory", + CustomSamplerFactory.get_custom_sampler, + ) + ] + ) + sampler_name = _get_sampler() sampler = _import_sampler(sampler_name) _init_tracing({}, sampler=sampler) provider = self.set_provider_mock.call_args[0][0] self.assertIsInstance(provider.sampler, CustomSampler) - @patch("opentelemetry.sdk._configuration.iter_entry_points") + @patch("opentelemetry.sdk._configuration.entry_points") @patch.dict( "os.environ", { @@ -441,17 +508,32 @@ def test_trace_init_custom_sampler_with_env_unused_arg( OTEL_TRACES_SAMPLER_ARG: "0.5", }, ) - def test_trace_init_custom_ratio_sampler_with_env( - self, mock_iter_entry_points - ): - mock_iter_entry_points.configure_mock( - return_value=[ - IterEntryPoint( - "custom_ratio_sampler_factory", - CustomSamplerFactory.get_custom_ratio_sampler, - ) - ] - ) + def test_trace_init_custom_ratio_sampler_with_env(self, mock_entry_points): + + if version_info.minor <= 9: + + mock_entry_points.configure_mock( + return_value={ + "opentelemetry_traces_sampler": ( + IterEntryPoint( + "custom_ratio_sampler_factory", + CustomSamplerFactory.get_custom_ratio_sampler, + ), + ) + } + ) + + else: + + mock_entry_points.configure_mock( + return_value=[ + IterEntryPoint( + "custom_ratio_sampler_factory", + CustomSamplerFactory.get_custom_ratio_sampler, + ) + ] + ) + sampler_name = _get_sampler() sampler = _import_sampler(sampler_name) _init_tracing({}, sampler=sampler) @@ -459,7 +541,7 @@ def test_trace_init_custom_ratio_sampler_with_env( self.assertIsInstance(provider.sampler, CustomRatioSampler) self.assertEqual(provider.sampler.ratio, 0.5) - @patch("opentelemetry.sdk._configuration.iter_entry_points") + @patch("opentelemetry.sdk._configuration.entry_points") @patch.dict( "os.environ", { @@ -468,23 +550,40 @@ def test_trace_init_custom_ratio_sampler_with_env( }, ) def test_trace_init_custom_ratio_sampler_with_env_bad_arg( - self, mock_iter_entry_points + self, mock_entry_points ): - mock_iter_entry_points.configure_mock( - return_value=[ - IterEntryPoint( - "custom_ratio_sampler_factory", - CustomSamplerFactory.get_custom_ratio_sampler, - ) - ] - ) + + if version_info.minor <= 9: + + mock_entry_points.configure_mock( + return_value={ + "opentelemetry_traces_sampler": ( + IterEntryPoint( + "custom_ratio_sampler_factory", + CustomSamplerFactory.get_custom_ratio_sampler, + ), + ) + } + ) + + else: + + mock_entry_points.configure_mock( + return_value=[ + IterEntryPoint( + "custom_ratio_sampler_factory", + CustomSamplerFactory.get_custom_ratio_sampler, + ) + ] + ) + sampler_name = _get_sampler() sampler = _import_sampler(sampler_name) _init_tracing({}, sampler=sampler) provider = self.set_provider_mock.call_args[0][0] self.assertIsNone(provider.sampler) - @patch("opentelemetry.sdk._configuration.iter_entry_points") + @patch("opentelemetry.sdk._configuration.entry_points") @patch.dict( "os.environ", { @@ -492,23 +591,40 @@ def test_trace_init_custom_ratio_sampler_with_env_bad_arg( }, ) def test_trace_init_custom_ratio_sampler_with_env_missing_arg( - self, mock_iter_entry_points + self, mock_entry_points ): - mock_iter_entry_points.configure_mock( - return_value=[ - IterEntryPoint( - "custom_ratio_sampler_factory", - CustomSamplerFactory.get_custom_ratio_sampler, - ) - ] - ) + + if version_info.minor <= 9: + + mock_entry_points.configure_mock( + return_value={ + "opentelemetry_traces_sampler": ( + IterEntryPoint( + "custom_ratio_sampler_factory", + CustomSamplerFactory.get_custom_ratio_sampler, + ), + ) + } + ) + + else: + + mock_entry_points.configure_mock( + return_value=[ + IterEntryPoint( + "custom_ratio_sampler_factory", + CustomSamplerFactory.get_custom_ratio_sampler, + ) + ] + ) + sampler_name = _get_sampler() sampler = _import_sampler(sampler_name) _init_tracing({}, sampler=sampler) provider = self.set_provider_mock.call_args[0][0] self.assertIsNone(provider.sampler) - @patch("opentelemetry.sdk._configuration.iter_entry_points") + @patch("opentelemetry.sdk._configuration.entry_points") @patch.dict( "os.environ", { @@ -517,24 +633,41 @@ def test_trace_init_custom_ratio_sampler_with_env_missing_arg( }, ) def test_trace_init_custom_ratio_sampler_with_env_multiple_entry_points( - self, mock_iter_entry_points + self, mock_entry_points ): - mock_iter_entry_points.configure_mock( - return_value=[ - IterEntryPoint( - "custom_ratio_sampler_factory", - CustomSamplerFactory.get_custom_ratio_sampler, - ), - IterEntryPoint( - "custom_sampler_factory", - CustomSamplerFactory.get_custom_sampler, - ), - IterEntryPoint( - "custom_z_sampler_factory", - CustomSamplerFactory.empty_get_custom_sampler, - ), - ] - ) + + if version_info.minor <= 9: + + mock_entry_points.configure_mock( + return_value={ + "opentelemetry_traces_sampler": ( + IterEntryPoint( + "custom_ratio_sampler_factory", + CustomSamplerFactory.get_custom_ratio_sampler, + ), + IterEntryPoint( + "custom_sampler_factory", + CustomSamplerFactory.get_custom_sampler, + ), + IterEntryPoint( + "custom_z_sampler_factory", + CustomSamplerFactory.empty_get_custom_sampler, + ), + ) + } + ) + + else: + + mock_entry_points.configure_mock( + return_value=[ + IterEntryPoint( + "custom_sampler_factory", + CustomSamplerFactory.get_custom_sampler, + ), + ] + ) + sampler_name = _get_sampler() sampler = _import_sampler(sampler_name) _init_tracing({}, sampler=sampler) diff --git a/opentelemetry-semantic-conventions/tests/test_semconv.py b/opentelemetry-semantic-conventions/tests/test_semconv.py index f8e827145ab..a7362a8af77 100644 --- a/opentelemetry-semantic-conventions/tests/test_semconv.py +++ b/opentelemetry-semantic-conventions/tests/test_semconv.py @@ -13,15 +13,12 @@ # limitations under the License. # type: ignore +from importlib.util import find_spec from unittest import TestCase -from pkg_resources import DistributionNotFound, require - class TestSemanticConventions(TestCase): def test_semantic_conventions(self): - try: - require(["opentelemetry-semantic-conventions"]) - except DistributionNotFound: + if find_spec("opentelemetry.semconv") is None: self.fail("opentelemetry-semantic-conventions not installed") diff --git a/shim/opentelemetry-opentracing-shim/tests/testbed/test_active_span_replacement/test_asyncio.py b/shim/opentelemetry-opentracing-shim/tests/testbed/test_active_span_replacement/test_asyncio.py index 131bb70b91b..0419ab44a22 100644 --- a/shim/opentelemetry-opentracing-shim/tests/testbed/test_active_span_replacement/test_asyncio.py +++ b/shim/opentelemetry-opentracing-shim/tests/testbed/test_active_span_replacement/test_asyncio.py @@ -1,12 +1,27 @@ +# Copyright The OpenTelemetry Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + import asyncio +# pylint: disable=import-error from ..otel_ot_shim_tracer import MockTracer from ..testcase import OpenTelemetryTestCase from ..utils import stop_loop_when class TestAsyncio(OpenTelemetryTestCase): - def setUp(self): + def setUp(self): # pylint: disable=invalid-name self.tracer = MockTracer() self.loop = asyncio.get_event_loop() diff --git a/shim/opentelemetry-opentracing-shim/tests/testbed/test_active_span_replacement/test_threads.py b/shim/opentelemetry-opentracing-shim/tests/testbed/test_active_span_replacement/test_threads.py index c8d490063ba..4e76c87a03e 100644 --- a/shim/opentelemetry-opentracing-shim/tests/testbed/test_active_span_replacement/test_threads.py +++ b/shim/opentelemetry-opentracing-shim/tests/testbed/test_active_span_replacement/test_threads.py @@ -1,11 +1,26 @@ +# Copyright The OpenTelemetry Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + from concurrent.futures import ThreadPoolExecutor +# pylint: disable=import-error from ..otel_ot_shim_tracer import MockTracer from ..testcase import OpenTelemetryTestCase class TestThreads(OpenTelemetryTestCase): - def setUp(self): + def setUp(self): # pylint: disable=invalid-name self.tracer = MockTracer() # use max_workers=3 as a general example even if only one would suffice self.executor = ThreadPoolExecutor(max_workers=3) diff --git a/shim/opentelemetry-opentracing-shim/tests/testbed/test_client_server/test_asyncio.py b/shim/opentelemetry-opentracing-shim/tests/testbed/test_client_server/test_asyncio.py index d76fffe3b38..adf99e76b23 100644 --- a/shim/opentelemetry-opentracing-shim/tests/testbed/test_client_server/test_asyncio.py +++ b/shim/opentelemetry-opentracing-shim/tests/testbed/test_client_server/test_asyncio.py @@ -1,8 +1,23 @@ +# Copyright The OpenTelemetry Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + import asyncio import opentracing from opentracing.ext import tags +# pylint: disable=import-error from ..otel_ot_shim_tracer import MockTracer from ..testcase import OpenTelemetryTestCase from ..utils import get_logger, get_one_by_tag, stop_loop_when @@ -50,7 +65,7 @@ async def send(self): class TestAsyncio(OpenTelemetryTestCase): - def setUp(self): + def setUp(self): # pylint: disable=invalid-name self.tracer = MockTracer() self.queue = asyncio.Queue() self.loop = asyncio.get_event_loop() diff --git a/shim/opentelemetry-opentracing-shim/tests/testbed/test_client_server/test_threads.py b/shim/opentelemetry-opentracing-shim/tests/testbed/test_client_server/test_threads.py index df382c34636..6fa5974d791 100644 --- a/shim/opentelemetry-opentracing-shim/tests/testbed/test_client_server/test_threads.py +++ b/shim/opentelemetry-opentracing-shim/tests/testbed/test_client_server/test_threads.py @@ -1,9 +1,24 @@ +# Copyright The OpenTelemetry Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + from queue import Queue from threading import Thread import opentracing from opentracing.ext import tags +# pylint: disable=import-error from ..otel_ot_shim_tracer import MockTracer from ..testcase import OpenTelemetryTestCase from ..utils import await_until, get_logger, get_one_by_tag @@ -52,7 +67,7 @@ def send(self): class TestThreads(OpenTelemetryTestCase): - def setUp(self): + def setUp(self): # pylint: disable=invalid-name self.tracer = MockTracer() self.queue = Queue() self.server = Server(tracer=self.tracer, queue=self.queue) diff --git a/shim/opentelemetry-opentracing-shim/tests/testbed/test_common_request_handler/request_handler.py b/shim/opentelemetry-opentracing-shim/tests/testbed/test_common_request_handler/request_handler.py index 22d59fbca64..b48a5dbc68b 100644 --- a/shim/opentelemetry-opentracing-shim/tests/testbed/test_common_request_handler/request_handler.py +++ b/shim/opentelemetry-opentracing-shim/tests/testbed/test_common_request_handler/request_handler.py @@ -1,5 +1,20 @@ +# Copyright The OpenTelemetry Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + from opentracing.ext import tags +# pylint: disable=import-error from ..utils import get_logger logger = get_logger(__name__) diff --git a/shim/opentelemetry-opentracing-shim/tests/testbed/test_common_request_handler/test_asyncio.py b/shim/opentelemetry-opentracing-shim/tests/testbed/test_common_request_handler/test_asyncio.py index 14958418a32..58970a223c3 100644 --- a/shim/opentelemetry-opentracing-shim/tests/testbed/test_common_request_handler/test_asyncio.py +++ b/shim/opentelemetry-opentracing-shim/tests/testbed/test_common_request_handler/test_asyncio.py @@ -1,7 +1,22 @@ +# Copyright The OpenTelemetry Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + import asyncio from opentracing.ext import tags +# pylint: disable=import-error from ..otel_ot_shim_tracer import MockTracer from ..testcase import OpenTelemetryTestCase from ..utils import get_logger, get_one_by_operation_name, stop_loop_when @@ -45,7 +60,7 @@ class TestAsyncio(OpenTelemetryTestCase): So one issue here is setting correct parent span. """ - def setUp(self): + def setUp(self): # pylint: disable=invalid-name self.tracer = MockTracer() self.loop = asyncio.get_event_loop() self.client = Client(RequestHandler(self.tracer), self.loop) diff --git a/shim/opentelemetry-opentracing-shim/tests/testbed/test_common_request_handler/test_threads.py b/shim/opentelemetry-opentracing-shim/tests/testbed/test_common_request_handler/test_threads.py index 6f5022ccc15..fdc0549d62f 100644 --- a/shim/opentelemetry-opentracing-shim/tests/testbed/test_common_request_handler/test_threads.py +++ b/shim/opentelemetry-opentracing-shim/tests/testbed/test_common_request_handler/test_threads.py @@ -1,7 +1,22 @@ +# Copyright The OpenTelemetry Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + from concurrent.futures import ThreadPoolExecutor from opentracing.ext import tags +# pylint: disable=import-error from ..otel_ot_shim_tracer import MockTracer from ..testcase import OpenTelemetryTestCase from ..utils import get_logger, get_one_by_operation_name @@ -45,7 +60,7 @@ class TestThreads(OpenTelemetryTestCase): activate span. So one issue here is setting correct parent span. """ - def setUp(self): + def setUp(self): # pylint: disable=invalid-name self.tracer = MockTracer() self.executor = ThreadPoolExecutor(max_workers=3) self.client = Client(RequestHandler(self.tracer), self.executor) diff --git a/shim/opentelemetry-opentracing-shim/tests/testbed/test_late_span_finish/test_asyncio.py b/shim/opentelemetry-opentracing-shim/tests/testbed/test_late_span_finish/test_asyncio.py index 86a47c6a737..d27e51ca88f 100644 --- a/shim/opentelemetry-opentracing-shim/tests/testbed/test_late_span_finish/test_asyncio.py +++ b/shim/opentelemetry-opentracing-shim/tests/testbed/test_late_span_finish/test_asyncio.py @@ -1,5 +1,20 @@ +# Copyright The OpenTelemetry Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + import asyncio +# pylint: disable=import-error from ..otel_ot_shim_tracer import MockTracer from ..testcase import OpenTelemetryTestCase from ..utils import get_logger, stop_loop_when @@ -8,7 +23,7 @@ class TestAsyncio(OpenTelemetryTestCase): - def setUp(self): + def setUp(self): # pylint: disable=invalid-name self.tracer = MockTracer() self.loop = asyncio.get_event_loop() diff --git a/shim/opentelemetry-opentracing-shim/tests/testbed/test_late_span_finish/test_threads.py b/shim/opentelemetry-opentracing-shim/tests/testbed/test_late_span_finish/test_threads.py index de8acb70bf3..2cd43d7e70b 100644 --- a/shim/opentelemetry-opentracing-shim/tests/testbed/test_late_span_finish/test_threads.py +++ b/shim/opentelemetry-opentracing-shim/tests/testbed/test_late_span_finish/test_threads.py @@ -1,12 +1,27 @@ +# Copyright The OpenTelemetry Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + import time from concurrent.futures import ThreadPoolExecutor +# pylint: disable=import-error from ..otel_ot_shim_tracer import MockTracer from ..testcase import OpenTelemetryTestCase class TestThreads(OpenTelemetryTestCase): - def setUp(self): + def setUp(self): # pylint: disable=invalid-name self.tracer = MockTracer() self.executor = ThreadPoolExecutor(max_workers=3) diff --git a/shim/opentelemetry-opentracing-shim/tests/testbed/test_listener_per_request/test_asyncio.py b/shim/opentelemetry-opentracing-shim/tests/testbed/test_listener_per_request/test_asyncio.py index 66999f04bf2..d0f0a6a577e 100644 --- a/shim/opentelemetry-opentracing-shim/tests/testbed/test_listener_per_request/test_asyncio.py +++ b/shim/opentelemetry-opentracing-shim/tests/testbed/test_listener_per_request/test_asyncio.py @@ -1,7 +1,22 @@ +# Copyright The OpenTelemetry Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + import asyncio from opentracing.ext import tags +# pylint: disable=import-error from ..otel_ot_shim_tracer import MockTracer from ..testcase import OpenTelemetryTestCase from ..utils import get_one_by_tag @@ -28,7 +43,7 @@ def send_sync(self, message): class TestAsyncio(OpenTelemetryTestCase): - def setUp(self): + def setUp(self): # pylint: disable=invalid-name self.tracer = MockTracer() self.loop = asyncio.get_event_loop() diff --git a/shim/opentelemetry-opentracing-shim/tests/testbed/test_listener_per_request/test_threads.py b/shim/opentelemetry-opentracing-shim/tests/testbed/test_listener_per_request/test_threads.py index b810db1f01c..39d0a3d1d4c 100644 --- a/shim/opentelemetry-opentracing-shim/tests/testbed/test_listener_per_request/test_threads.py +++ b/shim/opentelemetry-opentracing-shim/tests/testbed/test_listener_per_request/test_threads.py @@ -1,7 +1,22 @@ +# Copyright The OpenTelemetry Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + from concurrent.futures import ThreadPoolExecutor from opentracing.ext import tags +# pylint: disable=import-error from ..otel_ot_shim_tracer import MockTracer from ..testcase import OpenTelemetryTestCase from ..utils import get_one_by_tag @@ -28,7 +43,7 @@ def send_sync(self, message): class TestThreads(OpenTelemetryTestCase): - def setUp(self): + def setUp(self): # pylint: disable=invalid-name self.tracer = MockTracer() def test_main(self): diff --git a/shim/opentelemetry-opentracing-shim/tests/testbed/test_multiple_callbacks/test_asyncio.py b/shim/opentelemetry-opentracing-shim/tests/testbed/test_multiple_callbacks/test_asyncio.py index 3754367878c..bbfb620a840 100644 --- a/shim/opentelemetry-opentracing-shim/tests/testbed/test_multiple_callbacks/test_asyncio.py +++ b/shim/opentelemetry-opentracing-shim/tests/testbed/test_multiple_callbacks/test_asyncio.py @@ -1,6 +1,21 @@ +# Copyright The OpenTelemetry Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + import asyncio import random +# pylint: disable=import-error from ..otel_ot_shim_tracer import MockTracer from ..testcase import OpenTelemetryTestCase from ..utils import get_logger, stop_loop_when @@ -10,7 +25,7 @@ class TestAsyncio(OpenTelemetryTestCase): - def setUp(self): + def setUp(self): # pylint: disable=invalid-name self.tracer = MockTracer() self.loop = asyncio.get_event_loop() diff --git a/shim/opentelemetry-opentracing-shim/tests/testbed/test_multiple_callbacks/test_threads.py b/shim/opentelemetry-opentracing-shim/tests/testbed/test_multiple_callbacks/test_threads.py index dfb567c7454..6e8b405ccee 100644 --- a/shim/opentelemetry-opentracing-shim/tests/testbed/test_multiple_callbacks/test_threads.py +++ b/shim/opentelemetry-opentracing-shim/tests/testbed/test_multiple_callbacks/test_threads.py @@ -1,7 +1,22 @@ +# Copyright The OpenTelemetry Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + import random import time from concurrent.futures import ThreadPoolExecutor +# pylint: disable=import-error from ..otel_ot_shim_tracer import MockTracer from ..testcase import OpenTelemetryTestCase from ..utils import RefCount, get_logger @@ -11,7 +26,7 @@ class TestThreads(OpenTelemetryTestCase): - def setUp(self): + def setUp(self): # pylint: disable=invalid-name self.tracer = MockTracer() self.executor = ThreadPoolExecutor(max_workers=3) diff --git a/shim/opentelemetry-opentracing-shim/tests/testbed/test_nested_callbacks/test_asyncio.py b/shim/opentelemetry-opentracing-shim/tests/testbed/test_nested_callbacks/test_asyncio.py index b6b8277d38f..f00258624ca 100644 --- a/shim/opentelemetry-opentracing-shim/tests/testbed/test_nested_callbacks/test_asyncio.py +++ b/shim/opentelemetry-opentracing-shim/tests/testbed/test_nested_callbacks/test_asyncio.py @@ -1,12 +1,27 @@ +# Copyright The OpenTelemetry Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + import asyncio +# pylint: disable=import-error from ..otel_ot_shim_tracer import MockTracer from ..testcase import OpenTelemetryTestCase from ..utils import stop_loop_when class TestAsyncio(OpenTelemetryTestCase): - def setUp(self): + def setUp(self): # pylint: disable=invalid-name self.tracer = MockTracer() self.loop = asyncio.get_event_loop() diff --git a/shim/opentelemetry-opentracing-shim/tests/testbed/test_nested_callbacks/test_threads.py b/shim/opentelemetry-opentracing-shim/tests/testbed/test_nested_callbacks/test_threads.py index 1f86b2dfba3..955298537da 100644 --- a/shim/opentelemetry-opentracing-shim/tests/testbed/test_nested_callbacks/test_threads.py +++ b/shim/opentelemetry-opentracing-shim/tests/testbed/test_nested_callbacks/test_threads.py @@ -1,16 +1,31 @@ +# Copyright The OpenTelemetry Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + from concurrent.futures import ThreadPoolExecutor +# pylint: disable=import-error from ..otel_ot_shim_tracer import MockTracer from ..testcase import OpenTelemetryTestCase from ..utils import await_until class TestThreads(OpenTelemetryTestCase): - def setUp(self): + def setUp(self): # pylint: disable=invalid-name self.tracer = MockTracer() self.executor = ThreadPoolExecutor(max_workers=3) - def tearDown(self): + def tearDown(self): # pylint: disable=invalid-name self.executor.shutdown(False) def test_main(self): diff --git a/shim/opentelemetry-opentracing-shim/tests/testbed/test_subtask_span_propagation/test_asyncio.py b/shim/opentelemetry-opentracing-shim/tests/testbed/test_subtask_span_propagation/test_asyncio.py index bd08ee6d098..653f9bd810e 100644 --- a/shim/opentelemetry-opentracing-shim/tests/testbed/test_subtask_span_propagation/test_asyncio.py +++ b/shim/opentelemetry-opentracing-shim/tests/testbed/test_subtask_span_propagation/test_asyncio.py @@ -1,11 +1,26 @@ +# Copyright The OpenTelemetry Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + import asyncio +# pylint: disable=import-error from ..otel_ot_shim_tracer import MockTracer from ..testcase import OpenTelemetryTestCase class TestAsyncio(OpenTelemetryTestCase): - def setUp(self): + def setUp(self): # pylint: disable=invalid-name self.tracer = MockTracer() self.loop = asyncio.get_event_loop() diff --git a/shim/opentelemetry-opentracing-shim/tests/testbed/test_subtask_span_propagation/test_threads.py b/shim/opentelemetry-opentracing-shim/tests/testbed/test_subtask_span_propagation/test_threads.py index ea15aafb9a6..0d003c9062a 100644 --- a/shim/opentelemetry-opentracing-shim/tests/testbed/test_subtask_span_propagation/test_threads.py +++ b/shim/opentelemetry-opentracing-shim/tests/testbed/test_subtask_span_propagation/test_threads.py @@ -1,11 +1,26 @@ +# Copyright The OpenTelemetry Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + from concurrent.futures import ThreadPoolExecutor +# pylint: disable=import-error from ..otel_ot_shim_tracer import MockTracer from ..testcase import OpenTelemetryTestCase class TestThreads(OpenTelemetryTestCase): - def setUp(self): + def setUp(self): # pylint: disable=invalid-name self.tracer = MockTracer() self.executor = ThreadPoolExecutor(max_workers=3) diff --git a/tox.ini b/tox.ini index 147931eb6b8..fd357987a61 100644 --- a/tox.ini +++ b/tox.ini @@ -198,7 +198,7 @@ commands = codespell [testenv:lint] -basepython: python3.9 +basepython: python3.10 recreate = True deps = -c dev-requirements.txt From 9c4b65f21771ac9e34d39c58ed87d9e0cb5ed3e5 Mon Sep 17 00:00:00 2001 From: Diego Hurtado Date: Mon, 28 Nov 2022 15:54:21 +0100 Subject: [PATCH 02/29] WIP --- .../src/opentelemetry/context/__init__.py | 15 +++++++- .../src/opentelemetry/propagate/__init__.py | 13 ++++++- .../tests/propagators/test_propagators.py | 37 ++++++++++++++++++- 3 files changed, 61 insertions(+), 4 deletions(-) diff --git a/opentelemetry-api/src/opentelemetry/context/__init__.py b/opentelemetry-api/src/opentelemetry/context/__init__.py index 8c82d42d234..1a04de1a613 100644 --- a/opentelemetry-api/src/opentelemetry/context/__init__.py +++ b/opentelemetry-api/src/opentelemetry/context/__init__.py @@ -63,7 +63,18 @@ def wrapper( # type: ignore[misc] OTEL_PYTHON_CONTEXT, default_context ) # type: str try: - if version_info.minor <= 9: + # FIXME Remove when support for 3.7 is dropped. + if version_info.minor == 7: + for entry_point in entry_points(): + if ( + entry_point.group == "opentelemetry_context" + and entry_point.name == configured_context + ): + _RUNTIME_CONTEXT = entry_point.load()() # type: ignore + break + + # FIXME Remove when support for 3.9 is dropped. + elif version_info.minor <= 9: for entry_point in entry_points()[ # type: ignore "opentelemetry_context" ]: @@ -81,7 +92,7 @@ def wrapper( # type: ignore[misc] name=configured_context, )[0].load()() except Exception: # pylint: disable=broad-except - logger.error( + logger.exception( "Failed to load context: %s", configured_context ) return func(*args, **kwargs) # type: ignore[misc] diff --git a/opentelemetry-api/src/opentelemetry/propagate/__init__.py b/opentelemetry-api/src/opentelemetry/propagate/__init__.py index ef10844ebd0..e5d3cf91308 100644 --- a/opentelemetry-api/src/opentelemetry/propagate/__init__.py +++ b/opentelemetry-api/src/opentelemetry/propagate/__init__.py @@ -140,7 +140,18 @@ def inject( propagator = propagator.strip() try: - if version_info.minor <= 9: + # FIXME: Remove when support for 3.7 is dropped. + if version_info.minor == 7: + + for entry_point in entry_points(): + if ( + entry_point.group == "opentelemetry_propagator" + and entry_point.name == propagator + ): + propagators.append(entry_point.load()()) # type: ignore + + # FIXME: Remove when support for 3.9 is dropped. + elif version_info.minor <= 9: for entry_point in entry_points().get( # type: ignore "opentelemetry_propagator", [] diff --git a/opentelemetry-api/tests/propagators/test_propagators.py b/opentelemetry-api/tests/propagators/test_propagators.py index 8e06ce2535c..d1ba9471878 100644 --- a/opentelemetry-api/tests/propagators/test_propagators.py +++ b/opentelemetry-api/tests/propagators/test_propagators.py @@ -64,7 +64,42 @@ def test_non_default_propagators( self, mock_entry_points, mock_compositehttppropagator ): - if version_info.minor <= 9: + # FIXME Remove when support for 3.7 is dropped. + if version_info.minor == 7: + + mock_a = Mock() + mock_a.configure_mock( + **{ + "name": "a", + "group": "opentelemetry_propagator", + "load.return_value": Mock(**{"return_value": "a"}), + } + ) + mock_b = Mock() + mock_b.configure_mock( + **{ + "name": "b", + "group": "opentelemetry_propagator", + "load.return_value": Mock(**{"return_value": "b"}), + } + ) + mock_c = Mock() + mock_c.configure_mock( + **{ + "name": "c", + "group": "opentelemetry_propagator", + "load.return_value": Mock(**{"return_value": "c"}), + } + ) + + mock_entry_points.configure_mock( + **{ + "return_value": (mock_a, mock_b, mock_c) + } + ) + + # FIXME Remove when support for 3.9 is dropped. + elif version_info.minor <= 9: mock_a = Mock() mock_a.configure_mock( From bd3baaac38772883ddd3d93e9cee15fb7a300072 Mon Sep 17 00:00:00 2001 From: Diego Hurtado Date: Mon, 28 Nov 2022 16:07:10 +0100 Subject: [PATCH 03/29] WIP --- opentelemetry-api/src/opentelemetry/util/_providers.py | 1 + opentelemetry-api/tests/propagators/test_propagators.py | 1 + .../src/opentelemetry/sdk/_configuration/__init__.py | 1 + .../src/opentelemetry/sdk/error_handler/__init__.py | 1 + .../tests/error_handler/test_error_handler.py | 3 +++ opentelemetry-sdk/tests/test_configurator.py | 8 ++++++++ 6 files changed, 15 insertions(+) diff --git a/opentelemetry-api/src/opentelemetry/util/_providers.py b/opentelemetry-api/src/opentelemetry/util/_providers.py index 9f700a88df4..901fcdce36d 100644 --- a/opentelemetry-api/src/opentelemetry/util/_providers.py +++ b/opentelemetry-api/src/opentelemetry/util/_providers.py @@ -39,6 +39,7 @@ def _load_provider( try: + # FIXME remove when support for 3.9 is dropped. if version_info.minor <= 9: provider_name = cast( diff --git a/opentelemetry-api/tests/propagators/test_propagators.py b/opentelemetry-api/tests/propagators/test_propagators.py index d1ba9471878..1c840fab0ff 100644 --- a/opentelemetry-api/tests/propagators/test_propagators.py +++ b/opentelemetry-api/tests/propagators/test_propagators.py @@ -52,6 +52,7 @@ def test_propagators(propagators): reload(opentelemetry.propagate) + # FIXME Remove when support for 3.7 is dropped. if version_info.minor == 7: entry_points_path = "importlib_metadata.entry_points" else: diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/_configuration/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/_configuration/__init__.py index da137420836..89b8c5bdc7c 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/_configuration/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/_configuration/__init__.py @@ -100,6 +100,7 @@ def _import_config_components( component_implementations = [] + # FIXME remove when support for 3.9 is dropped. if version_info.minor <= 9: for entry_point in entry_points()[entry_point_name]: diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/error_handler/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/error_handler/__init__.py index 66dfae8f239..3503cdb0194 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/error_handler/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/error_handler/__init__.py @@ -124,6 +124,7 @@ def __exit__(self, exc_type, exc_value, traceback): plugin_handled = False + # FIXME remove when support for 3.9 is dropped. if version_info.minor <= 9: error_handler_entry_points = entry_points()[ diff --git a/opentelemetry-sdk/tests/error_handler/test_error_handler.py b/opentelemetry-sdk/tests/error_handler/test_error_handler.py index bdab5095e17..148cbe28236 100644 --- a/opentelemetry-sdk/tests/error_handler/test_error_handler.py +++ b/opentelemetry-sdk/tests/error_handler/test_error_handler.py @@ -55,6 +55,7 @@ class AssertionErrorHandler(ErrorHandler, AssertionError): **{"load.return_value": AssertionErrorHandler} ) + # FIXME remove when support for 3.9 is dropped. if version_info.minor <= 9: mock_entry_points.configure_mock( @@ -107,6 +108,7 @@ def _handle(self, error: Exception): **{"load.return_value": ErrorErrorHandler} ) + # FIXME remove when support for 3.9 is dropped. if version_info.minor <= 9: mock_entry_points.configure_mock( @@ -146,6 +148,7 @@ def __new__(cls): **{"load.return_value": MockErrorHandlerClass} ) + # FIXME remove when support for 3.9 is dropped. if version_info.minor <= 9: mock_entry_points.configure_mock( diff --git a/opentelemetry-sdk/tests/test_configurator.py b/opentelemetry-sdk/tests/test_configurator.py index 183e5dee064..1de1077a207 100644 --- a/opentelemetry-sdk/tests/test_configurator.py +++ b/opentelemetry-sdk/tests/test_configurator.py @@ -352,6 +352,7 @@ def test_trace_init_otlp(self): @patch("opentelemetry.sdk._configuration.IdGenerator", new=IdGenerator) @patch("opentelemetry.sdk._configuration.entry_points") def test_trace_init_custom_id_generator(self, mock_entry_points): + # FIXME remove when support for 3.9 is dropped. if version_info.minor <= 9: mock_entry_points.configure_mock( @@ -392,6 +393,7 @@ def test_trace_init_custom_sampler_with_env_non_existent_entry_point(self): @patch.dict("os.environ", {OTEL_TRACES_SAMPLER: "custom_sampler_factory"}) def test_trace_init_custom_sampler_with_env(self, mock_entry_points): + # FIXME remove when support for 3.9 is dropped. if version_info.minor <= 9: mock_entry_points.configure_mock( @@ -428,6 +430,7 @@ def test_trace_init_custom_sampler_with_env_bad_factory( self, mock_entry_points ): + # FIXME remove when support for 3.9 is dropped. if version_info.minor <= 9: mock_entry_points.configure_mock( @@ -470,6 +473,7 @@ def test_trace_init_custom_sampler_with_env_unused_arg( self, mock_entry_points ): + # FIXME remove when support for 3.9 is dropped. if version_info.minor <= 9: mock_entry_points.configure_mock( @@ -510,6 +514,7 @@ def test_trace_init_custom_sampler_with_env_unused_arg( ) def test_trace_init_custom_ratio_sampler_with_env(self, mock_entry_points): + # FIXME remove when support for 3.9 is dropped. if version_info.minor <= 9: mock_entry_points.configure_mock( @@ -553,6 +558,7 @@ def test_trace_init_custom_ratio_sampler_with_env_bad_arg( self, mock_entry_points ): + # FIXME remove when support for 3.9 is dropped. if version_info.minor <= 9: mock_entry_points.configure_mock( @@ -594,6 +600,7 @@ def test_trace_init_custom_ratio_sampler_with_env_missing_arg( self, mock_entry_points ): + # FIXME remove when support for 3.9 is dropped. if version_info.minor <= 9: mock_entry_points.configure_mock( @@ -636,6 +643,7 @@ def test_trace_init_custom_ratio_sampler_with_env_multiple_entry_points( self, mock_entry_points ): + # FIXME remove when support for 3.9 is dropped. if version_info.minor <= 9: mock_entry_points.configure_mock( From fff44303a48bb85ab0b3cec859c1ed78b156b5ce Mon Sep 17 00:00:00 2001 From: Diego Hurtado Date: Mon, 28 Nov 2022 18:05:07 +0100 Subject: [PATCH 04/29] Add test case for _load_providers --- .../src/opentelemetry/util/_providers.py | 14 +++ .../tests/util/test__providers.py | 104 ++++++++++++++++++ 2 files changed, 118 insertions(+) create mode 100644 opentelemetry-api/tests/util/test__providers.py diff --git a/opentelemetry-api/src/opentelemetry/util/_providers.py b/opentelemetry-api/src/opentelemetry/util/_providers.py index 901fcdce36d..a6d6ddf4d8b 100644 --- a/opentelemetry-api/src/opentelemetry/util/_providers.py +++ b/opentelemetry-api/src/opentelemetry/util/_providers.py @@ -49,6 +49,20 @@ def _load_provider( ), ) + # FIXME remove when support for 3.7 is dropped. + if version_info.minor == 7: + + for entry_point in entry_points(): # type: ignore + if ( + entry_point.name == provider_name + and entry_point.group == f"opentelemetry_{provider}" + ): # type: ignore + return cast(Provider, entry_point.load()()) # type: ignore + raise Exception(f"Provider {provider_name} not found") + + # FIXME remove when support for 3.9 is dropped. + elif version_info.minor <= 9: + for entry_point in entry_points()[f"opentelemetry_{provider}"]: # type: ignore if entry_point.name == provider_name: # type: ignore return cast(Provider, entry_point.load()()) # type: ignore diff --git a/opentelemetry-api/tests/util/test__providers.py b/opentelemetry-api/tests/util/test__providers.py new file mode 100644 index 00000000000..092ca84b3f8 --- /dev/null +++ b/opentelemetry-api/tests/util/test__providers.py @@ -0,0 +1,104 @@ +# Copyright The OpenTelemetry Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from unittest import TestCase +from unittest.mock import patch, Mock +from sys import version_info +from os import environ +from importlib import reload + +from opentelemetry.util import _providers + + +class Test_Providers(TestCase): + + # FIXME Remove when support for 3.7 is dropped. + if version_info.minor == 7: + entry_points_path = "importlib_metadata.entry_points" + else: + entry_points_path = "importlib.metadata.entry_points" + + @patch.dict( + environ, + {"provider_environment_variable": "mock_provider_environment_variable"} + ) + @patch(entry_points_path) + # @patch("opentelemetry.util._providers.entry_points") + def test__providers(self, mock_entry_points): + + reload(_providers) + + # FIXME Remove when support for 3.7 is dropped. + if version_info.minor == 7: + + mock_a = Mock() + mock_a.configure_mock( + **{ + "name": "mock_provider_environment_variable", + "group": "opentelemetry_provider", + "load.return_value": Mock(**{"return_value": "a"}), + } + ) + + mock_entry_points.configure_mock( + **{ + "return_value": (mock_a,) + } + ) + + # FIXME Remove when support for 3.9 is dropped. + elif version_info.minor <= 9: + + mock_a = Mock() + mock_a.configure_mock( + **{ + "name": "mock_provider_environment_variable", + "group": "opentelemetry_provider", + "load.return_value": Mock(**{"return_value": "a"}), + } + ) + + mock_entry_points.configure_mock( + **{ + "return_value": { + "opentelemetry_propagator": [mock_a] + } + } + ) + + else: + + mock_entry_points.configure_mock( + **{ + "side_effect": [ + [ + Mock( + **{ + "load.return_value": Mock( + **{"return_value": "a"} + ) + } + ), + ], + ] + } + ) + + self.assertEqual( + _providers._load_provider( + "provider_environment_variable", + "provider" + ), + "a" + ) From 2eb9b58b49f2570c9905570a46a4f728020dba24 Mon Sep 17 00:00:00 2001 From: Diego Hurtado Date: Mon, 28 Nov 2022 18:08:07 +0100 Subject: [PATCH 05/29] Fix test case for 3.9 --- opentelemetry-api/tests/util/test__providers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/opentelemetry-api/tests/util/test__providers.py b/opentelemetry-api/tests/util/test__providers.py index 092ca84b3f8..f9cefdbf23e 100644 --- a/opentelemetry-api/tests/util/test__providers.py +++ b/opentelemetry-api/tests/util/test__providers.py @@ -72,7 +72,7 @@ def test__providers(self, mock_entry_points): mock_entry_points.configure_mock( **{ "return_value": { - "opentelemetry_propagator": [mock_a] + "opentelemetry_provider": [mock_a] } } ) From 79d056c806cebf1ad2ed9a51d118b65bdd3c5a5f Mon Sep 17 00:00:00 2001 From: Diego Hurtado Date: Mon, 28 Nov 2022 18:10:08 +0100 Subject: [PATCH 06/29] Fix lint --- .../tests/propagators/test_propagators.py | 4 +-- .../tests/util/test__providers.py | 29 +++++++------------ 2 files changed, 12 insertions(+), 21 deletions(-) diff --git a/opentelemetry-api/tests/propagators/test_propagators.py b/opentelemetry-api/tests/propagators/test_propagators.py index 1c840fab0ff..9cc51e52bfd 100644 --- a/opentelemetry-api/tests/propagators/test_propagators.py +++ b/opentelemetry-api/tests/propagators/test_propagators.py @@ -94,9 +94,7 @@ def test_non_default_propagators( ) mock_entry_points.configure_mock( - **{ - "return_value": (mock_a, mock_b, mock_c) - } + **{"return_value": (mock_a, mock_b, mock_c)} ) # FIXME Remove when support for 3.9 is dropped. diff --git a/opentelemetry-api/tests/util/test__providers.py b/opentelemetry-api/tests/util/test__providers.py index f9cefdbf23e..048fc8f6c4c 100644 --- a/opentelemetry-api/tests/util/test__providers.py +++ b/opentelemetry-api/tests/util/test__providers.py @@ -12,11 +12,11 @@ # See the License for the specific language governing permissions and # limitations under the License. -from unittest import TestCase -from unittest.mock import patch, Mock -from sys import version_info -from os import environ from importlib import reload +from os import environ +from sys import version_info +from unittest import TestCase +from unittest.mock import Mock, patch from opentelemetry.util import _providers @@ -31,7 +31,9 @@ class Test_Providers(TestCase): @patch.dict( environ, - {"provider_environment_variable": "mock_provider_environment_variable"} + { + "provider_environment_variable": "mock_provider_environment_variable" + }, ) @patch(entry_points_path) # @patch("opentelemetry.util._providers.entry_points") @@ -51,11 +53,7 @@ def test__providers(self, mock_entry_points): } ) - mock_entry_points.configure_mock( - **{ - "return_value": (mock_a,) - } - ) + mock_entry_points.configure_mock(**{"return_value": (mock_a,)}) # FIXME Remove when support for 3.9 is dropped. elif version_info.minor <= 9: @@ -70,11 +68,7 @@ def test__providers(self, mock_entry_points): ) mock_entry_points.configure_mock( - **{ - "return_value": { - "opentelemetry_provider": [mock_a] - } - } + **{"return_value": {"opentelemetry_provider": [mock_a]}} ) else: @@ -97,8 +91,7 @@ def test__providers(self, mock_entry_points): self.assertEqual( _providers._load_provider( - "provider_environment_variable", - "provider" + "provider_environment_variable", "provider" ), - "a" + "a", ) From a3be85ab1ba0f6206fed5added7e84e6e1b84dfb Mon Sep 17 00:00:00 2001 From: Diego Hurtado Date: Mon, 28 Nov 2022 20:35:26 +0100 Subject: [PATCH 07/29] Fix 3.7 sdk tests --- .../sdk/_configuration/__init__.py | 15 +- opentelemetry-sdk/tests/test_configurator.py | 157 +++++++++++++++++- 2 files changed, 162 insertions(+), 10 deletions(-) diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/_configuration/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/_configuration/__init__.py index 89b8c5bdc7c..6fe698b21c9 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/_configuration/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/_configuration/__init__.py @@ -100,8 +100,21 @@ def _import_config_components( component_implementations = [] + # FIXME remove when support for 3.7 is dropped. + if version_info.minor == 7: + + for entry_point in entry_points(): + for selected_component in selected_components: + if ( + entry_point.name == selected_component + and entry_point.group == entry_point_name + ): + component_implementations.append( + (selected_component, entry_point.load()) + ) + # FIXME remove when support for 3.9 is dropped. - if version_info.minor <= 9: + elif version_info.minor <= 9: for entry_point in entry_points()[entry_point_name]: for selected_component in selected_components: diff --git a/opentelemetry-sdk/tests/test_configurator.py b/opentelemetry-sdk/tests/test_configurator.py index 1de1077a207..0d9f72790c4 100644 --- a/opentelemetry-sdk/tests/test_configurator.py +++ b/opentelemetry-sdk/tests/test_configurator.py @@ -19,7 +19,7 @@ from sys import version_info from typing import Dict, Iterable, Optional, Sequence from unittest import TestCase -from unittest.mock import patch +from unittest.mock import patch, Mock from opentelemetry import trace from opentelemetry.context import Context @@ -352,8 +352,20 @@ def test_trace_init_otlp(self): @patch("opentelemetry.sdk._configuration.IdGenerator", new=IdGenerator) @patch("opentelemetry.sdk._configuration.entry_points") def test_trace_init_custom_id_generator(self, mock_entry_points): + # FIXME remove when support for 3.7 is dropped. + if version_info.minor == 7: + + mock = Mock() + mock.configure_mock( + **{ + "name": "custom_id_generator", + "group": "opentelemetry_id_generator", + "load.return_value": CustomIdGenerator + } + ) + mock_entry_points.configure_mock(return_value=(mock,)) # FIXME remove when support for 3.9 is dropped. - if version_info.minor <= 9: + elif version_info.minor <= 9: mock_entry_points.configure_mock( return_value={ @@ -393,8 +405,21 @@ def test_trace_init_custom_sampler_with_env_non_existent_entry_point(self): @patch.dict("os.environ", {OTEL_TRACES_SAMPLER: "custom_sampler_factory"}) def test_trace_init_custom_sampler_with_env(self, mock_entry_points): + # FIXME remove when support for 3.7 is dropped. + if version_info.minor == 7: + + mock = Mock() + mock.configure_mock( + **{ + "name": "custom_sampler_factory", + "group": "opentelemetry_traces_sampler", + "load.return_value": CustomSamplerFactory.get_custom_sampler + } + ) + mock_entry_points.configure_mock(return_value=(mock,)) + # FIXME remove when support for 3.9 is dropped. - if version_info.minor <= 9: + elif version_info.minor <= 9: mock_entry_points.configure_mock( return_value={ @@ -429,9 +454,23 @@ def test_trace_init_custom_sampler_with_env(self, mock_entry_points): def test_trace_init_custom_sampler_with_env_bad_factory( self, mock_entry_points ): + # FIXME remove when support for 3.7 is dropped. + if version_info.minor == 7: + + mock = Mock() + mock.configure_mock( + **{ + "name": "custom_sampler_factory", + "group": "opentelemetry_traces_sampler", + "load.return_value": ( + CustomSamplerFactory.empty_get_custom_sampler + ) + } + ) + mock_entry_points.configure_mock(return_value=(mock,)) # FIXME remove when support for 3.9 is dropped. - if version_info.minor <= 9: + elif version_info.minor <= 9: mock_entry_points.configure_mock( return_value={ @@ -473,8 +512,21 @@ def test_trace_init_custom_sampler_with_env_unused_arg( self, mock_entry_points ): + # FIXME remove when support for 3.7 is dropped. + if version_info.minor == 7: + + mock = Mock() + mock.configure_mock( + **{ + "name": "custom_sampler_factory", + "group": "opentelemetry_traces_sampler", + "load.return_value": CustomSamplerFactory.get_custom_sampler + } + ) + mock_entry_points.configure_mock(return_value=(mock,)) + # FIXME remove when support for 3.9 is dropped. - if version_info.minor <= 9: + elif version_info.minor <= 9: mock_entry_points.configure_mock( return_value={ @@ -514,8 +566,23 @@ def test_trace_init_custom_sampler_with_env_unused_arg( ) def test_trace_init_custom_ratio_sampler_with_env(self, mock_entry_points): + # FIXME remove when support for 3.7 is dropped. + if version_info.minor == 7: + + mock = Mock() + mock.configure_mock( + **{ + "name": "custom_ratio_sampler_factory", + "group": "opentelemetry_traces_sampler", + "load.return_value": ( + CustomSamplerFactory.get_custom_ratio_sampler + ) + } + ) + mock_entry_points.configure_mock(return_value=(mock,)) + # FIXME remove when support for 3.9 is dropped. - if version_info.minor <= 9: + elif version_info.minor <= 9: mock_entry_points.configure_mock( return_value={ @@ -558,8 +625,23 @@ def test_trace_init_custom_ratio_sampler_with_env_bad_arg( self, mock_entry_points ): + # FIXME remove when support for 3.7 is dropped. + if version_info.minor == 7: + + mock = Mock() + mock.configure_mock( + **{ + "name": "custom_ratio_sampler_factory", + "group": "opentelemetry_traces_sampler", + "load.return_value": ( + CustomSamplerFactory.get_custom_ratio_sampler + ) + } + ) + mock_entry_points.configure_mock(return_value=(mock,)) + # FIXME remove when support for 3.9 is dropped. - if version_info.minor <= 9: + elif version_info.minor <= 9: mock_entry_points.configure_mock( return_value={ @@ -600,8 +682,23 @@ def test_trace_init_custom_ratio_sampler_with_env_missing_arg( self, mock_entry_points ): + # FIXME remove when support for 3.7 is dropped. + if version_info.minor == 7: + + mock = Mock() + mock.configure_mock( + **{ + "name": "custom_ratio_sampler_factory", + "group": "opentelemetry_traces_sampler", + "load.return_value": ( + CustomSamplerFactory.get_custom_ratio_sampler + ) + } + ) + mock_entry_points.configure_mock(return_value=(mock,)) + # FIXME remove when support for 3.9 is dropped. - if version_info.minor <= 9: + elif version_info.minor <= 9: mock_entry_points.configure_mock( return_value={ @@ -643,8 +740,50 @@ def test_trace_init_custom_ratio_sampler_with_env_multiple_entry_points( self, mock_entry_points ): + # FIXME remove when support for 3.7 is dropped. + if version_info.minor == 7: + + mock_ratio_sampler_factory = Mock() + mock_ratio_sampler_factory.configure_mock( + **{ + "name": "custom_ratio_sampler_factory", + "group": "opentelemetry_traces_sampler", + "load.return_value": ( + CustomSamplerFactory.get_custom_ratio_sampler + ) + } + ) + mock_sampler_factory = Mock() + mock_sampler_factory.configure_mock( + **{ + "name": "custom_sampler_factory", + "group": "opentelemetry_traces_sampler", + "load.return_value": ( + CustomSamplerFactory.get_custom_sampler + ) + } + ) + mock_custom_z_sampler_factory = Mock() + mock_custom_z_sampler_factory.configure_mock( + **{ + "name": "custom_z_sampler_factory", + "group": "opentelemetry_traces_sampler", + "load.return_value": ( + CustomSamplerFactory.empty_get_custom_sampler + ) + } + ) + + mock_entry_points.configure_mock( + return_value=( + mock_ratio_sampler_factory, + mock_sampler_factory, + mock_custom_z_sampler_factory + ) + ) + # FIXME remove when support for 3.9 is dropped. - if version_info.minor <= 9: + elif version_info.minor <= 9: mock_entry_points.configure_mock( return_value={ From c050c1c9783e46005635916751a1494330b94eb1 Mon Sep 17 00:00:00 2001 From: Diego Hurtado Date: Mon, 28 Nov 2022 20:37:38 +0100 Subject: [PATCH 08/29] Fix lint --- opentelemetry-sdk/tests/test_configurator.py | 24 ++++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/opentelemetry-sdk/tests/test_configurator.py b/opentelemetry-sdk/tests/test_configurator.py index 0d9f72790c4..d8d8df87f0c 100644 --- a/opentelemetry-sdk/tests/test_configurator.py +++ b/opentelemetry-sdk/tests/test_configurator.py @@ -19,7 +19,7 @@ from sys import version_info from typing import Dict, Iterable, Optional, Sequence from unittest import TestCase -from unittest.mock import patch, Mock +from unittest.mock import Mock, patch from opentelemetry import trace from opentelemetry.context import Context @@ -360,7 +360,7 @@ def test_trace_init_custom_id_generator(self, mock_entry_points): **{ "name": "custom_id_generator", "group": "opentelemetry_id_generator", - "load.return_value": CustomIdGenerator + "load.return_value": CustomIdGenerator, } ) mock_entry_points.configure_mock(return_value=(mock,)) @@ -413,7 +413,7 @@ def test_trace_init_custom_sampler_with_env(self, mock_entry_points): **{ "name": "custom_sampler_factory", "group": "opentelemetry_traces_sampler", - "load.return_value": CustomSamplerFactory.get_custom_sampler + "load.return_value": CustomSamplerFactory.get_custom_sampler, } ) mock_entry_points.configure_mock(return_value=(mock,)) @@ -464,7 +464,7 @@ def test_trace_init_custom_sampler_with_env_bad_factory( "group": "opentelemetry_traces_sampler", "load.return_value": ( CustomSamplerFactory.empty_get_custom_sampler - ) + ), } ) mock_entry_points.configure_mock(return_value=(mock,)) @@ -520,7 +520,7 @@ def test_trace_init_custom_sampler_with_env_unused_arg( **{ "name": "custom_sampler_factory", "group": "opentelemetry_traces_sampler", - "load.return_value": CustomSamplerFactory.get_custom_sampler + "load.return_value": CustomSamplerFactory.get_custom_sampler, } ) mock_entry_points.configure_mock(return_value=(mock,)) @@ -576,7 +576,7 @@ def test_trace_init_custom_ratio_sampler_with_env(self, mock_entry_points): "group": "opentelemetry_traces_sampler", "load.return_value": ( CustomSamplerFactory.get_custom_ratio_sampler - ) + ), } ) mock_entry_points.configure_mock(return_value=(mock,)) @@ -635,7 +635,7 @@ def test_trace_init_custom_ratio_sampler_with_env_bad_arg( "group": "opentelemetry_traces_sampler", "load.return_value": ( CustomSamplerFactory.get_custom_ratio_sampler - ) + ), } ) mock_entry_points.configure_mock(return_value=(mock,)) @@ -692,7 +692,7 @@ def test_trace_init_custom_ratio_sampler_with_env_missing_arg( "group": "opentelemetry_traces_sampler", "load.return_value": ( CustomSamplerFactory.get_custom_ratio_sampler - ) + ), } ) mock_entry_points.configure_mock(return_value=(mock,)) @@ -750,7 +750,7 @@ def test_trace_init_custom_ratio_sampler_with_env_multiple_entry_points( "group": "opentelemetry_traces_sampler", "load.return_value": ( CustomSamplerFactory.get_custom_ratio_sampler - ) + ), } ) mock_sampler_factory = Mock() @@ -760,7 +760,7 @@ def test_trace_init_custom_ratio_sampler_with_env_multiple_entry_points( "group": "opentelemetry_traces_sampler", "load.return_value": ( CustomSamplerFactory.get_custom_sampler - ) + ), } ) mock_custom_z_sampler_factory = Mock() @@ -770,7 +770,7 @@ def test_trace_init_custom_ratio_sampler_with_env_multiple_entry_points( "group": "opentelemetry_traces_sampler", "load.return_value": ( CustomSamplerFactory.empty_get_custom_sampler - ) + ), } ) @@ -778,7 +778,7 @@ def test_trace_init_custom_ratio_sampler_with_env_multiple_entry_points( return_value=( mock_ratio_sampler_factory, mock_sampler_factory, - mock_custom_z_sampler_factory + mock_custom_z_sampler_factory, ) ) From cf2b427e9fb5a973f443c261a05d82a56d05152f Mon Sep 17 00:00:00 2001 From: Diego Hurtado Date: Mon, 28 Nov 2022 20:45:26 +0100 Subject: [PATCH 09/29] Fix lint --- .../src/opentelemetry/context/__init__.py | 8 ++++---- .../src/opentelemetry/propagate/__init__.py | 8 ++++---- opentelemetry-api/src/opentelemetry/util/_providers.py | 10 +++++----- opentelemetry-api/tests/util/test__providers.py | 3 +-- 4 files changed, 14 insertions(+), 15 deletions(-) diff --git a/opentelemetry-api/src/opentelemetry/context/__init__.py b/opentelemetry-api/src/opentelemetry/context/__init__.py index 1a04de1a613..011ac915428 100644 --- a/opentelemetry-api/src/opentelemetry/context/__init__.py +++ b/opentelemetry-api/src/opentelemetry/context/__init__.py @@ -25,7 +25,7 @@ # pylint: disable=import-error from importlib_metadata import entry_points # type: ignore else: - from importlib.metadata import entry_points + from importlib.metadata import entry_points # type: ignore # pylint: disable=wrong-import-position from opentelemetry.context.context import Context, _RuntimeContext @@ -65,10 +65,10 @@ def wrapper( # type: ignore[misc] try: # FIXME Remove when support for 3.7 is dropped. if version_info.minor == 7: - for entry_point in entry_points(): + for entry_point in entry_points(): # type: ignore if ( - entry_point.group == "opentelemetry_context" - and entry_point.name == configured_context + entry_point.group == "opentelemetry_context" # type: ignore + and entry_point.name == configured_context # type: ignore ): _RUNTIME_CONTEXT = entry_point.load()() # type: ignore break diff --git a/opentelemetry-api/src/opentelemetry/propagate/__init__.py b/opentelemetry-api/src/opentelemetry/propagate/__init__.py index e5d3cf91308..839245aeb2d 100644 --- a/opentelemetry-api/src/opentelemetry/propagate/__init__.py +++ b/opentelemetry-api/src/opentelemetry/propagate/__init__.py @@ -82,7 +82,7 @@ def example_route(): # pylint: disable=import-error from importlib_metadata import entry_points # type: ignore else: - from importlib.metadata import entry_points + from importlib.metadata import entry_points # type: ignore logger = getLogger(__name__) @@ -143,10 +143,10 @@ def inject( # FIXME: Remove when support for 3.7 is dropped. if version_info.minor == 7: - for entry_point in entry_points(): + for entry_point in entry_points(): # type: ignore if ( - entry_point.group == "opentelemetry_propagator" - and entry_point.name == propagator + entry_point.group == "opentelemetry_propagator" # type: ignore + and entry_point.name == propagator # type: ignore ): propagators.append(entry_point.load()()) # type: ignore diff --git a/opentelemetry-api/src/opentelemetry/util/_providers.py b/opentelemetry-api/src/opentelemetry/util/_providers.py index a6d6ddf4d8b..db2cd8fd448 100644 --- a/opentelemetry-api/src/opentelemetry/util/_providers.py +++ b/opentelemetry-api/src/opentelemetry/util/_providers.py @@ -22,7 +22,7 @@ # pylint: disable=import-error from importlib_metadata import entry_points # type: ignore else: - from importlib.metadata import entry_points + from importlib.metadata import entry_points # type: ignore if TYPE_CHECKING: from opentelemetry.metrics import MeterProvider @@ -54,14 +54,14 @@ def _load_provider( for entry_point in entry_points(): # type: ignore if ( - entry_point.name == provider_name - and entry_point.group == f"opentelemetry_{provider}" - ): # type: ignore + entry_point.name == provider_name # type: ignore + and entry_point.group == f"opentelemetry_{provider}" # type: ignore + ): return cast(Provider, entry_point.load()()) # type: ignore raise Exception(f"Provider {provider_name} not found") # FIXME remove when support for 3.9 is dropped. - elif version_info.minor <= 9: + if version_info.minor <= 9: for entry_point in entry_points()[f"opentelemetry_{provider}"]: # type: ignore if entry_point.name == provider_name: # type: ignore diff --git a/opentelemetry-api/tests/util/test__providers.py b/opentelemetry-api/tests/util/test__providers.py index 048fc8f6c4c..41f99bb5022 100644 --- a/opentelemetry-api/tests/util/test__providers.py +++ b/opentelemetry-api/tests/util/test__providers.py @@ -31,12 +31,11 @@ class Test_Providers(TestCase): @patch.dict( environ, - { + { # type: ignore "provider_environment_variable": "mock_provider_environment_variable" }, ) @patch(entry_points_path) - # @patch("opentelemetry.util._providers.entry_points") def test__providers(self, mock_entry_points): reload(_providers) From 5d5a74f2fc9d5e422a8e827214ffbd957f4bedbf Mon Sep 17 00:00:00 2001 From: Diego Hurtado Date: Mon, 28 Nov 2022 20:53:53 +0100 Subject: [PATCH 10/29] Fix mypy again --- opentelemetry-api/src/opentelemetry/context/__init__.py | 2 +- opentelemetry-api/src/opentelemetry/propagate/__init__.py | 2 +- opentelemetry-api/src/opentelemetry/util/_providers.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/opentelemetry-api/src/opentelemetry/context/__init__.py b/opentelemetry-api/src/opentelemetry/context/__init__.py index 011ac915428..4f6f60512f8 100644 --- a/opentelemetry-api/src/opentelemetry/context/__init__.py +++ b/opentelemetry-api/src/opentelemetry/context/__init__.py @@ -25,7 +25,7 @@ # pylint: disable=import-error from importlib_metadata import entry_points # type: ignore else: - from importlib.metadata import entry_points # type: ignore + from importlib.metadata import entry_points # pylint: disable=wrong-import-position from opentelemetry.context.context import Context, _RuntimeContext diff --git a/opentelemetry-api/src/opentelemetry/propagate/__init__.py b/opentelemetry-api/src/opentelemetry/propagate/__init__.py index 839245aeb2d..0aa16299102 100644 --- a/opentelemetry-api/src/opentelemetry/propagate/__init__.py +++ b/opentelemetry-api/src/opentelemetry/propagate/__init__.py @@ -82,7 +82,7 @@ def example_route(): # pylint: disable=import-error from importlib_metadata import entry_points # type: ignore else: - from importlib.metadata import entry_points # type: ignore + from importlib.metadata import entry_points logger = getLogger(__name__) diff --git a/opentelemetry-api/src/opentelemetry/util/_providers.py b/opentelemetry-api/src/opentelemetry/util/_providers.py index db2cd8fd448..a1c4e2837d8 100644 --- a/opentelemetry-api/src/opentelemetry/util/_providers.py +++ b/opentelemetry-api/src/opentelemetry/util/_providers.py @@ -22,7 +22,7 @@ # pylint: disable=import-error from importlib_metadata import entry_points # type: ignore else: - from importlib.metadata import entry_points # type: ignore + from importlib.metadata import entry_points if TYPE_CHECKING: from opentelemetry.metrics import MeterProvider From 8b627fe3a1cba897d4cb2490324d161db07fb69b Mon Sep 17 00:00:00 2001 From: Diego Hurtado Date: Tue, 6 Dec 2022 09:20:27 -0600 Subject: [PATCH 11/29] WIP --- opentelemetry-api/src/opentelemetry/context/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/opentelemetry-api/src/opentelemetry/context/__init__.py b/opentelemetry-api/src/opentelemetry/context/__init__.py index 4f6f60512f8..e53e353e78e 100644 --- a/opentelemetry-api/src/opentelemetry/context/__init__.py +++ b/opentelemetry-api/src/opentelemetry/context/__init__.py @@ -28,7 +28,7 @@ from importlib.metadata import entry_points # pylint: disable=wrong-import-position -from opentelemetry.context.context import Context, _RuntimeContext +from opentelemetry.context.context import Context from opentelemetry.environment_variables import OTEL_PYTHON_CONTEXT logger = logging.getLogger(__name__) From 09111e441b80d1b40c688266265c9d8997b142ed Mon Sep 17 00:00:00 2001 From: Diego Hurtado Date: Tue, 6 Dec 2022 09:59:09 -0600 Subject: [PATCH 12/29] WIP --- .../src/opentelemetry/context/__init__.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/opentelemetry-api/src/opentelemetry/context/__init__.py b/opentelemetry-api/src/opentelemetry/context/__init__.py index e53e353e78e..410b59194dc 100644 --- a/opentelemetry-api/src/opentelemetry/context/__init__.py +++ b/opentelemetry-api/src/opentelemetry/context/__init__.py @@ -65,13 +65,12 @@ def wrapper( # type: ignore[misc] try: # FIXME Remove when support for 3.7 is dropped. if version_info.minor == 7: - for entry_point in entry_points(): # type: ignore - if ( - entry_point.group == "opentelemetry_context" # type: ignore - and entry_point.name == configured_context # type: ignore - ): - _RUNTIME_CONTEXT = entry_point.load()() # type: ignore - break + for entry_point in entry_points( + group="opentelemetry_context", + name=configured_context + ): # type: ignore + _RUNTIME_CONTEXT = entry_point.load()() # type: ignore + break # FIXME Remove when support for 3.9 is dropped. elif version_info.minor <= 9: From 25af79adafdaa84f4bd0e66c3b0ca1a0706dc473 Mon Sep 17 00:00:00 2001 From: Diego Hurtado Date: Tue, 6 Dec 2022 11:27:57 -0600 Subject: [PATCH 13/29] WIP --- .../src/opentelemetry/context/__init__.py | 33 +++++++++++-------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/opentelemetry-api/src/opentelemetry/context/__init__.py b/opentelemetry-api/src/opentelemetry/context/__init__.py index 410b59194dc..77080bd395c 100644 --- a/opentelemetry-api/src/opentelemetry/context/__init__.py +++ b/opentelemetry-api/src/opentelemetry/context/__init__.py @@ -63,17 +63,8 @@ def wrapper( # type: ignore[misc] OTEL_PYTHON_CONTEXT, default_context ) # type: str try: - # FIXME Remove when support for 3.7 is dropped. - if version_info.minor == 7: - for entry_point in entry_points( - group="opentelemetry_context", - name=configured_context - ): # type: ignore - _RUNTIME_CONTEXT = entry_point.load()() # type: ignore - break - # FIXME Remove when support for 3.9 is dropped. - elif version_info.minor <= 9: + if version_info.minor == 8 or version_info.minor == 9: for entry_point in entry_points()[ # type: ignore "opentelemetry_context" ]: @@ -86,10 +77,24 @@ def wrapper( # type: ignore[misc] f" {configured_context}" ) else: - _RUNTIME_CONTEXT = entry_points( # type: ignore - group="opentelemetry_context", - name=configured_context, - )[0].load()() + + try: + _RUNTIME_CONTEXT = next( + iter( + entry_points( # type: ignore + group="opentelemetry_context", + name=configured_context, + ) + ) + ).load()() + + except StopIteration: + + raise Exception( + f"No entry point found for configured_context:" + f" {configured_context}" + ) + except Exception: # pylint: disable=broad-except logger.exception( "Failed to load context: %s", configured_context From 78ee090bf56ec4a703b9ba18f00701fba0b57844 Mon Sep 17 00:00:00 2001 From: Diego Hurtado Date: Tue, 6 Dec 2022 14:01:44 -0600 Subject: [PATCH 14/29] WIP --- .../src/opentelemetry/propagate/__init__.py | 33 ++++++++---------- .../tests/propagators/test_propagators.py | 34 +------------------ 2 files changed, 15 insertions(+), 52 deletions(-) diff --git a/opentelemetry-api/src/opentelemetry/propagate/__init__.py b/opentelemetry-api/src/opentelemetry/propagate/__init__.py index 0aa16299102..5a1e586bc51 100644 --- a/opentelemetry-api/src/opentelemetry/propagate/__init__.py +++ b/opentelemetry-api/src/opentelemetry/propagate/__init__.py @@ -138,21 +138,10 @@ def inject( for propagator in environ_propagators.split(","): propagator = propagator.strip() - try: - - # FIXME: Remove when support for 3.7 is dropped. - if version_info.minor == 7: - - for entry_point in entry_points(): # type: ignore - if ( - entry_point.group == "opentelemetry_propagator" # type: ignore - and entry_point.name == propagator # type: ignore - ): - propagators.append(entry_point.load()()) # type: ignore - - # FIXME: Remove when support for 3.9 is dropped. - elif version_info.minor <= 9: + try: + # FIXME Remove when support for 3.9 is dropped. + if version_info.minor in (8, 9): for entry_point in entry_points().get( # type: ignore "opentelemetry_propagator", [] ): @@ -160,18 +149,24 @@ def inject( propagators.append(entry_point.load()()) # type: ignore else: - propagators.append( # type: ignore - entry_points( # type: ignore - group="opentelemetry_propagator", name=propagator - )[0].load()() + propagators.append( + next( + iter( + entry_points( # type: ignore + group="opentelemetry_propagator", + name=propagator, + ) + ) + ).load()() ) except Exception: # pylint: disable=broad-except logger.exception( - "Failed to load configured propagator `%s`", propagator + "Failed to load configured propagator: %s", propagator ) raise + _HTTP_TEXT_FORMAT = composite.CompositePropagator(propagators) # type: ignore diff --git a/opentelemetry-api/tests/propagators/test_propagators.py b/opentelemetry-api/tests/propagators/test_propagators.py index 9cc51e52bfd..d7980e48d39 100644 --- a/opentelemetry-api/tests/propagators/test_propagators.py +++ b/opentelemetry-api/tests/propagators/test_propagators.py @@ -65,40 +65,8 @@ def test_non_default_propagators( self, mock_entry_points, mock_compositehttppropagator ): - # FIXME Remove when support for 3.7 is dropped. - if version_info.minor == 7: - - mock_a = Mock() - mock_a.configure_mock( - **{ - "name": "a", - "group": "opentelemetry_propagator", - "load.return_value": Mock(**{"return_value": "a"}), - } - ) - mock_b = Mock() - mock_b.configure_mock( - **{ - "name": "b", - "group": "opentelemetry_propagator", - "load.return_value": Mock(**{"return_value": "b"}), - } - ) - mock_c = Mock() - mock_c.configure_mock( - **{ - "name": "c", - "group": "opentelemetry_propagator", - "load.return_value": Mock(**{"return_value": "c"}), - } - ) - - mock_entry_points.configure_mock( - **{"return_value": (mock_a, mock_b, mock_c)} - ) - # FIXME Remove when support for 3.9 is dropped. - elif version_info.minor <= 9: + if version_info.minor in (8, 9): mock_a = Mock() mock_a.configure_mock( From 34e4e5c174ad90ca69dbc06d034becb04f0c3ba9 Mon Sep 17 00:00:00 2001 From: Diego Hurtado Date: Tue, 6 Dec 2022 14:20:02 -0600 Subject: [PATCH 15/29] Move to module --- .../src/opentelemetry/context/__init__.py | 8 +------ .../src/opentelemetry/propagate/__init__.py | 7 +----- .../src/opentelemetry/util/_entry_points.py | 24 +++++++++++++++++++ .../src/opentelemetry/util/_providers.py | 24 ++----------------- .../tests/propagators/test_propagators.py | 8 +------ .../tests/util/test__providers.py | 24 ++----------------- 6 files changed, 31 insertions(+), 64 deletions(-) create mode 100644 opentelemetry-api/src/opentelemetry/util/_entry_points.py diff --git a/opentelemetry-api/src/opentelemetry/context/__init__.py b/opentelemetry-api/src/opentelemetry/context/__init__.py index 77080bd395c..58cdc560c63 100644 --- a/opentelemetry-api/src/opentelemetry/context/__init__.py +++ b/opentelemetry-api/src/opentelemetry/context/__init__.py @@ -19,13 +19,7 @@ from os import environ from uuid import uuid4 from sys import version_info - -# FIXME remove when support for 3.7 is dropped. -if version_info.minor == 7: - # pylint: disable=import-error - from importlib_metadata import entry_points # type: ignore -else: - from importlib.metadata import entry_points +from opentelemetry.util._entry_points import entry_points # pylint: disable=wrong-import-position from opentelemetry.context.context import Context diff --git a/opentelemetry-api/src/opentelemetry/propagate/__init__.py b/opentelemetry-api/src/opentelemetry/propagate/__init__.py index 5a1e586bc51..2c21b27b033 100644 --- a/opentelemetry-api/src/opentelemetry/propagate/__init__.py +++ b/opentelemetry-api/src/opentelemetry/propagate/__init__.py @@ -76,13 +76,8 @@ def example_route(): from opentelemetry.context.context import Context from opentelemetry.environment_variables import OTEL_PROPAGATORS from opentelemetry.propagators import composite, textmap +from opentelemetry.util._entry_points import entry_points -# FIXME remove when support for 3.7 is dropped. -if version_info.minor == 7: - # pylint: disable=import-error - from importlib_metadata import entry_points # type: ignore -else: - from importlib.metadata import entry_points logger = getLogger(__name__) diff --git a/opentelemetry-api/src/opentelemetry/util/_entry_points.py b/opentelemetry-api/src/opentelemetry/util/_entry_points.py new file mode 100644 index 00000000000..9871937909d --- /dev/null +++ b/opentelemetry-api/src/opentelemetry/util/_entry_points.py @@ -0,0 +1,24 @@ +# Copyright The OpenTelemetry Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from sys import version_info + +# FIXME remove this file when support for 3.7 is dropped. +if version_info.minor == 7: + # pylint: disable=import-error + from importlib_metadata import entry_points # type: ignore +else: + from importlib.metadata import entry_points + +__all__ = ["entry_points"] diff --git a/opentelemetry-api/src/opentelemetry/util/_providers.py b/opentelemetry-api/src/opentelemetry/util/_providers.py index a1c4e2837d8..cbad08c1134 100644 --- a/opentelemetry-api/src/opentelemetry/util/_providers.py +++ b/opentelemetry-api/src/opentelemetry/util/_providers.py @@ -16,13 +16,7 @@ from os import environ from sys import version_info from typing import TYPE_CHECKING, TypeVar, cast - -# FIXME remove when support for 3.7 is dropped. -if version_info.minor == 7: - # pylint: disable=import-error - from importlib_metadata import entry_points # type: ignore -else: - from importlib.metadata import entry_points +from opentelemetry.util._entry_points import entry_points if TYPE_CHECKING: from opentelemetry.metrics import MeterProvider @@ -40,7 +34,7 @@ def _load_provider( try: # FIXME remove when support for 3.9 is dropped. - if version_info.minor <= 9: + if version_info.minor in (8, 9): provider_name = cast( str, @@ -49,20 +43,6 @@ def _load_provider( ), ) - # FIXME remove when support for 3.7 is dropped. - if version_info.minor == 7: - - for entry_point in entry_points(): # type: ignore - if ( - entry_point.name == provider_name # type: ignore - and entry_point.group == f"opentelemetry_{provider}" # type: ignore - ): - return cast(Provider, entry_point.load()()) # type: ignore - raise Exception(f"Provider {provider_name} not found") - - # FIXME remove when support for 3.9 is dropped. - if version_info.minor <= 9: - for entry_point in entry_points()[f"opentelemetry_{provider}"]: # type: ignore if entry_point.name == provider_name: # type: ignore return cast(Provider, entry_point.load()()) # type: ignore diff --git a/opentelemetry-api/tests/propagators/test_propagators.py b/opentelemetry-api/tests/propagators/test_propagators.py index d7980e48d39..c963ce091f0 100644 --- a/opentelemetry-api/tests/propagators/test_propagators.py +++ b/opentelemetry-api/tests/propagators/test_propagators.py @@ -52,15 +52,9 @@ def test_propagators(propagators): reload(opentelemetry.propagate) - # FIXME Remove when support for 3.7 is dropped. - if version_info.minor == 7: - entry_points_path = "importlib_metadata.entry_points" - else: - entry_points_path = "importlib.metadata.entry_points" - @patch.dict(environ, {OTEL_PROPAGATORS: "a, b, c "}) @patch("opentelemetry.propagators.composite.CompositePropagator") - @patch(entry_points_path) + @patch("opentelemetry.util._entry_points.entry_points") def test_non_default_propagators( self, mock_entry_points, mock_compositehttppropagator ): diff --git a/opentelemetry-api/tests/util/test__providers.py b/opentelemetry-api/tests/util/test__providers.py index 41f99bb5022..ed42d27af70 100644 --- a/opentelemetry-api/tests/util/test__providers.py +++ b/opentelemetry-api/tests/util/test__providers.py @@ -23,39 +23,19 @@ class Test_Providers(TestCase): - # FIXME Remove when support for 3.7 is dropped. - if version_info.minor == 7: - entry_points_path = "importlib_metadata.entry_points" - else: - entry_points_path = "importlib.metadata.entry_points" - @patch.dict( environ, { # type: ignore "provider_environment_variable": "mock_provider_environment_variable" }, ) - @patch(entry_points_path) + @patch("opentelemetry.util._entry_points.entry_points") def test__providers(self, mock_entry_points): reload(_providers) - # FIXME Remove when support for 3.7 is dropped. - if version_info.minor == 7: - - mock_a = Mock() - mock_a.configure_mock( - **{ - "name": "mock_provider_environment_variable", - "group": "opentelemetry_provider", - "load.return_value": Mock(**{"return_value": "a"}), - } - ) - - mock_entry_points.configure_mock(**{"return_value": (mock_a,)}) - # FIXME Remove when support for 3.9 is dropped. - elif version_info.minor <= 9: + if version_info.minor in (8, 9): mock_a = Mock() mock_a.configure_mock( From a63a8cfce2bb8d6599e172fbf072f8d6bdb01d01 Mon Sep 17 00:00:00 2001 From: Diego Hurtado Date: Tue, 6 Dec 2022 14:54:11 -0600 Subject: [PATCH 16/29] Fix SDK --- .../src/opentelemetry/context/__init__.py | 6 +- .../src/opentelemetry/propagate/__init__.py | 1 - .../src/opentelemetry/util/_entry_points.py | 6 +- .../src/opentelemetry/util/_providers.py | 1 + .../tests/util/test__providers.py | 1 - opentelemetry-api/tests/util/test_re.py | 4 +- .../sdk/_configuration/__init__.py | 33 +--- .../sdk/error_handler/__init__.py | 9 +- .../opentelemetry/sdk/resources/__init__.py | 9 +- .../tests/error_handler/test_error_handler.py | 6 +- opentelemetry-sdk/tests/test_configurator.py | 163 ++---------------- 11 files changed, 37 insertions(+), 202 deletions(-) diff --git a/opentelemetry-api/src/opentelemetry/context/__init__.py b/opentelemetry-api/src/opentelemetry/context/__init__.py index 58cdc560c63..43488a3e2e7 100644 --- a/opentelemetry-api/src/opentelemetry/context/__init__.py +++ b/opentelemetry-api/src/opentelemetry/context/__init__.py @@ -19,11 +19,11 @@ from os import environ from uuid import uuid4 from sys import version_info -from opentelemetry.util._entry_points import entry_points # pylint: disable=wrong-import-position -from opentelemetry.context.context import Context +from opentelemetry.context.context import Context, _RuntimeContext # noqa from opentelemetry.environment_variables import OTEL_PYTHON_CONTEXT +from opentelemetry.util._entry_points import entry_points logger = logging.getLogger(__name__) _RUNTIME_CONTEXT = None # type: typing.Optional[_RuntimeContext] @@ -58,7 +58,7 @@ def wrapper( # type: ignore[misc] ) # type: str try: # FIXME Remove when support for 3.9 is dropped. - if version_info.minor == 8 or version_info.minor == 9: + if version_info.minor in (8, 9): for entry_point in entry_points()[ # type: ignore "opentelemetry_context" ]: diff --git a/opentelemetry-api/src/opentelemetry/propagate/__init__.py b/opentelemetry-api/src/opentelemetry/propagate/__init__.py index 2c21b27b033..2eb4f56800c 100644 --- a/opentelemetry-api/src/opentelemetry/propagate/__init__.py +++ b/opentelemetry-api/src/opentelemetry/propagate/__init__.py @@ -78,7 +78,6 @@ def example_route(): from opentelemetry.propagators import composite, textmap from opentelemetry.util._entry_points import entry_points - logger = getLogger(__name__) diff --git a/opentelemetry-api/src/opentelemetry/util/_entry_points.py b/opentelemetry-api/src/opentelemetry/util/_entry_points.py index 9871937909d..c92ee436a8c 100644 --- a/opentelemetry-api/src/opentelemetry/util/_entry_points.py +++ b/opentelemetry-api/src/opentelemetry/util/_entry_points.py @@ -17,8 +17,8 @@ # FIXME remove this file when support for 3.7 is dropped. if version_info.minor == 7: # pylint: disable=import-error - from importlib_metadata import entry_points # type: ignore + from importlib_metadata import entry_points, version # type: ignore else: - from importlib.metadata import entry_points + from importlib.metadata import entry_points, version -__all__ = ["entry_points"] +__all__ = ["entry_points", "version"] diff --git a/opentelemetry-api/src/opentelemetry/util/_providers.py b/opentelemetry-api/src/opentelemetry/util/_providers.py index cbad08c1134..038b7fde436 100644 --- a/opentelemetry-api/src/opentelemetry/util/_providers.py +++ b/opentelemetry-api/src/opentelemetry/util/_providers.py @@ -16,6 +16,7 @@ from os import environ from sys import version_info from typing import TYPE_CHECKING, TypeVar, cast + from opentelemetry.util._entry_points import entry_points if TYPE_CHECKING: diff --git a/opentelemetry-api/tests/util/test__providers.py b/opentelemetry-api/tests/util/test__providers.py index ed42d27af70..a4d5e074b1a 100644 --- a/opentelemetry-api/tests/util/test__providers.py +++ b/opentelemetry-api/tests/util/test__providers.py @@ -22,7 +22,6 @@ class Test_Providers(TestCase): - @patch.dict( environ, { # type: ignore diff --git a/opentelemetry-api/tests/util/test_re.py b/opentelemetry-api/tests/util/test_re.py index 85515846ec1..ea86f3e7006 100644 --- a/opentelemetry-api/tests/util/test_re.py +++ b/opentelemetry-api/tests/util/test_re.py @@ -63,7 +63,9 @@ def test_parse_env_headers(self): headers, expected, warn = case_ if warn: with self.assertLogs(level="WARNING") as cm: - self.assertEqual(parse_env_headers(headers), dict(expected)) + self.assertEqual( + parse_env_headers(headers), dict(expected) + ) self.assertTrue( "Header format invalid! Header values in environment " "variables must be URL encoded per the OpenTelemetry " diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/_configuration/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/_configuration/__init__.py index 6fe698b21c9..fa45ac55c8c 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/_configuration/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/_configuration/__init__.py @@ -57,13 +57,7 @@ from opentelemetry.sdk.trace.sampling import Sampler from opentelemetry.semconv.resource import ResourceAttributes from opentelemetry.trace import set_tracer_provider - -# FIXME remove when support for 3.7 is dropped. -if version_info.minor == 7: - # pylint: disable=import-error - from importlib_metadata import entry_points -else: - from importlib.metadata import entry_points +from opentelemetry.util._entry_points import entry_points _EXPORTER_OTLP = "otlp" _EXPORTER_OTLP_PROTO_GRPC = "otlp_proto_grpc" @@ -100,21 +94,8 @@ def _import_config_components( component_implementations = [] - # FIXME remove when support for 3.7 is dropped. - if version_info.minor == 7: - - for entry_point in entry_points(): - for selected_component in selected_components: - if ( - entry_point.name == selected_component - and entry_point.group == entry_point_name - ): - component_implementations.append( - (selected_component, entry_point.load()) - ) - # FIXME remove when support for 3.9 is dropped. - elif version_info.minor <= 9: + if version_info.minor in (8, 9): for entry_point in entry_points()[entry_point_name]: for selected_component in selected_components: @@ -128,9 +109,13 @@ def _import_config_components( component_implementations.append( ( selected_component, - entry_points( - group=entry_point_name, name=selected_component - )[0].load(), + next( + iter( + entry_points( + group=entry_point_name, name=selected_component + ) + ) + ).load(), ) ) diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/error_handler/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/error_handler/__init__.py index 3503cdb0194..8fbe3e13679 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/error_handler/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/error_handler/__init__.py @@ -63,12 +63,7 @@ def _handle(self, error: Exception, *args, **kwargs): from logging import getLogger from sys import version_info -# FIXME remove when support for 3.7 is dropped. -if version_info.minor == 7: - # pylint: disable=import-error - from importlib_metadata import entry_points -else: - from importlib.metadata import entry_points +from opentelemetry.util._entry_points import entry_points logger = getLogger(__name__) @@ -125,7 +120,7 @@ def __exit__(self, exc_type, exc_value, traceback): plugin_handled = False # FIXME remove when support for 3.9 is dropped. - if version_info.minor <= 9: + if version_info.minor in (8, 9): error_handler_entry_points = entry_points()[ "opentelemetry_error_handler" diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py index 030965e9664..1a7c9aaa7bd 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py @@ -63,7 +63,6 @@ import typing from json import dumps from urllib import parse -from sys import version_info from opentelemetry.attributes import BoundedAttributes from opentelemetry.sdk.environment_variables import ( @@ -71,15 +70,9 @@ OTEL_SERVICE_NAME, ) from opentelemetry.semconv.resource import ResourceAttributes +from opentelemetry.util._entry_points import version from opentelemetry.util.types import AttributeValue -# FIXME remove when support for 3.7 is dropped. -if version_info.minor == 7: - # pylint: disable=import-error - from importlib_metadata import version -else: - from importlib.metadata import version - LabelValue = AttributeValue Attributes = typing.Dict[str, LabelValue] logger = logging.getLogger(__name__) diff --git a/opentelemetry-sdk/tests/error_handler/test_error_handler.py b/opentelemetry-sdk/tests/error_handler/test_error_handler.py index 148cbe28236..2fb6f24196f 100644 --- a/opentelemetry-sdk/tests/error_handler/test_error_handler.py +++ b/opentelemetry-sdk/tests/error_handler/test_error_handler.py @@ -56,7 +56,7 @@ class AssertionErrorHandler(ErrorHandler, AssertionError): ) # FIXME remove when support for 3.9 is dropped. - if version_info.minor <= 9: + if version_info.minor in (8, 9): mock_entry_points.configure_mock( **{ @@ -109,7 +109,7 @@ def _handle(self, error: Exception): ) # FIXME remove when support for 3.9 is dropped. - if version_info.minor <= 9: + if version_info.minor in (8, 9): mock_entry_points.configure_mock( **{ @@ -149,7 +149,7 @@ def __new__(cls): ) # FIXME remove when support for 3.9 is dropped. - if version_info.minor <= 9: + if version_info.minor in (8, 9): mock_entry_points.configure_mock( **{ diff --git a/opentelemetry-sdk/tests/test_configurator.py b/opentelemetry-sdk/tests/test_configurator.py index d8d8df87f0c..621fe91b7ae 100644 --- a/opentelemetry-sdk/tests/test_configurator.py +++ b/opentelemetry-sdk/tests/test_configurator.py @@ -14,12 +14,12 @@ # type: ignore # pylint: skip-file -import logging +from logging import getLogger from os import environ from sys import version_info from typing import Dict, Iterable, Optional, Sequence from unittest import TestCase -from unittest.mock import Mock, patch +from unittest.mock import patch from opentelemetry import trace from opentelemetry.context import Context @@ -352,20 +352,8 @@ def test_trace_init_otlp(self): @patch("opentelemetry.sdk._configuration.IdGenerator", new=IdGenerator) @patch("opentelemetry.sdk._configuration.entry_points") def test_trace_init_custom_id_generator(self, mock_entry_points): - # FIXME remove when support for 3.7 is dropped. - if version_info.minor == 7: - - mock = Mock() - mock.configure_mock( - **{ - "name": "custom_id_generator", - "group": "opentelemetry_id_generator", - "load.return_value": CustomIdGenerator, - } - ) - mock_entry_points.configure_mock(return_value=(mock,)) # FIXME remove when support for 3.9 is dropped. - elif version_info.minor <= 9: + if version_info.minor in (8, 9): mock_entry_points.configure_mock( return_value={ @@ -405,21 +393,8 @@ def test_trace_init_custom_sampler_with_env_non_existent_entry_point(self): @patch.dict("os.environ", {OTEL_TRACES_SAMPLER: "custom_sampler_factory"}) def test_trace_init_custom_sampler_with_env(self, mock_entry_points): - # FIXME remove when support for 3.7 is dropped. - if version_info.minor == 7: - - mock = Mock() - mock.configure_mock( - **{ - "name": "custom_sampler_factory", - "group": "opentelemetry_traces_sampler", - "load.return_value": CustomSamplerFactory.get_custom_sampler, - } - ) - mock_entry_points.configure_mock(return_value=(mock,)) - # FIXME remove when support for 3.9 is dropped. - elif version_info.minor <= 9: + if version_info.minor in (8, 9): mock_entry_points.configure_mock( return_value={ @@ -454,23 +429,9 @@ def test_trace_init_custom_sampler_with_env(self, mock_entry_points): def test_trace_init_custom_sampler_with_env_bad_factory( self, mock_entry_points ): - # FIXME remove when support for 3.7 is dropped. - if version_info.minor == 7: - - mock = Mock() - mock.configure_mock( - **{ - "name": "custom_sampler_factory", - "group": "opentelemetry_traces_sampler", - "load.return_value": ( - CustomSamplerFactory.empty_get_custom_sampler - ), - } - ) - mock_entry_points.configure_mock(return_value=(mock,)) # FIXME remove when support for 3.9 is dropped. - elif version_info.minor <= 9: + if version_info.minor in (8, 9): mock_entry_points.configure_mock( return_value={ @@ -512,21 +473,8 @@ def test_trace_init_custom_sampler_with_env_unused_arg( self, mock_entry_points ): - # FIXME remove when support for 3.7 is dropped. - if version_info.minor == 7: - - mock = Mock() - mock.configure_mock( - **{ - "name": "custom_sampler_factory", - "group": "opentelemetry_traces_sampler", - "load.return_value": CustomSamplerFactory.get_custom_sampler, - } - ) - mock_entry_points.configure_mock(return_value=(mock,)) - # FIXME remove when support for 3.9 is dropped. - elif version_info.minor <= 9: + if version_info.minor in (8, 9): mock_entry_points.configure_mock( return_value={ @@ -566,23 +514,8 @@ def test_trace_init_custom_sampler_with_env_unused_arg( ) def test_trace_init_custom_ratio_sampler_with_env(self, mock_entry_points): - # FIXME remove when support for 3.7 is dropped. - if version_info.minor == 7: - - mock = Mock() - mock.configure_mock( - **{ - "name": "custom_ratio_sampler_factory", - "group": "opentelemetry_traces_sampler", - "load.return_value": ( - CustomSamplerFactory.get_custom_ratio_sampler - ), - } - ) - mock_entry_points.configure_mock(return_value=(mock,)) - # FIXME remove when support for 3.9 is dropped. - elif version_info.minor <= 9: + if version_info.minor in (8, 9): mock_entry_points.configure_mock( return_value={ @@ -625,23 +558,8 @@ def test_trace_init_custom_ratio_sampler_with_env_bad_arg( self, mock_entry_points ): - # FIXME remove when support for 3.7 is dropped. - if version_info.minor == 7: - - mock = Mock() - mock.configure_mock( - **{ - "name": "custom_ratio_sampler_factory", - "group": "opentelemetry_traces_sampler", - "load.return_value": ( - CustomSamplerFactory.get_custom_ratio_sampler - ), - } - ) - mock_entry_points.configure_mock(return_value=(mock,)) - # FIXME remove when support for 3.9 is dropped. - elif version_info.minor <= 9: + if version_info.minor in (8, 9): mock_entry_points.configure_mock( return_value={ @@ -682,23 +600,8 @@ def test_trace_init_custom_ratio_sampler_with_env_missing_arg( self, mock_entry_points ): - # FIXME remove when support for 3.7 is dropped. - if version_info.minor == 7: - - mock = Mock() - mock.configure_mock( - **{ - "name": "custom_ratio_sampler_factory", - "group": "opentelemetry_traces_sampler", - "load.return_value": ( - CustomSamplerFactory.get_custom_ratio_sampler - ), - } - ) - mock_entry_points.configure_mock(return_value=(mock,)) - # FIXME remove when support for 3.9 is dropped. - elif version_info.minor <= 9: + if version_info.minor in (8, 9): mock_entry_points.configure_mock( return_value={ @@ -740,50 +643,8 @@ def test_trace_init_custom_ratio_sampler_with_env_multiple_entry_points( self, mock_entry_points ): - # FIXME remove when support for 3.7 is dropped. - if version_info.minor == 7: - - mock_ratio_sampler_factory = Mock() - mock_ratio_sampler_factory.configure_mock( - **{ - "name": "custom_ratio_sampler_factory", - "group": "opentelemetry_traces_sampler", - "load.return_value": ( - CustomSamplerFactory.get_custom_ratio_sampler - ), - } - ) - mock_sampler_factory = Mock() - mock_sampler_factory.configure_mock( - **{ - "name": "custom_sampler_factory", - "group": "opentelemetry_traces_sampler", - "load.return_value": ( - CustomSamplerFactory.get_custom_sampler - ), - } - ) - mock_custom_z_sampler_factory = Mock() - mock_custom_z_sampler_factory.configure_mock( - **{ - "name": "custom_z_sampler_factory", - "group": "opentelemetry_traces_sampler", - "load.return_value": ( - CustomSamplerFactory.empty_get_custom_sampler - ), - } - ) - - mock_entry_points.configure_mock( - return_value=( - mock_ratio_sampler_factory, - mock_sampler_factory, - mock_custom_z_sampler_factory, - ) - ) - # FIXME remove when support for 3.9 is dropped. - elif version_info.minor <= 9: + if version_info.minor in (8, 9): mock_entry_points.configure_mock( return_value={ @@ -849,7 +710,7 @@ def tearDown(self): self.processor_patch.stop() self.set_provider_patch.stop() self.provider_patch.stop() - root_logger = logging.getLogger("root") + root_logger = getLogger("root") root_logger.handlers = [ handler for handler in root_logger.handlers @@ -885,7 +746,7 @@ def test_logging_init_exporter(self): self.assertIsInstance( provider.processor.exporter, DummyOTLPLogExporter ) - logging.getLogger(__name__).error("hello") + getLogger(__name__).error("hello") self.assertTrue(provider.processor.exporter.export_called) @patch.dict( From 0e0c46ae82cced10011e6b8b4b20402e1339cf7e Mon Sep 17 00:00:00 2001 From: Diego Hurtado Date: Tue, 6 Dec 2022 15:25:39 -0600 Subject: [PATCH 17/29] Fix mypy --- opentelemetry-api/src/opentelemetry/context/__init__.py | 4 ++-- opentelemetry-api/src/opentelemetry/propagate/__init__.py | 6 +++--- opentelemetry-api/src/opentelemetry/util/_entry_points.py | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/opentelemetry-api/src/opentelemetry/context/__init__.py b/opentelemetry-api/src/opentelemetry/context/__init__.py index 43488a3e2e7..6be2ccc03a9 100644 --- a/opentelemetry-api/src/opentelemetry/context/__init__.py +++ b/opentelemetry-api/src/opentelemetry/context/__init__.py @@ -73,8 +73,8 @@ def wrapper( # type: ignore[misc] else: try: - _RUNTIME_CONTEXT = next( - iter( + _RUNTIME_CONTEXT = next( # type: ignore + iter( # type: ignore entry_points( # type: ignore group="opentelemetry_context", name=configured_context, diff --git a/opentelemetry-api/src/opentelemetry/propagate/__init__.py b/opentelemetry-api/src/opentelemetry/propagate/__init__.py index 2eb4f56800c..3343e249f08 100644 --- a/opentelemetry-api/src/opentelemetry/propagate/__init__.py +++ b/opentelemetry-api/src/opentelemetry/propagate/__init__.py @@ -143,9 +143,9 @@ def inject( propagators.append(entry_point.load()()) # type: ignore else: - propagators.append( - next( - iter( + propagators.append( # type: ignore + next( # type: ignore + iter( # type: ignore entry_points( # type: ignore group="opentelemetry_propagator", name=propagator, diff --git a/opentelemetry-api/src/opentelemetry/util/_entry_points.py b/opentelemetry-api/src/opentelemetry/util/_entry_points.py index c92ee436a8c..1d464c0c32e 100644 --- a/opentelemetry-api/src/opentelemetry/util/_entry_points.py +++ b/opentelemetry-api/src/opentelemetry/util/_entry_points.py @@ -19,6 +19,6 @@ # pylint: disable=import-error from importlib_metadata import entry_points, version # type: ignore else: - from importlib.metadata import entry_points, version + from importlib.metadata import entry_points, version # type: ignore __all__ = ["entry_points", "version"] From ff4e9d995d3efe5fd0ed1781bfdd9db8082edb13 Mon Sep 17 00:00:00 2001 From: Diego Hurtado Date: Tue, 6 Dec 2022 15:32:53 -0600 Subject: [PATCH 18/29] Refactor load call --- .../src/opentelemetry/util/_providers.py | 24 +++++++++++-------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/opentelemetry-api/src/opentelemetry/util/_providers.py b/opentelemetry-api/src/opentelemetry/util/_providers.py index 038b7fde436..16e2cd4238f 100644 --- a/opentelemetry-api/src/opentelemetry/util/_providers.py +++ b/opentelemetry-api/src/opentelemetry/util/_providers.py @@ -51,16 +51,20 @@ def _load_provider( return cast( Provider, - entry_points( # type: ignore - group=f"opentelemetry_{provider}", - name=cast( - str, - environ.get( - provider_environment_variable, - f"default_{provider}", - ), - ), - )[0].load()(), + next( + iter( + entry_points( # type: ignore + group=f"opentelemetry_{provider}", + name=cast( + str, + environ.get( + provider_environment_variable, + f"default_{provider}", + ), + ), + ) + ) + ).load()(), ) except Exception: # pylint: disable=broad-except logger.exception("Failed to load configured provider %s", provider) From ace3324dbf2fd90dcb4536650eadc9a59bdeb768 Mon Sep 17 00:00:00 2001 From: Diego Hurtado Date: Tue, 6 Dec 2022 15:36:16 -0600 Subject: [PATCH 19/29] Fix mypy --- opentelemetry-api/src/opentelemetry/util/_providers.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/opentelemetry-api/src/opentelemetry/util/_providers.py b/opentelemetry-api/src/opentelemetry/util/_providers.py index 16e2cd4238f..c703cee9541 100644 --- a/opentelemetry-api/src/opentelemetry/util/_providers.py +++ b/opentelemetry-api/src/opentelemetry/util/_providers.py @@ -51,8 +51,8 @@ def _load_provider( return cast( Provider, - next( - iter( + next( # type: ignore + iter( # type: ignore entry_points( # type: ignore group=f"opentelemetry_{provider}", name=cast( From 1df755663c188c0edcdcc1ea58a195d4f312e8d2 Mon Sep 17 00:00:00 2001 From: Diego Hurtado Date: Tue, 6 Dec 2022 15:45:04 -0600 Subject: [PATCH 20/29] Fix lint --- .../src/opentelemetry/util/_providers.py | 20 ++++++------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/opentelemetry-api/src/opentelemetry/util/_providers.py b/opentelemetry-api/src/opentelemetry/util/_providers.py index c703cee9541..f51337b66b3 100644 --- a/opentelemetry-api/src/opentelemetry/util/_providers.py +++ b/opentelemetry-api/src/opentelemetry/util/_providers.py @@ -34,16 +34,14 @@ def _load_provider( try: + provider_name = cast( + str, + environ.get(provider_environment_variable, f"default_{provider}"), + ) + # FIXME remove when support for 3.9 is dropped. if version_info.minor in (8, 9): - provider_name = cast( - str, - environ.get( - provider_environment_variable, f"default_{provider}" - ), - ) - for entry_point in entry_points()[f"opentelemetry_{provider}"]: # type: ignore if entry_point.name == provider_name: # type: ignore return cast(Provider, entry_point.load()()) # type: ignore @@ -55,13 +53,7 @@ def _load_provider( iter( # type: ignore entry_points( # type: ignore group=f"opentelemetry_{provider}", - name=cast( - str, - environ.get( - provider_environment_variable, - f"default_{provider}", - ), - ), + name=provider_name, ) ) ).load()(), From df3041e640c6c79c2319ef63f7b1cf7c0ae30792 Mon Sep 17 00:00:00 2001 From: Diego Hurtado Date: Tue, 6 Dec 2022 15:53:09 -0600 Subject: [PATCH 21/29] Fix opencensus exporter --- .../opentelemetry/exporter/opencensus/util.py | 23 ++++++------------- 1 file changed, 7 insertions(+), 16 deletions(-) diff --git a/exporter/opentelemetry-exporter-opencensus/src/opentelemetry/exporter/opencensus/util.py b/exporter/opentelemetry-exporter-opencensus/src/opentelemetry/exporter/opencensus/util.py index 8334d7b8a92..bf34ae05369 100644 --- a/exporter/opentelemetry-exporter-opencensus/src/opentelemetry/exporter/opencensus/util.py +++ b/exporter/opentelemetry-exporter-opencensus/src/opentelemetry/exporter/opencensus/util.py @@ -12,17 +12,9 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os -import socket -import time -from sys import version_info - -# FIXME remove when support for 3.7 is dropped. -if version_info.minor == 7: - # pylint: disable=import-error - from importlib_metadata import version -else: - from importlib.metadata import version +from os import getpid +from socket import gethostname +from time import time # pylint: disable=wrong-import-position from google.protobuf.timestamp_pb2 import Timestamp @@ -33,6 +25,7 @@ __version__ as opencensusexporter_exporter_version, ) from opentelemetry.trace import SpanKind +from opentelemetry.util._entry_points import version OPENTELEMETRY_VERSION = version("opentelemetry-api") @@ -94,11 +87,9 @@ def get_node(service_name, host_name): """ return common_pb2.Node( identifier=common_pb2.ProcessIdentifier( - host_name=socket.gethostname() if host_name is None else host_name, - pid=os.getpid(), - start_timestamp=proto_timestamp_from_time_ns( - int(time.time() * 1e9) - ), + host_name=gethostname() if host_name is None else host_name, + pid=getpid(), + start_timestamp=proto_timestamp_from_time_ns(int(time() * 1e9)), ), library_info=common_pb2.LibraryInfo( language=common_pb2.LibraryInfo.Language.Value("PYTHON"), From 622e81cb0218757bf9bb2c39f40a66f3fa254c66 Mon Sep 17 00:00:00 2001 From: Diego Hurtado Date: Wed, 14 Dec 2022 14:33:12 -0600 Subject: [PATCH 22/29] Refactor implementation --- .../opentelemetry/exporter/opencensus/util.py | 2 +- .../src/opentelemetry/context/__init__.py | 41 +-- .../src/opentelemetry/propagate/__init__.py | 37 +-- ...entry_points.py => _importlib_metadata.py} | 18 +- .../src/opentelemetry/util/_providers.py | 11 +- .../tests/propagators/test_propagators.py | 104 +++---- .../tests/util/test__importlib_metadata.py | 34 +++ .../tests/util/test__providers.py | 51 ++-- .../sdk/_configuration/__init__.py | 36 +-- .../sdk/error_handler/__init__.py | 18 +- .../opentelemetry/sdk/resources/__init__.py | 2 +- .../tests/error_handler/test_error_handler.py | 75 +---- opentelemetry-sdk/tests/test_configurator.py | 265 ++++-------------- 13 files changed, 217 insertions(+), 477 deletions(-) rename opentelemetry-api/src/opentelemetry/util/{_entry_points.py => _importlib_metadata.py} (57%) create mode 100644 opentelemetry-api/tests/util/test__importlib_metadata.py diff --git a/exporter/opentelemetry-exporter-opencensus/src/opentelemetry/exporter/opencensus/util.py b/exporter/opentelemetry-exporter-opencensus/src/opentelemetry/exporter/opencensus/util.py index bf34ae05369..694e8dc6a10 100644 --- a/exporter/opentelemetry-exporter-opencensus/src/opentelemetry/exporter/opencensus/util.py +++ b/exporter/opentelemetry-exporter-opencensus/src/opentelemetry/exporter/opencensus/util.py @@ -25,7 +25,7 @@ __version__ as opencensusexporter_exporter_version, ) from opentelemetry.trace import SpanKind -from opentelemetry.util._entry_points import version +from opentelemetry.util._importlib_metadata import version OPENTELEMETRY_VERSION = version("opentelemetry-api") diff --git a/opentelemetry-api/src/opentelemetry/context/__init__.py b/opentelemetry-api/src/opentelemetry/context/__init__.py index 6be2ccc03a9..3e85b64fe48 100644 --- a/opentelemetry-api/src/opentelemetry/context/__init__.py +++ b/opentelemetry-api/src/opentelemetry/context/__init__.py @@ -18,12 +18,11 @@ from functools import wraps from os import environ from uuid import uuid4 -from sys import version_info # pylint: disable=wrong-import-position from opentelemetry.context.context import Context, _RuntimeContext # noqa from opentelemetry.environment_variables import OTEL_PYTHON_CONTEXT -from opentelemetry.util._entry_points import entry_points +from opentelemetry.util._importlib_metadata import entry_points logger = logging.getLogger(__name__) _RUNTIME_CONTEXT = None # type: typing.Optional[_RuntimeContext] @@ -57,37 +56,15 @@ def wrapper( # type: ignore[misc] OTEL_PYTHON_CONTEXT, default_context ) # type: str try: - # FIXME Remove when support for 3.9 is dropped. - if version_info.minor in (8, 9): - for entry_point in entry_points()[ # type: ignore - "opentelemetry_context" - ]: - if entry_point.name == configured_context: # type: ignore - _RUNTIME_CONTEXT = entry_point.load()() # type: ignore - break - else: - raise Exception( - f"No entry point found for configured_context:" - f" {configured_context}" - ) - else: - - try: - _RUNTIME_CONTEXT = next( # type: ignore - iter( # type: ignore - entry_points( # type: ignore - group="opentelemetry_context", - name=configured_context, - ) - ) - ).load()() - - except StopIteration: - - raise Exception( - f"No entry point found for configured_context:" - f" {configured_context}" + + _RUNTIME_CONTEXT = next( # type: ignore + iter( # type: ignore + entry_points( # type: ignore + group="opentelemetry_context", + name=configured_context, ) + ) + ).load()() except Exception: # pylint: disable=broad-except logger.exception( diff --git a/opentelemetry-api/src/opentelemetry/propagate/__init__.py b/opentelemetry-api/src/opentelemetry/propagate/__init__.py index 3343e249f08..56f217f282d 100644 --- a/opentelemetry-api/src/opentelemetry/propagate/__init__.py +++ b/opentelemetry-api/src/opentelemetry/propagate/__init__.py @@ -68,22 +68,21 @@ def example_route(): https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/context/api-propagators.md """ -import typing from logging import getLogger from os import environ -from sys import version_info +from typing import Optional from opentelemetry.context.context import Context from opentelemetry.environment_variables import OTEL_PROPAGATORS from opentelemetry.propagators import composite, textmap -from opentelemetry.util._entry_points import entry_points +from opentelemetry.util._importlib_metadata import entry_points logger = getLogger(__name__) def extract( carrier: textmap.CarrierT, - context: typing.Optional[Context] = None, + context: Optional[Context] = None, getter: textmap.Getter[textmap.CarrierT] = textmap.default_getter, ) -> Context: """Uses the configured propagator to extract a Context from the carrier. @@ -104,7 +103,7 @@ def extract( def inject( carrier: textmap.CarrierT, - context: typing.Optional[Context] = None, + context: Optional[Context] = None, setter: textmap.Setter[textmap.CarrierT] = textmap.default_setter, ) -> None: """Uses the configured propagator to inject a Context into the carrier. @@ -134,25 +133,17 @@ def inject( propagator = propagator.strip() try: - # FIXME Remove when support for 3.9 is dropped. - if version_info.minor in (8, 9): - for entry_point in entry_points().get( # type: ignore - "opentelemetry_propagator", [] - ): - if entry_point.name == propagator: # type: ignore - propagators.append(entry_point.load()()) # type: ignore - else: - - propagators.append( # type: ignore - next( # type: ignore - iter( # type: ignore - entry_points( # type: ignore - group="opentelemetry_propagator", - name=propagator, - ) + + propagators.append( # type: ignore + next( # type: ignore + iter( # type: ignore + entry_points( # type: ignore + group="opentelemetry_propagator", + name=propagator, ) - ).load()() - ) + ) + ).load()() + ) except Exception: # pylint: disable=broad-except logger.exception( diff --git a/opentelemetry-api/src/opentelemetry/util/_entry_points.py b/opentelemetry-api/src/opentelemetry/util/_importlib_metadata.py similarity index 57% rename from opentelemetry-api/src/opentelemetry/util/_entry_points.py rename to opentelemetry-api/src/opentelemetry/util/_importlib_metadata.py index 1d464c0c32e..889c4cf1ac2 100644 --- a/opentelemetry-api/src/opentelemetry/util/_entry_points.py +++ b/opentelemetry-api/src/opentelemetry/util/_importlib_metadata.py @@ -14,11 +14,25 @@ from sys import version_info -# FIXME remove this file when support for 3.7 is dropped. +# FIXME remove this when support for 3.7 is dropped. if version_info.minor == 7: # pylint: disable=import-error from importlib_metadata import entry_points, version # type: ignore + +# FIXME remove this file when support for 3.9 is dropped. +elif version_info.minor in (8, 9): + # pylint: disable=import-error + from importlib.metadata import ( + entry_points as importlib_metadata_entry_points, + ) + from importlib.metadata import version + + def entry_points(group: str, name: str): # type: ignore + for entry_point in importlib_metadata_entry_points()[group]: + if entry_point.name == name: + yield entry_point + else: - from importlib.metadata import entry_points, version # type: ignore + from importlib.metadata import entry_points, version __all__ = ["entry_points", "version"] diff --git a/opentelemetry-api/src/opentelemetry/util/_providers.py b/opentelemetry-api/src/opentelemetry/util/_providers.py index f51337b66b3..d255ac999f0 100644 --- a/opentelemetry-api/src/opentelemetry/util/_providers.py +++ b/opentelemetry-api/src/opentelemetry/util/_providers.py @@ -14,10 +14,9 @@ from logging import getLogger from os import environ -from sys import version_info from typing import TYPE_CHECKING, TypeVar, cast -from opentelemetry.util._entry_points import entry_points +from opentelemetry.util._importlib_metadata import entry_points if TYPE_CHECKING: from opentelemetry.metrics import MeterProvider @@ -39,14 +38,6 @@ def _load_provider( environ.get(provider_environment_variable, f"default_{provider}"), ) - # FIXME remove when support for 3.9 is dropped. - if version_info.minor in (8, 9): - - for entry_point in entry_points()[f"opentelemetry_{provider}"]: # type: ignore - if entry_point.name == provider_name: # type: ignore - return cast(Provider, entry_point.load()()) # type: ignore - raise Exception(f"Provider {provider_name} not found") - return cast( Provider, next( # type: ignore diff --git a/opentelemetry-api/tests/propagators/test_propagators.py b/opentelemetry-api/tests/propagators/test_propagators.py index c963ce091f0..bb84bc4f1a0 100644 --- a/opentelemetry-api/tests/propagators/test_propagators.py +++ b/opentelemetry-api/tests/propagators/test_propagators.py @@ -17,7 +17,6 @@ from importlib import reload from logging import ERROR from os import environ -from sys import version_info from unittest import TestCase from unittest.mock import Mock, patch @@ -54,79 +53,44 @@ def test_propagators(propagators): @patch.dict(environ, {OTEL_PROPAGATORS: "a, b, c "}) @patch("opentelemetry.propagators.composite.CompositePropagator") - @patch("opentelemetry.util._entry_points.entry_points") + @patch("opentelemetry.util._importlib_metadata.entry_points") def test_non_default_propagators( self, mock_entry_points, mock_compositehttppropagator ): - # FIXME Remove when support for 3.9 is dropped. - if version_info.minor in (8, 9): - - mock_a = Mock() - mock_a.configure_mock( - **{ - "name": "a", - "load.return_value": Mock(**{"return_value": "a"}), - } - ) - mock_b = Mock() - mock_b.configure_mock( - **{ - "name": "b", - "load.return_value": Mock(**{"return_value": "b"}), - } - ) - mock_c = Mock() - mock_c.configure_mock( - **{ - "name": "c", - "load.return_value": Mock(**{"return_value": "c"}), - } - ) - - mock_entry_points.configure_mock( - **{ - "return_value": { - "opentelemetry_propagator": [mock_a, mock_b, mock_c] - } - } - ) - - else: - - mock_entry_points.configure_mock( - **{ - "side_effect": [ - [ - Mock( - **{ - "load.return_value": Mock( - **{"return_value": "a"} - ) - } - ), - ], - [ - Mock( - **{ - "load.return_value": Mock( - **{"return_value": "b"} - ) - } - ) - ], - [ - Mock( - **{ - "load.return_value": Mock( - **{"return_value": "c"} - ) - } - ) - ], - ] - } - ) + mock_entry_points.configure_mock( + **{ + "side_effect": [ + [ + Mock( + **{ + "load.return_value": Mock( + **{"return_value": "a"} + ) + } + ), + ], + [ + Mock( + **{ + "load.return_value": Mock( + **{"return_value": "b"} + ) + } + ) + ], + [ + Mock( + **{ + "load.return_value": Mock( + **{"return_value": "c"} + ) + } + ) + ], + ] + } + ) def test_propagators(propagators): self.assertEqual(propagators, ["a", "b", "c"]) diff --git a/opentelemetry-api/tests/util/test__importlib_metadata.py b/opentelemetry-api/tests/util/test__importlib_metadata.py new file mode 100644 index 00000000000..7ca58881b8e --- /dev/null +++ b/opentelemetry-api/tests/util/test__importlib_metadata.py @@ -0,0 +1,34 @@ +# Copyright The OpenTelemetry Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from unittest import TestCase + +from opentelemetry.metrics import MeterProvider +from opentelemetry.util._importlib_metadata import entry_points + + +class TestEntryPoints(TestCase): + def test_entry_points(self): + + self.assertIsInstance( + next( + iter( + entry_points( + group="opentelemetry_meter_provider", + name="default_meter_provider", + ) + ) + ).load()(), + MeterProvider, + ) diff --git a/opentelemetry-api/tests/util/test__providers.py b/opentelemetry-api/tests/util/test__providers.py index a4d5e074b1a..f7b21ebacf1 100644 --- a/opentelemetry-api/tests/util/test__providers.py +++ b/opentelemetry-api/tests/util/test__providers.py @@ -14,7 +14,6 @@ from importlib import reload from os import environ -from sys import version_info from unittest import TestCase from unittest.mock import Mock, patch @@ -28,44 +27,26 @@ class Test_Providers(TestCase): "provider_environment_variable": "mock_provider_environment_variable" }, ) - @patch("opentelemetry.util._entry_points.entry_points") + @patch("opentelemetry.util._importlib_metadata.entry_points") def test__providers(self, mock_entry_points): reload(_providers) - # FIXME Remove when support for 3.9 is dropped. - if version_info.minor in (8, 9): - - mock_a = Mock() - mock_a.configure_mock( - **{ - "name": "mock_provider_environment_variable", - "group": "opentelemetry_provider", - "load.return_value": Mock(**{"return_value": "a"}), - } - ) - - mock_entry_points.configure_mock( - **{"return_value": {"opentelemetry_provider": [mock_a]}} - ) - - else: - - mock_entry_points.configure_mock( - **{ - "side_effect": [ - [ - Mock( - **{ - "load.return_value": Mock( - **{"return_value": "a"} - ) - } - ), - ], - ] - } - ) + mock_entry_points.configure_mock( + **{ + "side_effect": [ + [ + Mock( + **{ + "load.return_value": Mock( + **{"return_value": "a"} + ) + } + ), + ], + ] + } + ) self.assertEqual( _providers._load_provider( diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/_configuration/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/_configuration/__init__.py index fa45ac55c8c..1c2272a90cd 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/_configuration/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/_configuration/__init__.py @@ -21,7 +21,6 @@ import os from abc import ABC, abstractmethod from os import environ -from sys import version_info from typing import Callable, Dict, List, Optional, Sequence, Tuple, Type from typing_extensions import Literal @@ -57,7 +56,7 @@ from opentelemetry.sdk.trace.sampling import Sampler from opentelemetry.semconv.resource import ResourceAttributes from opentelemetry.trace import set_tracer_provider -from opentelemetry.util._entry_points import entry_points +from opentelemetry.util._importlib_metadata import entry_points _EXPORTER_OTLP = "otlp" _EXPORTER_OTLP_PROTO_GRPC = "otlp_proto_grpc" @@ -94,30 +93,19 @@ def _import_config_components( component_implementations = [] - # FIXME remove when support for 3.9 is dropped. - if version_info.minor in (8, 9): - - for entry_point in entry_points()[entry_point_name]: - for selected_component in selected_components: - if entry_point.name == selected_component: - component_implementations.append( - (selected_component, entry_point.load()) - ) - else: - - for selected_component in selected_components: - component_implementations.append( - ( - selected_component, - next( - iter( - entry_points( - group=entry_point_name, name=selected_component - ) + for selected_component in selected_components: + component_implementations.append( + ( + selected_component, + next( + iter( + entry_points( + group=entry_point_name, name=selected_component ) - ).load(), - ) + ) + ).load(), ) + ) return component_implementations diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/error_handler/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/error_handler/__init__.py index 8fbe3e13679..7b21d92d2af 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/error_handler/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/error_handler/__init__.py @@ -61,9 +61,8 @@ def _handle(self, error: Exception, *args, **kwargs): from abc import ABC, abstractmethod from logging import getLogger -from sys import version_info -from opentelemetry.util._entry_points import entry_points +from opentelemetry.util._importlib_metadata import entry_points logger = getLogger(__name__) @@ -119,18 +118,9 @@ def __exit__(self, exc_type, exc_value, traceback): plugin_handled = False - # FIXME remove when support for 3.9 is dropped. - if version_info.minor in (8, 9): - - error_handler_entry_points = entry_points()[ - "opentelemetry_error_handler" - ] - - else: - - error_handler_entry_points = entry_points( - group="opentelemetry_error_handler" - ) + error_handler_entry_points = entry_points( + group="opentelemetry_error_handler" + ) for error_handler_entry_point in error_handler_entry_points: diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py index 1a7c9aaa7bd..c46b87f89cc 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py @@ -70,7 +70,7 @@ OTEL_SERVICE_NAME, ) from opentelemetry.semconv.resource import ResourceAttributes -from opentelemetry.util._entry_points import version +from opentelemetry.util._importlib_metadata import version from opentelemetry.util.types import AttributeValue LabelValue = AttributeValue diff --git a/opentelemetry-sdk/tests/error_handler/test_error_handler.py b/opentelemetry-sdk/tests/error_handler/test_error_handler.py index 2fb6f24196f..116771dc9a1 100644 --- a/opentelemetry-sdk/tests/error_handler/test_error_handler.py +++ b/opentelemetry-sdk/tests/error_handler/test_error_handler.py @@ -14,7 +14,6 @@ # pylint: disable=broad-except from logging import ERROR -from sys import version_info from unittest import TestCase from unittest.mock import Mock, patch @@ -55,30 +54,14 @@ class AssertionErrorHandler(ErrorHandler, AssertionError): **{"load.return_value": AssertionErrorHandler} ) - # FIXME remove when support for 3.9 is dropped. - if version_info.minor in (8, 9): - - mock_entry_points.configure_mock( - **{ - "return_value": { - "opentelemetry_error_handler": [ - mock_entry_point_zero_division_error_handler, - mock_entry_point_assertion_error_handler, - ] - } - } - ) - - else: - - mock_entry_points.configure_mock( - **{ - "return_value": [ - mock_entry_point_zero_division_error_handler, - mock_entry_point_assertion_error_handler, - ] - } - ) + mock_entry_points.configure_mock( + **{ + "return_value": [ + mock_entry_point_zero_division_error_handler, + mock_entry_point_assertion_error_handler, + ] + } + ) error = ZeroDivisionError() @@ -108,24 +91,9 @@ def _handle(self, error: Exception): **{"load.return_value": ErrorErrorHandler} ) - # FIXME remove when support for 3.9 is dropped. - if version_info.minor in (8, 9): - - mock_entry_points.configure_mock( - **{ - "return_value": { - "opentelemetry_error_handler": [ - mock_entry_point_error_error_handler - ] - } - } - ) - - else: - - mock_entry_points.configure_mock( - **{"return_value": [mock_entry_point_error_error_handler]} - ) + mock_entry_points.configure_mock( + **{"return_value": [mock_entry_point_error_error_handler]} + ) error = ZeroDivisionError() @@ -148,24 +116,9 @@ def __new__(cls): **{"load.return_value": MockErrorHandlerClass} ) - # FIXME remove when support for 3.9 is dropped. - if version_info.minor in (8, 9): - - mock_entry_points.configure_mock( - **{ - "return_value": { - "opentelemetry_error_handler": [ - mock_entry_point_error_handler - ] - } - } - ) - - else: - - mock_entry_points.configure_mock( - **{"return_value": [mock_entry_point_error_handler]} - ) + mock_entry_points.configure_mock( + **{"return_value": [mock_entry_point_error_handler]} + ) error = IndexError() diff --git a/opentelemetry-sdk/tests/test_configurator.py b/opentelemetry-sdk/tests/test_configurator.py index 621fe91b7ae..c17bd62c72c 100644 --- a/opentelemetry-sdk/tests/test_configurator.py +++ b/opentelemetry-sdk/tests/test_configurator.py @@ -16,7 +16,6 @@ from logging import getLogger from os import environ -from sys import version_info from typing import Dict, Iterable, Optional, Sequence from unittest import TestCase from unittest.mock import patch @@ -352,26 +351,11 @@ def test_trace_init_otlp(self): @patch("opentelemetry.sdk._configuration.IdGenerator", new=IdGenerator) @patch("opentelemetry.sdk._configuration.entry_points") def test_trace_init_custom_id_generator(self, mock_entry_points): - # FIXME remove when support for 3.9 is dropped. - if version_info.minor in (8, 9): - - mock_entry_points.configure_mock( - return_value={ - "opentelemetry_id_generator": ( - IterEntryPoint( - "custom_id_generator", CustomIdGenerator - ), - ) - } - ) - - else: - - mock_entry_points.configure_mock( - return_value=[ - IterEntryPoint("custom_id_generator", CustomIdGenerator) - ] - ) + mock_entry_points.configure_mock( + return_value=[ + IterEntryPoint("custom_id_generator", CustomIdGenerator) + ] + ) id_generator_name = _get_id_generator() id_generator = _import_id_generator(id_generator_name) @@ -392,31 +376,14 @@ def test_trace_init_custom_sampler_with_env_non_existent_entry_point(self): @patch("opentelemetry.sdk._configuration.entry_points") @patch.dict("os.environ", {OTEL_TRACES_SAMPLER: "custom_sampler_factory"}) def test_trace_init_custom_sampler_with_env(self, mock_entry_points): - - # FIXME remove when support for 3.9 is dropped. - if version_info.minor in (8, 9): - - mock_entry_points.configure_mock( - return_value={ - "opentelemetry_traces_sampler": ( - IterEntryPoint( - "custom_sampler_factory", - CustomSamplerFactory.get_custom_sampler, - ), - ) - } - ) - - else: - - mock_entry_points.configure_mock( - return_value=[ - IterEntryPoint( - "custom_sampler_factory", - CustomSamplerFactory.get_custom_sampler, - ) - ] - ) + mock_entry_points.configure_mock( + return_value=[ + IterEntryPoint( + "custom_sampler_factory", + CustomSamplerFactory.get_custom_sampler, + ) + ] + ) sampler_name = _get_sampler() sampler = _import_sampler(sampler_name) @@ -429,31 +396,14 @@ def test_trace_init_custom_sampler_with_env(self, mock_entry_points): def test_trace_init_custom_sampler_with_env_bad_factory( self, mock_entry_points ): - - # FIXME remove when support for 3.9 is dropped. - if version_info.minor in (8, 9): - - mock_entry_points.configure_mock( - return_value={ - "opentelemetry_traces_sampler": ( - IterEntryPoint( - "custom_sampler_factory", - CustomSamplerFactory.empty_get_custom_sampler, - ), - ) - } - ) - - else: - - mock_entry_points.configure_mock( - return_value=[ - IterEntryPoint( - "custom_sampler_factory", - CustomSamplerFactory.empty_get_custom_sampler, - ) - ] - ) + mock_entry_points.configure_mock( + return_value=[ + IterEntryPoint( + "custom_sampler_factory", + CustomSamplerFactory.empty_get_custom_sampler, + ) + ] + ) sampler_name = _get_sampler() sampler = _import_sampler(sampler_name) @@ -472,31 +422,14 @@ def test_trace_init_custom_sampler_with_env_bad_factory( def test_trace_init_custom_sampler_with_env_unused_arg( self, mock_entry_points ): - - # FIXME remove when support for 3.9 is dropped. - if version_info.minor in (8, 9): - - mock_entry_points.configure_mock( - return_value={ - "opentelemetry_traces_sampler": ( - IterEntryPoint( - "custom_sampler_factory", - CustomSamplerFactory.get_custom_sampler, - ), - ) - } - ) - - else: - - mock_entry_points.configure_mock( - return_value=[ - IterEntryPoint( - "custom_sampler_factory", - CustomSamplerFactory.get_custom_sampler, - ) - ] - ) + mock_entry_points.configure_mock( + return_value=[ + IterEntryPoint( + "custom_sampler_factory", + CustomSamplerFactory.get_custom_sampler, + ) + ] + ) sampler_name = _get_sampler() sampler = _import_sampler(sampler_name) @@ -513,31 +446,14 @@ def test_trace_init_custom_sampler_with_env_unused_arg( }, ) def test_trace_init_custom_ratio_sampler_with_env(self, mock_entry_points): - - # FIXME remove when support for 3.9 is dropped. - if version_info.minor in (8, 9): - - mock_entry_points.configure_mock( - return_value={ - "opentelemetry_traces_sampler": ( - IterEntryPoint( - "custom_ratio_sampler_factory", - CustomSamplerFactory.get_custom_ratio_sampler, - ), - ) - } - ) - - else: - - mock_entry_points.configure_mock( - return_value=[ - IterEntryPoint( - "custom_ratio_sampler_factory", - CustomSamplerFactory.get_custom_ratio_sampler, - ) - ] - ) + mock_entry_points.configure_mock( + return_value=[ + IterEntryPoint( + "custom_ratio_sampler_factory", + CustomSamplerFactory.get_custom_ratio_sampler, + ) + ] + ) sampler_name = _get_sampler() sampler = _import_sampler(sampler_name) @@ -557,31 +473,14 @@ def test_trace_init_custom_ratio_sampler_with_env(self, mock_entry_points): def test_trace_init_custom_ratio_sampler_with_env_bad_arg( self, mock_entry_points ): - - # FIXME remove when support for 3.9 is dropped. - if version_info.minor in (8, 9): - - mock_entry_points.configure_mock( - return_value={ - "opentelemetry_traces_sampler": ( - IterEntryPoint( - "custom_ratio_sampler_factory", - CustomSamplerFactory.get_custom_ratio_sampler, - ), - ) - } - ) - - else: - - mock_entry_points.configure_mock( - return_value=[ - IterEntryPoint( - "custom_ratio_sampler_factory", - CustomSamplerFactory.get_custom_ratio_sampler, - ) - ] - ) + mock_entry_points.configure_mock( + return_value=[ + IterEntryPoint( + "custom_ratio_sampler_factory", + CustomSamplerFactory.get_custom_ratio_sampler, + ) + ] + ) sampler_name = _get_sampler() sampler = _import_sampler(sampler_name) @@ -599,31 +498,14 @@ def test_trace_init_custom_ratio_sampler_with_env_bad_arg( def test_trace_init_custom_ratio_sampler_with_env_missing_arg( self, mock_entry_points ): - - # FIXME remove when support for 3.9 is dropped. - if version_info.minor in (8, 9): - - mock_entry_points.configure_mock( - return_value={ - "opentelemetry_traces_sampler": ( - IterEntryPoint( - "custom_ratio_sampler_factory", - CustomSamplerFactory.get_custom_ratio_sampler, - ), - ) - } - ) - - else: - - mock_entry_points.configure_mock( - return_value=[ - IterEntryPoint( - "custom_ratio_sampler_factory", - CustomSamplerFactory.get_custom_ratio_sampler, - ) - ] - ) + mock_entry_points.configure_mock( + return_value=[ + IterEntryPoint( + "custom_ratio_sampler_factory", + CustomSamplerFactory.get_custom_ratio_sampler, + ) + ] + ) sampler_name = _get_sampler() sampler = _import_sampler(sampler_name) @@ -642,39 +524,14 @@ def test_trace_init_custom_ratio_sampler_with_env_missing_arg( def test_trace_init_custom_ratio_sampler_with_env_multiple_entry_points( self, mock_entry_points ): - - # FIXME remove when support for 3.9 is dropped. - if version_info.minor in (8, 9): - - mock_entry_points.configure_mock( - return_value={ - "opentelemetry_traces_sampler": ( - IterEntryPoint( - "custom_ratio_sampler_factory", - CustomSamplerFactory.get_custom_ratio_sampler, - ), - IterEntryPoint( - "custom_sampler_factory", - CustomSamplerFactory.get_custom_sampler, - ), - IterEntryPoint( - "custom_z_sampler_factory", - CustomSamplerFactory.empty_get_custom_sampler, - ), - ) - } - ) - - else: - - mock_entry_points.configure_mock( - return_value=[ - IterEntryPoint( - "custom_sampler_factory", - CustomSamplerFactory.get_custom_sampler, - ), - ] - ) + mock_entry_points.configure_mock( + return_value=[ + IterEntryPoint( + "custom_sampler_factory", + CustomSamplerFactory.get_custom_sampler, + ), + ] + ) sampler_name = _get_sampler() sampler = _import_sampler(sampler_name) From 036927d15a65b50dea47b3de1af34a3123f738c7 Mon Sep 17 00:00:00 2001 From: Diego Hurtado Date: Wed, 14 Dec 2022 17:28:28 -0600 Subject: [PATCH 23/29] Add missing dependency --- exporter/opentelemetry-exporter-opencensus/pyproject.toml | 1 - opentelemetry-api/pyproject.toml | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/exporter/opentelemetry-exporter-opencensus/pyproject.toml b/exporter/opentelemetry-exporter-opencensus/pyproject.toml index 86a0d921ccb..fe03741dfb9 100644 --- a/exporter/opentelemetry-exporter-opencensus/pyproject.toml +++ b/exporter/opentelemetry-exporter-opencensus/pyproject.toml @@ -32,7 +32,6 @@ dependencies = [ "opentelemetry-sdk ~= 1.3", "protobuf ~= 3.13", "setuptools >= 16.0", - "importlib-metadata >= 5.0.0; python_version=='3.7'" ] [project.optional-dependencies] diff --git a/opentelemetry-api/pyproject.toml b/opentelemetry-api/pyproject.toml index 50b2a8b8a0d..8e245be587c 100644 --- a/opentelemetry-api/pyproject.toml +++ b/opentelemetry-api/pyproject.toml @@ -27,6 +27,7 @@ classifiers = [ dependencies = [ "Deprecated >= 1.2.6", "setuptools >= 16.0", + "importlib-metadata >= 5.0.0; python=='3.7'" ] dynamic = [ "version", From 15f305006e349e6d283335b9f080d8f31c3a8e8d Mon Sep 17 00:00:00 2001 From: Diego Hurtado Date: Wed, 14 Dec 2022 17:39:43 -0600 Subject: [PATCH 24/29] Undo changes in shim --- .../test_asyncio.py | 17 +---------------- .../test_threads.py | 17 +---------------- .../test_client_server/test_asyncio.py | 17 +---------------- .../test_client_server/test_threads.py | 17 +---------------- .../request_handler.py | 15 --------------- .../test_asyncio.py | 17 +---------------- .../test_threads.py | 17 +---------------- .../test_late_span_finish/test_asyncio.py | 17 +---------------- .../test_late_span_finish/test_threads.py | 17 +---------------- .../test_listener_per_request/test_asyncio.py | 17 +---------------- .../test_listener_per_request/test_threads.py | 17 +---------------- .../test_multiple_callbacks/test_asyncio.py | 17 +---------------- .../test_multiple_callbacks/test_threads.py | 17 +---------------- .../test_nested_callbacks/test_asyncio.py | 17 +---------------- .../test_nested_callbacks/test_threads.py | 19 ++----------------- .../test_asyncio.py | 17 +---------------- .../test_threads.py | 17 +---------------- 17 files changed, 17 insertions(+), 272 deletions(-) diff --git a/shim/opentelemetry-opentracing-shim/tests/testbed/test_active_span_replacement/test_asyncio.py b/shim/opentelemetry-opentracing-shim/tests/testbed/test_active_span_replacement/test_asyncio.py index 0419ab44a22..131bb70b91b 100644 --- a/shim/opentelemetry-opentracing-shim/tests/testbed/test_active_span_replacement/test_asyncio.py +++ b/shim/opentelemetry-opentracing-shim/tests/testbed/test_active_span_replacement/test_asyncio.py @@ -1,27 +1,12 @@ -# Copyright The OpenTelemetry Authors -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - import asyncio -# pylint: disable=import-error from ..otel_ot_shim_tracer import MockTracer from ..testcase import OpenTelemetryTestCase from ..utils import stop_loop_when class TestAsyncio(OpenTelemetryTestCase): - def setUp(self): # pylint: disable=invalid-name + def setUp(self): self.tracer = MockTracer() self.loop = asyncio.get_event_loop() diff --git a/shim/opentelemetry-opentracing-shim/tests/testbed/test_active_span_replacement/test_threads.py b/shim/opentelemetry-opentracing-shim/tests/testbed/test_active_span_replacement/test_threads.py index 4e76c87a03e..c8d490063ba 100644 --- a/shim/opentelemetry-opentracing-shim/tests/testbed/test_active_span_replacement/test_threads.py +++ b/shim/opentelemetry-opentracing-shim/tests/testbed/test_active_span_replacement/test_threads.py @@ -1,26 +1,11 @@ -# Copyright The OpenTelemetry Authors -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - from concurrent.futures import ThreadPoolExecutor -# pylint: disable=import-error from ..otel_ot_shim_tracer import MockTracer from ..testcase import OpenTelemetryTestCase class TestThreads(OpenTelemetryTestCase): - def setUp(self): # pylint: disable=invalid-name + def setUp(self): self.tracer = MockTracer() # use max_workers=3 as a general example even if only one would suffice self.executor = ThreadPoolExecutor(max_workers=3) diff --git a/shim/opentelemetry-opentracing-shim/tests/testbed/test_client_server/test_asyncio.py b/shim/opentelemetry-opentracing-shim/tests/testbed/test_client_server/test_asyncio.py index adf99e76b23..d76fffe3b38 100644 --- a/shim/opentelemetry-opentracing-shim/tests/testbed/test_client_server/test_asyncio.py +++ b/shim/opentelemetry-opentracing-shim/tests/testbed/test_client_server/test_asyncio.py @@ -1,23 +1,8 @@ -# Copyright The OpenTelemetry Authors -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - import asyncio import opentracing from opentracing.ext import tags -# pylint: disable=import-error from ..otel_ot_shim_tracer import MockTracer from ..testcase import OpenTelemetryTestCase from ..utils import get_logger, get_one_by_tag, stop_loop_when @@ -65,7 +50,7 @@ async def send(self): class TestAsyncio(OpenTelemetryTestCase): - def setUp(self): # pylint: disable=invalid-name + def setUp(self): self.tracer = MockTracer() self.queue = asyncio.Queue() self.loop = asyncio.get_event_loop() diff --git a/shim/opentelemetry-opentracing-shim/tests/testbed/test_client_server/test_threads.py b/shim/opentelemetry-opentracing-shim/tests/testbed/test_client_server/test_threads.py index 6fa5974d791..df382c34636 100644 --- a/shim/opentelemetry-opentracing-shim/tests/testbed/test_client_server/test_threads.py +++ b/shim/opentelemetry-opentracing-shim/tests/testbed/test_client_server/test_threads.py @@ -1,24 +1,9 @@ -# Copyright The OpenTelemetry Authors -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - from queue import Queue from threading import Thread import opentracing from opentracing.ext import tags -# pylint: disable=import-error from ..otel_ot_shim_tracer import MockTracer from ..testcase import OpenTelemetryTestCase from ..utils import await_until, get_logger, get_one_by_tag @@ -67,7 +52,7 @@ def send(self): class TestThreads(OpenTelemetryTestCase): - def setUp(self): # pylint: disable=invalid-name + def setUp(self): self.tracer = MockTracer() self.queue = Queue() self.server = Server(tracer=self.tracer, queue=self.queue) diff --git a/shim/opentelemetry-opentracing-shim/tests/testbed/test_common_request_handler/request_handler.py b/shim/opentelemetry-opentracing-shim/tests/testbed/test_common_request_handler/request_handler.py index b48a5dbc68b..22d59fbca64 100644 --- a/shim/opentelemetry-opentracing-shim/tests/testbed/test_common_request_handler/request_handler.py +++ b/shim/opentelemetry-opentracing-shim/tests/testbed/test_common_request_handler/request_handler.py @@ -1,20 +1,5 @@ -# Copyright The OpenTelemetry Authors -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - from opentracing.ext import tags -# pylint: disable=import-error from ..utils import get_logger logger = get_logger(__name__) diff --git a/shim/opentelemetry-opentracing-shim/tests/testbed/test_common_request_handler/test_asyncio.py b/shim/opentelemetry-opentracing-shim/tests/testbed/test_common_request_handler/test_asyncio.py index 58970a223c3..14958418a32 100644 --- a/shim/opentelemetry-opentracing-shim/tests/testbed/test_common_request_handler/test_asyncio.py +++ b/shim/opentelemetry-opentracing-shim/tests/testbed/test_common_request_handler/test_asyncio.py @@ -1,22 +1,7 @@ -# Copyright The OpenTelemetry Authors -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - import asyncio from opentracing.ext import tags -# pylint: disable=import-error from ..otel_ot_shim_tracer import MockTracer from ..testcase import OpenTelemetryTestCase from ..utils import get_logger, get_one_by_operation_name, stop_loop_when @@ -60,7 +45,7 @@ class TestAsyncio(OpenTelemetryTestCase): So one issue here is setting correct parent span. """ - def setUp(self): # pylint: disable=invalid-name + def setUp(self): self.tracer = MockTracer() self.loop = asyncio.get_event_loop() self.client = Client(RequestHandler(self.tracer), self.loop) diff --git a/shim/opentelemetry-opentracing-shim/tests/testbed/test_common_request_handler/test_threads.py b/shim/opentelemetry-opentracing-shim/tests/testbed/test_common_request_handler/test_threads.py index fdc0549d62f..6f5022ccc15 100644 --- a/shim/opentelemetry-opentracing-shim/tests/testbed/test_common_request_handler/test_threads.py +++ b/shim/opentelemetry-opentracing-shim/tests/testbed/test_common_request_handler/test_threads.py @@ -1,22 +1,7 @@ -# Copyright The OpenTelemetry Authors -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - from concurrent.futures import ThreadPoolExecutor from opentracing.ext import tags -# pylint: disable=import-error from ..otel_ot_shim_tracer import MockTracer from ..testcase import OpenTelemetryTestCase from ..utils import get_logger, get_one_by_operation_name @@ -60,7 +45,7 @@ class TestThreads(OpenTelemetryTestCase): activate span. So one issue here is setting correct parent span. """ - def setUp(self): # pylint: disable=invalid-name + def setUp(self): self.tracer = MockTracer() self.executor = ThreadPoolExecutor(max_workers=3) self.client = Client(RequestHandler(self.tracer), self.executor) diff --git a/shim/opentelemetry-opentracing-shim/tests/testbed/test_late_span_finish/test_asyncio.py b/shim/opentelemetry-opentracing-shim/tests/testbed/test_late_span_finish/test_asyncio.py index d27e51ca88f..86a47c6a737 100644 --- a/shim/opentelemetry-opentracing-shim/tests/testbed/test_late_span_finish/test_asyncio.py +++ b/shim/opentelemetry-opentracing-shim/tests/testbed/test_late_span_finish/test_asyncio.py @@ -1,20 +1,5 @@ -# Copyright The OpenTelemetry Authors -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - import asyncio -# pylint: disable=import-error from ..otel_ot_shim_tracer import MockTracer from ..testcase import OpenTelemetryTestCase from ..utils import get_logger, stop_loop_when @@ -23,7 +8,7 @@ class TestAsyncio(OpenTelemetryTestCase): - def setUp(self): # pylint: disable=invalid-name + def setUp(self): self.tracer = MockTracer() self.loop = asyncio.get_event_loop() diff --git a/shim/opentelemetry-opentracing-shim/tests/testbed/test_late_span_finish/test_threads.py b/shim/opentelemetry-opentracing-shim/tests/testbed/test_late_span_finish/test_threads.py index 2cd43d7e70b..de8acb70bf3 100644 --- a/shim/opentelemetry-opentracing-shim/tests/testbed/test_late_span_finish/test_threads.py +++ b/shim/opentelemetry-opentracing-shim/tests/testbed/test_late_span_finish/test_threads.py @@ -1,27 +1,12 @@ -# Copyright The OpenTelemetry Authors -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - import time from concurrent.futures import ThreadPoolExecutor -# pylint: disable=import-error from ..otel_ot_shim_tracer import MockTracer from ..testcase import OpenTelemetryTestCase class TestThreads(OpenTelemetryTestCase): - def setUp(self): # pylint: disable=invalid-name + def setUp(self): self.tracer = MockTracer() self.executor = ThreadPoolExecutor(max_workers=3) diff --git a/shim/opentelemetry-opentracing-shim/tests/testbed/test_listener_per_request/test_asyncio.py b/shim/opentelemetry-opentracing-shim/tests/testbed/test_listener_per_request/test_asyncio.py index d0f0a6a577e..66999f04bf2 100644 --- a/shim/opentelemetry-opentracing-shim/tests/testbed/test_listener_per_request/test_asyncio.py +++ b/shim/opentelemetry-opentracing-shim/tests/testbed/test_listener_per_request/test_asyncio.py @@ -1,22 +1,7 @@ -# Copyright The OpenTelemetry Authors -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - import asyncio from opentracing.ext import tags -# pylint: disable=import-error from ..otel_ot_shim_tracer import MockTracer from ..testcase import OpenTelemetryTestCase from ..utils import get_one_by_tag @@ -43,7 +28,7 @@ def send_sync(self, message): class TestAsyncio(OpenTelemetryTestCase): - def setUp(self): # pylint: disable=invalid-name + def setUp(self): self.tracer = MockTracer() self.loop = asyncio.get_event_loop() diff --git a/shim/opentelemetry-opentracing-shim/tests/testbed/test_listener_per_request/test_threads.py b/shim/opentelemetry-opentracing-shim/tests/testbed/test_listener_per_request/test_threads.py index 39d0a3d1d4c..b810db1f01c 100644 --- a/shim/opentelemetry-opentracing-shim/tests/testbed/test_listener_per_request/test_threads.py +++ b/shim/opentelemetry-opentracing-shim/tests/testbed/test_listener_per_request/test_threads.py @@ -1,22 +1,7 @@ -# Copyright The OpenTelemetry Authors -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - from concurrent.futures import ThreadPoolExecutor from opentracing.ext import tags -# pylint: disable=import-error from ..otel_ot_shim_tracer import MockTracer from ..testcase import OpenTelemetryTestCase from ..utils import get_one_by_tag @@ -43,7 +28,7 @@ def send_sync(self, message): class TestThreads(OpenTelemetryTestCase): - def setUp(self): # pylint: disable=invalid-name + def setUp(self): self.tracer = MockTracer() def test_main(self): diff --git a/shim/opentelemetry-opentracing-shim/tests/testbed/test_multiple_callbacks/test_asyncio.py b/shim/opentelemetry-opentracing-shim/tests/testbed/test_multiple_callbacks/test_asyncio.py index bbfb620a840..3754367878c 100644 --- a/shim/opentelemetry-opentracing-shim/tests/testbed/test_multiple_callbacks/test_asyncio.py +++ b/shim/opentelemetry-opentracing-shim/tests/testbed/test_multiple_callbacks/test_asyncio.py @@ -1,21 +1,6 @@ -# Copyright The OpenTelemetry Authors -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - import asyncio import random -# pylint: disable=import-error from ..otel_ot_shim_tracer import MockTracer from ..testcase import OpenTelemetryTestCase from ..utils import get_logger, stop_loop_when @@ -25,7 +10,7 @@ class TestAsyncio(OpenTelemetryTestCase): - def setUp(self): # pylint: disable=invalid-name + def setUp(self): self.tracer = MockTracer() self.loop = asyncio.get_event_loop() diff --git a/shim/opentelemetry-opentracing-shim/tests/testbed/test_multiple_callbacks/test_threads.py b/shim/opentelemetry-opentracing-shim/tests/testbed/test_multiple_callbacks/test_threads.py index 6e8b405ccee..dfb567c7454 100644 --- a/shim/opentelemetry-opentracing-shim/tests/testbed/test_multiple_callbacks/test_threads.py +++ b/shim/opentelemetry-opentracing-shim/tests/testbed/test_multiple_callbacks/test_threads.py @@ -1,22 +1,7 @@ -# Copyright The OpenTelemetry Authors -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - import random import time from concurrent.futures import ThreadPoolExecutor -# pylint: disable=import-error from ..otel_ot_shim_tracer import MockTracer from ..testcase import OpenTelemetryTestCase from ..utils import RefCount, get_logger @@ -26,7 +11,7 @@ class TestThreads(OpenTelemetryTestCase): - def setUp(self): # pylint: disable=invalid-name + def setUp(self): self.tracer = MockTracer() self.executor = ThreadPoolExecutor(max_workers=3) diff --git a/shim/opentelemetry-opentracing-shim/tests/testbed/test_nested_callbacks/test_asyncio.py b/shim/opentelemetry-opentracing-shim/tests/testbed/test_nested_callbacks/test_asyncio.py index f00258624ca..b6b8277d38f 100644 --- a/shim/opentelemetry-opentracing-shim/tests/testbed/test_nested_callbacks/test_asyncio.py +++ b/shim/opentelemetry-opentracing-shim/tests/testbed/test_nested_callbacks/test_asyncio.py @@ -1,27 +1,12 @@ -# Copyright The OpenTelemetry Authors -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - import asyncio -# pylint: disable=import-error from ..otel_ot_shim_tracer import MockTracer from ..testcase import OpenTelemetryTestCase from ..utils import stop_loop_when class TestAsyncio(OpenTelemetryTestCase): - def setUp(self): # pylint: disable=invalid-name + def setUp(self): self.tracer = MockTracer() self.loop = asyncio.get_event_loop() diff --git a/shim/opentelemetry-opentracing-shim/tests/testbed/test_nested_callbacks/test_threads.py b/shim/opentelemetry-opentracing-shim/tests/testbed/test_nested_callbacks/test_threads.py index 955298537da..1f86b2dfba3 100644 --- a/shim/opentelemetry-opentracing-shim/tests/testbed/test_nested_callbacks/test_threads.py +++ b/shim/opentelemetry-opentracing-shim/tests/testbed/test_nested_callbacks/test_threads.py @@ -1,31 +1,16 @@ -# Copyright The OpenTelemetry Authors -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - from concurrent.futures import ThreadPoolExecutor -# pylint: disable=import-error from ..otel_ot_shim_tracer import MockTracer from ..testcase import OpenTelemetryTestCase from ..utils import await_until class TestThreads(OpenTelemetryTestCase): - def setUp(self): # pylint: disable=invalid-name + def setUp(self): self.tracer = MockTracer() self.executor = ThreadPoolExecutor(max_workers=3) - def tearDown(self): # pylint: disable=invalid-name + def tearDown(self): self.executor.shutdown(False) def test_main(self): diff --git a/shim/opentelemetry-opentracing-shim/tests/testbed/test_subtask_span_propagation/test_asyncio.py b/shim/opentelemetry-opentracing-shim/tests/testbed/test_subtask_span_propagation/test_asyncio.py index 653f9bd810e..bd08ee6d098 100644 --- a/shim/opentelemetry-opentracing-shim/tests/testbed/test_subtask_span_propagation/test_asyncio.py +++ b/shim/opentelemetry-opentracing-shim/tests/testbed/test_subtask_span_propagation/test_asyncio.py @@ -1,26 +1,11 @@ -# Copyright The OpenTelemetry Authors -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - import asyncio -# pylint: disable=import-error from ..otel_ot_shim_tracer import MockTracer from ..testcase import OpenTelemetryTestCase class TestAsyncio(OpenTelemetryTestCase): - def setUp(self): # pylint: disable=invalid-name + def setUp(self): self.tracer = MockTracer() self.loop = asyncio.get_event_loop() diff --git a/shim/opentelemetry-opentracing-shim/tests/testbed/test_subtask_span_propagation/test_threads.py b/shim/opentelemetry-opentracing-shim/tests/testbed/test_subtask_span_propagation/test_threads.py index 0d003c9062a..ea15aafb9a6 100644 --- a/shim/opentelemetry-opentracing-shim/tests/testbed/test_subtask_span_propagation/test_threads.py +++ b/shim/opentelemetry-opentracing-shim/tests/testbed/test_subtask_span_propagation/test_threads.py @@ -1,26 +1,11 @@ -# Copyright The OpenTelemetry Authors -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - from concurrent.futures import ThreadPoolExecutor -# pylint: disable=import-error from ..otel_ot_shim_tracer import MockTracer from ..testcase import OpenTelemetryTestCase class TestThreads(OpenTelemetryTestCase): - def setUp(self): # pylint: disable=invalid-name + def setUp(self): self.tracer = MockTracer() self.executor = ThreadPoolExecutor(max_workers=3) From 3ea9a8d0882db0ed6c359eab1c063ad9afb5c1ee Mon Sep 17 00:00:00 2001 From: Diego Hurtado Date: Wed, 14 Dec 2022 17:45:50 -0600 Subject: [PATCH 25/29] Fix dependency --- opentelemetry-api/pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/opentelemetry-api/pyproject.toml b/opentelemetry-api/pyproject.toml index 8e245be587c..e9490ab1d20 100644 --- a/opentelemetry-api/pyproject.toml +++ b/opentelemetry-api/pyproject.toml @@ -27,7 +27,7 @@ classifiers = [ dependencies = [ "Deprecated >= 1.2.6", "setuptools >= 16.0", - "importlib-metadata >= 5.0.0; python=='3.7'" + "importlib-metadata >= 5.0.0; python_version=='3.7'" ] dynamic = [ "version", From 66cde8cc042659189ed81364047b9b0962df1a5c Mon Sep 17 00:00:00 2001 From: Diego Hurtado Date: Wed, 14 Dec 2022 18:10:54 -0600 Subject: [PATCH 26/29] Revert "Undo changes in shim" This reverts commit bd82b8feb2ab38678bd62fd6a9a53db88dc2737d. --- .../test_asyncio.py | 17 ++++++++++++++++- .../test_threads.py | 17 ++++++++++++++++- .../test_client_server/test_asyncio.py | 17 ++++++++++++++++- .../test_client_server/test_threads.py | 17 ++++++++++++++++- .../request_handler.py | 15 +++++++++++++++ .../test_asyncio.py | 17 ++++++++++++++++- .../test_threads.py | 17 ++++++++++++++++- .../test_late_span_finish/test_asyncio.py | 17 ++++++++++++++++- .../test_late_span_finish/test_threads.py | 17 ++++++++++++++++- .../test_listener_per_request/test_asyncio.py | 17 ++++++++++++++++- .../test_listener_per_request/test_threads.py | 17 ++++++++++++++++- .../test_multiple_callbacks/test_asyncio.py | 17 ++++++++++++++++- .../test_multiple_callbacks/test_threads.py | 17 ++++++++++++++++- .../test_nested_callbacks/test_asyncio.py | 17 ++++++++++++++++- .../test_nested_callbacks/test_threads.py | 19 +++++++++++++++++-- .../test_asyncio.py | 17 ++++++++++++++++- .../test_threads.py | 17 ++++++++++++++++- 17 files changed, 272 insertions(+), 17 deletions(-) diff --git a/shim/opentelemetry-opentracing-shim/tests/testbed/test_active_span_replacement/test_asyncio.py b/shim/opentelemetry-opentracing-shim/tests/testbed/test_active_span_replacement/test_asyncio.py index 131bb70b91b..0419ab44a22 100644 --- a/shim/opentelemetry-opentracing-shim/tests/testbed/test_active_span_replacement/test_asyncio.py +++ b/shim/opentelemetry-opentracing-shim/tests/testbed/test_active_span_replacement/test_asyncio.py @@ -1,12 +1,27 @@ +# Copyright The OpenTelemetry Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + import asyncio +# pylint: disable=import-error from ..otel_ot_shim_tracer import MockTracer from ..testcase import OpenTelemetryTestCase from ..utils import stop_loop_when class TestAsyncio(OpenTelemetryTestCase): - def setUp(self): + def setUp(self): # pylint: disable=invalid-name self.tracer = MockTracer() self.loop = asyncio.get_event_loop() diff --git a/shim/opentelemetry-opentracing-shim/tests/testbed/test_active_span_replacement/test_threads.py b/shim/opentelemetry-opentracing-shim/tests/testbed/test_active_span_replacement/test_threads.py index c8d490063ba..4e76c87a03e 100644 --- a/shim/opentelemetry-opentracing-shim/tests/testbed/test_active_span_replacement/test_threads.py +++ b/shim/opentelemetry-opentracing-shim/tests/testbed/test_active_span_replacement/test_threads.py @@ -1,11 +1,26 @@ +# Copyright The OpenTelemetry Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + from concurrent.futures import ThreadPoolExecutor +# pylint: disable=import-error from ..otel_ot_shim_tracer import MockTracer from ..testcase import OpenTelemetryTestCase class TestThreads(OpenTelemetryTestCase): - def setUp(self): + def setUp(self): # pylint: disable=invalid-name self.tracer = MockTracer() # use max_workers=3 as a general example even if only one would suffice self.executor = ThreadPoolExecutor(max_workers=3) diff --git a/shim/opentelemetry-opentracing-shim/tests/testbed/test_client_server/test_asyncio.py b/shim/opentelemetry-opentracing-shim/tests/testbed/test_client_server/test_asyncio.py index d76fffe3b38..adf99e76b23 100644 --- a/shim/opentelemetry-opentracing-shim/tests/testbed/test_client_server/test_asyncio.py +++ b/shim/opentelemetry-opentracing-shim/tests/testbed/test_client_server/test_asyncio.py @@ -1,8 +1,23 @@ +# Copyright The OpenTelemetry Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + import asyncio import opentracing from opentracing.ext import tags +# pylint: disable=import-error from ..otel_ot_shim_tracer import MockTracer from ..testcase import OpenTelemetryTestCase from ..utils import get_logger, get_one_by_tag, stop_loop_when @@ -50,7 +65,7 @@ async def send(self): class TestAsyncio(OpenTelemetryTestCase): - def setUp(self): + def setUp(self): # pylint: disable=invalid-name self.tracer = MockTracer() self.queue = asyncio.Queue() self.loop = asyncio.get_event_loop() diff --git a/shim/opentelemetry-opentracing-shim/tests/testbed/test_client_server/test_threads.py b/shim/opentelemetry-opentracing-shim/tests/testbed/test_client_server/test_threads.py index df382c34636..6fa5974d791 100644 --- a/shim/opentelemetry-opentracing-shim/tests/testbed/test_client_server/test_threads.py +++ b/shim/opentelemetry-opentracing-shim/tests/testbed/test_client_server/test_threads.py @@ -1,9 +1,24 @@ +# Copyright The OpenTelemetry Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + from queue import Queue from threading import Thread import opentracing from opentracing.ext import tags +# pylint: disable=import-error from ..otel_ot_shim_tracer import MockTracer from ..testcase import OpenTelemetryTestCase from ..utils import await_until, get_logger, get_one_by_tag @@ -52,7 +67,7 @@ def send(self): class TestThreads(OpenTelemetryTestCase): - def setUp(self): + def setUp(self): # pylint: disable=invalid-name self.tracer = MockTracer() self.queue = Queue() self.server = Server(tracer=self.tracer, queue=self.queue) diff --git a/shim/opentelemetry-opentracing-shim/tests/testbed/test_common_request_handler/request_handler.py b/shim/opentelemetry-opentracing-shim/tests/testbed/test_common_request_handler/request_handler.py index 22d59fbca64..b48a5dbc68b 100644 --- a/shim/opentelemetry-opentracing-shim/tests/testbed/test_common_request_handler/request_handler.py +++ b/shim/opentelemetry-opentracing-shim/tests/testbed/test_common_request_handler/request_handler.py @@ -1,5 +1,20 @@ +# Copyright The OpenTelemetry Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + from opentracing.ext import tags +# pylint: disable=import-error from ..utils import get_logger logger = get_logger(__name__) diff --git a/shim/opentelemetry-opentracing-shim/tests/testbed/test_common_request_handler/test_asyncio.py b/shim/opentelemetry-opentracing-shim/tests/testbed/test_common_request_handler/test_asyncio.py index 14958418a32..58970a223c3 100644 --- a/shim/opentelemetry-opentracing-shim/tests/testbed/test_common_request_handler/test_asyncio.py +++ b/shim/opentelemetry-opentracing-shim/tests/testbed/test_common_request_handler/test_asyncio.py @@ -1,7 +1,22 @@ +# Copyright The OpenTelemetry Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + import asyncio from opentracing.ext import tags +# pylint: disable=import-error from ..otel_ot_shim_tracer import MockTracer from ..testcase import OpenTelemetryTestCase from ..utils import get_logger, get_one_by_operation_name, stop_loop_when @@ -45,7 +60,7 @@ class TestAsyncio(OpenTelemetryTestCase): So one issue here is setting correct parent span. """ - def setUp(self): + def setUp(self): # pylint: disable=invalid-name self.tracer = MockTracer() self.loop = asyncio.get_event_loop() self.client = Client(RequestHandler(self.tracer), self.loop) diff --git a/shim/opentelemetry-opentracing-shim/tests/testbed/test_common_request_handler/test_threads.py b/shim/opentelemetry-opentracing-shim/tests/testbed/test_common_request_handler/test_threads.py index 6f5022ccc15..fdc0549d62f 100644 --- a/shim/opentelemetry-opentracing-shim/tests/testbed/test_common_request_handler/test_threads.py +++ b/shim/opentelemetry-opentracing-shim/tests/testbed/test_common_request_handler/test_threads.py @@ -1,7 +1,22 @@ +# Copyright The OpenTelemetry Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + from concurrent.futures import ThreadPoolExecutor from opentracing.ext import tags +# pylint: disable=import-error from ..otel_ot_shim_tracer import MockTracer from ..testcase import OpenTelemetryTestCase from ..utils import get_logger, get_one_by_operation_name @@ -45,7 +60,7 @@ class TestThreads(OpenTelemetryTestCase): activate span. So one issue here is setting correct parent span. """ - def setUp(self): + def setUp(self): # pylint: disable=invalid-name self.tracer = MockTracer() self.executor = ThreadPoolExecutor(max_workers=3) self.client = Client(RequestHandler(self.tracer), self.executor) diff --git a/shim/opentelemetry-opentracing-shim/tests/testbed/test_late_span_finish/test_asyncio.py b/shim/opentelemetry-opentracing-shim/tests/testbed/test_late_span_finish/test_asyncio.py index 86a47c6a737..d27e51ca88f 100644 --- a/shim/opentelemetry-opentracing-shim/tests/testbed/test_late_span_finish/test_asyncio.py +++ b/shim/opentelemetry-opentracing-shim/tests/testbed/test_late_span_finish/test_asyncio.py @@ -1,5 +1,20 @@ +# Copyright The OpenTelemetry Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + import asyncio +# pylint: disable=import-error from ..otel_ot_shim_tracer import MockTracer from ..testcase import OpenTelemetryTestCase from ..utils import get_logger, stop_loop_when @@ -8,7 +23,7 @@ class TestAsyncio(OpenTelemetryTestCase): - def setUp(self): + def setUp(self): # pylint: disable=invalid-name self.tracer = MockTracer() self.loop = asyncio.get_event_loop() diff --git a/shim/opentelemetry-opentracing-shim/tests/testbed/test_late_span_finish/test_threads.py b/shim/opentelemetry-opentracing-shim/tests/testbed/test_late_span_finish/test_threads.py index de8acb70bf3..2cd43d7e70b 100644 --- a/shim/opentelemetry-opentracing-shim/tests/testbed/test_late_span_finish/test_threads.py +++ b/shim/opentelemetry-opentracing-shim/tests/testbed/test_late_span_finish/test_threads.py @@ -1,12 +1,27 @@ +# Copyright The OpenTelemetry Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + import time from concurrent.futures import ThreadPoolExecutor +# pylint: disable=import-error from ..otel_ot_shim_tracer import MockTracer from ..testcase import OpenTelemetryTestCase class TestThreads(OpenTelemetryTestCase): - def setUp(self): + def setUp(self): # pylint: disable=invalid-name self.tracer = MockTracer() self.executor = ThreadPoolExecutor(max_workers=3) diff --git a/shim/opentelemetry-opentracing-shim/tests/testbed/test_listener_per_request/test_asyncio.py b/shim/opentelemetry-opentracing-shim/tests/testbed/test_listener_per_request/test_asyncio.py index 66999f04bf2..d0f0a6a577e 100644 --- a/shim/opentelemetry-opentracing-shim/tests/testbed/test_listener_per_request/test_asyncio.py +++ b/shim/opentelemetry-opentracing-shim/tests/testbed/test_listener_per_request/test_asyncio.py @@ -1,7 +1,22 @@ +# Copyright The OpenTelemetry Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + import asyncio from opentracing.ext import tags +# pylint: disable=import-error from ..otel_ot_shim_tracer import MockTracer from ..testcase import OpenTelemetryTestCase from ..utils import get_one_by_tag @@ -28,7 +43,7 @@ def send_sync(self, message): class TestAsyncio(OpenTelemetryTestCase): - def setUp(self): + def setUp(self): # pylint: disable=invalid-name self.tracer = MockTracer() self.loop = asyncio.get_event_loop() diff --git a/shim/opentelemetry-opentracing-shim/tests/testbed/test_listener_per_request/test_threads.py b/shim/opentelemetry-opentracing-shim/tests/testbed/test_listener_per_request/test_threads.py index b810db1f01c..39d0a3d1d4c 100644 --- a/shim/opentelemetry-opentracing-shim/tests/testbed/test_listener_per_request/test_threads.py +++ b/shim/opentelemetry-opentracing-shim/tests/testbed/test_listener_per_request/test_threads.py @@ -1,7 +1,22 @@ +# Copyright The OpenTelemetry Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + from concurrent.futures import ThreadPoolExecutor from opentracing.ext import tags +# pylint: disable=import-error from ..otel_ot_shim_tracer import MockTracer from ..testcase import OpenTelemetryTestCase from ..utils import get_one_by_tag @@ -28,7 +43,7 @@ def send_sync(self, message): class TestThreads(OpenTelemetryTestCase): - def setUp(self): + def setUp(self): # pylint: disable=invalid-name self.tracer = MockTracer() def test_main(self): diff --git a/shim/opentelemetry-opentracing-shim/tests/testbed/test_multiple_callbacks/test_asyncio.py b/shim/opentelemetry-opentracing-shim/tests/testbed/test_multiple_callbacks/test_asyncio.py index 3754367878c..bbfb620a840 100644 --- a/shim/opentelemetry-opentracing-shim/tests/testbed/test_multiple_callbacks/test_asyncio.py +++ b/shim/opentelemetry-opentracing-shim/tests/testbed/test_multiple_callbacks/test_asyncio.py @@ -1,6 +1,21 @@ +# Copyright The OpenTelemetry Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + import asyncio import random +# pylint: disable=import-error from ..otel_ot_shim_tracer import MockTracer from ..testcase import OpenTelemetryTestCase from ..utils import get_logger, stop_loop_when @@ -10,7 +25,7 @@ class TestAsyncio(OpenTelemetryTestCase): - def setUp(self): + def setUp(self): # pylint: disable=invalid-name self.tracer = MockTracer() self.loop = asyncio.get_event_loop() diff --git a/shim/opentelemetry-opentracing-shim/tests/testbed/test_multiple_callbacks/test_threads.py b/shim/opentelemetry-opentracing-shim/tests/testbed/test_multiple_callbacks/test_threads.py index dfb567c7454..6e8b405ccee 100644 --- a/shim/opentelemetry-opentracing-shim/tests/testbed/test_multiple_callbacks/test_threads.py +++ b/shim/opentelemetry-opentracing-shim/tests/testbed/test_multiple_callbacks/test_threads.py @@ -1,7 +1,22 @@ +# Copyright The OpenTelemetry Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + import random import time from concurrent.futures import ThreadPoolExecutor +# pylint: disable=import-error from ..otel_ot_shim_tracer import MockTracer from ..testcase import OpenTelemetryTestCase from ..utils import RefCount, get_logger @@ -11,7 +26,7 @@ class TestThreads(OpenTelemetryTestCase): - def setUp(self): + def setUp(self): # pylint: disable=invalid-name self.tracer = MockTracer() self.executor = ThreadPoolExecutor(max_workers=3) diff --git a/shim/opentelemetry-opentracing-shim/tests/testbed/test_nested_callbacks/test_asyncio.py b/shim/opentelemetry-opentracing-shim/tests/testbed/test_nested_callbacks/test_asyncio.py index b6b8277d38f..f00258624ca 100644 --- a/shim/opentelemetry-opentracing-shim/tests/testbed/test_nested_callbacks/test_asyncio.py +++ b/shim/opentelemetry-opentracing-shim/tests/testbed/test_nested_callbacks/test_asyncio.py @@ -1,12 +1,27 @@ +# Copyright The OpenTelemetry Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + import asyncio +# pylint: disable=import-error from ..otel_ot_shim_tracer import MockTracer from ..testcase import OpenTelemetryTestCase from ..utils import stop_loop_when class TestAsyncio(OpenTelemetryTestCase): - def setUp(self): + def setUp(self): # pylint: disable=invalid-name self.tracer = MockTracer() self.loop = asyncio.get_event_loop() diff --git a/shim/opentelemetry-opentracing-shim/tests/testbed/test_nested_callbacks/test_threads.py b/shim/opentelemetry-opentracing-shim/tests/testbed/test_nested_callbacks/test_threads.py index 1f86b2dfba3..955298537da 100644 --- a/shim/opentelemetry-opentracing-shim/tests/testbed/test_nested_callbacks/test_threads.py +++ b/shim/opentelemetry-opentracing-shim/tests/testbed/test_nested_callbacks/test_threads.py @@ -1,16 +1,31 @@ +# Copyright The OpenTelemetry Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + from concurrent.futures import ThreadPoolExecutor +# pylint: disable=import-error from ..otel_ot_shim_tracer import MockTracer from ..testcase import OpenTelemetryTestCase from ..utils import await_until class TestThreads(OpenTelemetryTestCase): - def setUp(self): + def setUp(self): # pylint: disable=invalid-name self.tracer = MockTracer() self.executor = ThreadPoolExecutor(max_workers=3) - def tearDown(self): + def tearDown(self): # pylint: disable=invalid-name self.executor.shutdown(False) def test_main(self): diff --git a/shim/opentelemetry-opentracing-shim/tests/testbed/test_subtask_span_propagation/test_asyncio.py b/shim/opentelemetry-opentracing-shim/tests/testbed/test_subtask_span_propagation/test_asyncio.py index bd08ee6d098..653f9bd810e 100644 --- a/shim/opentelemetry-opentracing-shim/tests/testbed/test_subtask_span_propagation/test_asyncio.py +++ b/shim/opentelemetry-opentracing-shim/tests/testbed/test_subtask_span_propagation/test_asyncio.py @@ -1,11 +1,26 @@ +# Copyright The OpenTelemetry Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + import asyncio +# pylint: disable=import-error from ..otel_ot_shim_tracer import MockTracer from ..testcase import OpenTelemetryTestCase class TestAsyncio(OpenTelemetryTestCase): - def setUp(self): + def setUp(self): # pylint: disable=invalid-name self.tracer = MockTracer() self.loop = asyncio.get_event_loop() diff --git a/shim/opentelemetry-opentracing-shim/tests/testbed/test_subtask_span_propagation/test_threads.py b/shim/opentelemetry-opentracing-shim/tests/testbed/test_subtask_span_propagation/test_threads.py index ea15aafb9a6..0d003c9062a 100644 --- a/shim/opentelemetry-opentracing-shim/tests/testbed/test_subtask_span_propagation/test_threads.py +++ b/shim/opentelemetry-opentracing-shim/tests/testbed/test_subtask_span_propagation/test_threads.py @@ -1,11 +1,26 @@ +# Copyright The OpenTelemetry Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + from concurrent.futures import ThreadPoolExecutor +# pylint: disable=import-error from ..otel_ot_shim_tracer import MockTracer from ..testcase import OpenTelemetryTestCase class TestThreads(OpenTelemetryTestCase): - def setUp(self): + def setUp(self): # pylint: disable=invalid-name self.tracer = MockTracer() self.executor = ThreadPoolExecutor(max_workers=3) From eb583bb49d949d47558cad6a0cb03ff5631e210c Mon Sep 17 00:00:00 2001 From: Diego Hurtado Date: Thu, 29 Dec 2022 15:10:16 -0600 Subject: [PATCH 27/29] Update dependencies --- exporter/opentelemetry-exporter-opencensus/pyproject.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/exporter/opentelemetry-exporter-opencensus/pyproject.toml b/exporter/opentelemetry-exporter-opencensus/pyproject.toml index fe03741dfb9..a1edefc70fe 100644 --- a/exporter/opentelemetry-exporter-opencensus/pyproject.toml +++ b/exporter/opentelemetry-exporter-opencensus/pyproject.toml @@ -28,8 +28,8 @@ classifiers = [ dependencies = [ "grpcio >= 1.0.0, < 2.0.0", "opencensus-proto >= 0.1.0, < 1.0.0", - "opentelemetry-api ~= 1.3", - "opentelemetry-sdk ~= 1.3", + "opentelemetry-api >= 1.15", + "opentelemetry-sdk >= 1.15", "protobuf ~= 3.13", "setuptools >= 16.0", ] From af6d00f9befc7ed134ece81d17db5b5fac5684f7 Mon Sep 17 00:00:00 2001 From: Diego Hurtado Date: Wed, 18 Jan 2023 12:54:16 -0600 Subject: [PATCH 28/29] Update dependency for opencensus exporter --- exporter/opentelemetry-exporter-opencensus/pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exporter/opentelemetry-exporter-opencensus/pyproject.toml b/exporter/opentelemetry-exporter-opencensus/pyproject.toml index a1edefc70fe..81d4546dd0c 100644 --- a/exporter/opentelemetry-exporter-opencensus/pyproject.toml +++ b/exporter/opentelemetry-exporter-opencensus/pyproject.toml @@ -28,7 +28,7 @@ classifiers = [ dependencies = [ "grpcio >= 1.0.0, < 2.0.0", "opencensus-proto >= 0.1.0, < 1.0.0", - "opentelemetry-api >= 1.15", + "opentelemetry-api >= 1.16.0.dev", "opentelemetry-sdk >= 1.15", "protobuf ~= 3.13", "setuptools >= 16.0", From 5efa728466dc68f88b06d880a53396b25c5c1c3c Mon Sep 17 00:00:00 2001 From: Diego Hurtado Date: Fri, 20 Jan 2023 19:01:17 -0600 Subject: [PATCH 29/29] Add descriptive error --- .../sdk/_configuration/__init__.py | 33 ++++++++++++------ opentelemetry-sdk/tests/test_configurator.py | 34 +++++++++++++++++++ 2 files changed, 57 insertions(+), 10 deletions(-) diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/_configuration/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/_configuration/__init__.py index 1c2272a90cd..a65ed85d84d 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/_configuration/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/_configuration/__init__.py @@ -94,18 +94,31 @@ def _import_config_components( component_implementations = [] for selected_component in selected_components: - component_implementations.append( - ( - selected_component, - next( - iter( - entry_points( - group=entry_point_name, name=selected_component + try: + component_implementations.append( + ( + selected_component, + next( + iter( + entry_points( + group=entry_point_name, name=selected_component + ) ) - ) - ).load(), + ).load(), + ) + ) + except KeyError: + + raise RuntimeError( + f"Requested entry point '{entry_point_name}' not found" + ) + + except StopIteration: + + raise RuntimeError( + f"Requested component '{selected_component}' not found in " + f"entry point '{entry_point_name}'" ) - ) return component_implementations diff --git a/opentelemetry-sdk/tests/test_configurator.py b/opentelemetry-sdk/tests/test_configurator.py index c17bd62c72c..e07c5e3c787 100644 --- a/opentelemetry-sdk/tests/test_configurator.py +++ b/opentelemetry-sdk/tests/test_configurator.py @@ -20,6 +20,8 @@ from unittest import TestCase from unittest.mock import patch +from pytest import raises + from opentelemetry import trace from opentelemetry.context import Context from opentelemetry.environment_variables import OTEL_PYTHON_ID_GENERATOR @@ -30,6 +32,7 @@ _get_exporter_names, _get_id_generator, _get_sampler, + _import_config_components, _import_exporters, _import_id_generator, _import_sampler, @@ -780,3 +783,34 @@ def test_console_exporters(self): metric_exporterts["console"].__class__, ConsoleMetricExporter.__class__, ) + + +class TestImportConfigComponents(TestCase): + @patch( + "opentelemetry.sdk._configuration.entry_points", + **{"side_effect": KeyError}, + ) + def test__import_config_components_missing_entry_point( + self, mock_entry_points + ): + + with raises(RuntimeError) as error: + _import_config_components(["a", "b", "c"], "name") + self.assertEqual( + str(error.value), "Requested entry point 'name' not found" + ) + + @patch( + "opentelemetry.sdk._configuration.entry_points", + **{"side_effect": StopIteration}, + ) + def test__import_config_components_missing_component( + self, mock_entry_points + ): + + with raises(RuntimeError) as error: + _import_config_components(["a", "b", "c"], "name") + self.assertEqual( + str(error.value), + "Requested component 'a' not found in entry point 'name'", + )