From 2e14fe1295445d0c3cb6b5a29cc8db8c7db9f03c Mon Sep 17 00:00:00 2001 From: Diego Hurtado Date: Wed, 1 Feb 2023 17:48:27 -0600 Subject: [PATCH] Remove usage of pkg_resources (#3047) * Remove usage of pkg_resources Fixes #2927 * WIP * WIP * Add test case for _load_providers * Fix test case for 3.9 * Fix lint * Fix 3.7 sdk tests * Fix lint * Fix lint * Fix mypy again * WIP * WIP * WIP * WIP * Move to module * Fix SDK * Fix mypy * Refactor load call * Fix mypy * Fix lint * Fix opencensus exporter * Refactor implementation * Add missing dependency * Undo changes in shim * Fix dependency * Revert "Undo changes in shim" This reverts commit bd82b8feb2ab38678bd62fd6a9a53db88dc2737d. * Update dependencies * Update dependency for opencensus exporter * Add descriptive error --- .../pyproject.toml | 4 +- .../opentelemetry/exporter/opencensus/util.py | 21 ++-- .../tests/encoder/test_v1_json.py | 5 +- .../tests/encoder/test_v2_json.py | 5 +- .../tests/encoder/test_v2_protobuf.py | 5 +- opentelemetry-api/pyproject.toml | 1 + .../src/opentelemetry/context/__init__.py | 26 +++-- .../metrics/_internal/__init__.py | 2 +- .../src/opentelemetry/propagate/__init__.py | 23 ++-- .../src/opentelemetry/trace/__init__.py | 2 +- .../opentelemetry/util/_importlib_metadata.py | 38 +++++++ .../src/opentelemetry/util/_providers.py | 30 ++--- opentelemetry-api/tests/__init__.py | 9 -- .../tests/propagators/test_propagators.py | 51 ++++++--- opentelemetry-api/tests/trace/test_status.py | 6 +- .../tests/util/test__importlib_metadata.py | 34 ++++++ .../tests/util/test__providers.py | 56 +++++++++ opentelemetry-api/tests/util/test_re.py | 10 +- opentelemetry-proto/tests/test_proto.py | 7 +- .../sdk/_configuration/__init__.py | 38 +++++-- .../sdk/error_handler/__init__.py | 10 +- .../opentelemetry/sdk/resources/__init__.py | 7 +- .../tests/error_handler/test_error_handler.py | 24 ++-- opentelemetry-sdk/tests/test_configurator.py | 106 ++++++++++++------ .../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 +- 43 files changed, 637 insertions(+), 181 deletions(-) create mode 100644 opentelemetry-api/src/opentelemetry/util/_importlib_metadata.py create mode 100644 opentelemetry-api/tests/util/test__importlib_metadata.py create mode 100644 opentelemetry-api/tests/util/test__providers.py diff --git a/exporter/opentelemetry-exporter-opencensus/pyproject.toml b/exporter/opentelemetry-exporter-opencensus/pyproject.toml index fe03741dfb..81d4546dd0 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.16.0.dev", + "opentelemetry-sdk >= 1.15", "protobuf ~= 3.13", "setuptools >= 16.0", ] 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 e08b884c3f..694e8dc6a1 100644 --- a/exporter/opentelemetry-exporter-opencensus/src/opentelemetry/exporter/opencensus/util.py +++ b/exporter/opentelemetry-exporter-opencensus/src/opentelemetry/exporter/opencensus/util.py @@ -12,11 +12,11 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os -import socket -import time +from os import getpid +from socket import gethostname +from time import time -import pkg_resources +# 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 @@ -25,10 +25,9 @@ __version__ as opencensusexporter_exporter_version, ) from opentelemetry.trace import SpanKind +from opentelemetry.util._importlib_metadata import version -OPENTELEMETRY_VERSION = pkg_resources.get_distribution( - "opentelemetry-api" -).version +OPENTELEMETRY_VERSION = version("opentelemetry-api") def proto_timestamp_from_time_ns(time_ns): @@ -88,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"), 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 59a750eb51..778ed74e8d 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 85cc91a0d9..37a0414fca 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 8ce61a92a1..2f2c894e4a 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/pyproject.toml b/opentelemetry-api/pyproject.toml index 50b2a8b8a0..e9490ab1d2 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_version=='3.7'" ] dynamic = [ "version", diff --git a/opentelemetry-api/src/opentelemetry/context/__init__.py b/opentelemetry-api/src/opentelemetry/context/__init__.py index 306f0937d5..3e85b64fe4 100644 --- a/opentelemetry-api/src/opentelemetry/context/__init__.py +++ b/opentelemetry-api/src/opentelemetry/context/__init__.py @@ -19,10 +19,10 @@ from os import environ from uuid import uuid4 -from pkg_resources import iter_entry_points - -from opentelemetry.context.context import Context, _RuntimeContext +# pylint: disable=wrong-import-position +from opentelemetry.context.context import Context, _RuntimeContext # noqa from opentelemetry.environment_variables import OTEL_PYTHON_CONTEXT +from opentelemetry.util._importlib_metadata import entry_points logger = logging.getLogger(__name__) _RUNTIME_CONTEXT = None # type: typing.Optional[_RuntimeContext] @@ -41,27 +41,33 @@ 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 + + _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.error( + logger.exception( "Failed to load context: %s", configured_context ) return func(*args, **kwargs) # type: ignore[misc] diff --git a/opentelemetry-api/src/opentelemetry/metrics/_internal/__init__.py b/opentelemetry-api/src/opentelemetry/metrics/_internal/__init__.py index ccde6900d8..630e9c4053 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 a39d8a44d1..56f217f282 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 pkg_resources import iter_entry_points +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._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. @@ -129,20 +128,30 @@ 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) + iter( # type: ignore + 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/src/opentelemetry/trace/__init__.py b/opentelemetry-api/src/opentelemetry/trace/__init__.py index f0e92f14bb..304df22754 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/_importlib_metadata.py b/opentelemetry-api/src/opentelemetry/util/_importlib_metadata.py new file mode 100644 index 0000000000..889c4cf1ac --- /dev/null +++ b/opentelemetry-api/src/opentelemetry/util/_importlib_metadata.py @@ -0,0 +1,38 @@ +# 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 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 + +__all__ = ["entry_points", "version"] diff --git a/opentelemetry-api/src/opentelemetry/util/_providers.py b/opentelemetry-api/src/opentelemetry/util/_providers.py index d8feb88d62..d255ac999f 100644 --- a/opentelemetry-api/src/opentelemetry/util/_providers.py +++ b/opentelemetry-api/src/opentelemetry/util/_providers.py @@ -16,7 +16,7 @@ from os import environ from typing import TYPE_CHECKING, TypeVar, cast -from pkg_resources import iter_entry_points +from opentelemetry.util._importlib_metadata import entry_points if TYPE_CHECKING: from opentelemetry.metrics import MeterProvider @@ -30,23 +30,25 @@ def _load_provider( provider_environment_variable: str, provider: str ) -> Provider: + try: - entry_point = next( - iter_entry_points( - f"opentelemetry_{provider}", - name=cast( - str, - environ.get( - provider_environment_variable, - f"default_{provider}", - ), - ), - ) + + provider_name = cast( + str, + environ.get(provider_environment_variable, f"default_{provider}"), ) + return cast( Provider, - entry_point.load()(), + next( # type: ignore + iter( # type: ignore + entry_points( # type: ignore + group=f"opentelemetry_{provider}", + name=provider_name, + ) + ) + ).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 bc48946761..b0a6f42841 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 a8fed620be..bb84bc4f1a 100644 --- a/opentelemetry-api/tests/propagators/test_propagators.py +++ b/opentelemetry-api/tests/propagators/test_propagators.py @@ -53,29 +53,46 @@ def test_propagators(propagators): @patch.dict(environ, {OTEL_PROPAGATORS: "a, b, c "}) @patch("opentelemetry.propagators.composite.CompositePropagator") - @patch("pkg_resources.iter_entry_points") + @patch("opentelemetry.util._importlib_metadata.entry_points") 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]}) - ] - } - ) - ] - ) - mock_iter_entry_points.configure_mock( - **{"side_effect": iter_entry_points_mock} + 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 74da78d6c7..6388ae9804 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__importlib_metadata.py b/opentelemetry-api/tests/util/test__importlib_metadata.py new file mode 100644 index 0000000000..7ca58881b8 --- /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 new file mode 100644 index 0000000000..f7b21ebacf --- /dev/null +++ b/opentelemetry-api/tests/util/test__providers.py @@ -0,0 +1,56 @@ +# 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 importlib import reload +from os import environ +from unittest import TestCase +from unittest.mock import Mock, patch + +from opentelemetry.util import _providers + + +class Test_Providers(TestCase): + @patch.dict( + environ, + { # type: ignore + "provider_environment_variable": "mock_provider_environment_variable" + }, + ) + @patch("opentelemetry.util._importlib_metadata.entry_points") + def test__providers(self, mock_entry_points): + + reload(_providers) + + 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", + ) diff --git a/opentelemetry-api/tests/util/test_re.py b/opentelemetry-api/tests/util/test_re.py index 99d79ba9d2..ea86f3e700 100644 --- a/opentelemetry-api/tests/util/test_re.py +++ b/opentelemetry-api/tests/util/test_re.py @@ -59,11 +59,13 @@ 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 +73,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 6551e4640f..9670be4627 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 a1ef3b76a2..a65ed85d84 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/_configuration/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/_configuration/__init__.py @@ -23,7 +23,6 @@ from os import environ 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 @@ -57,6 +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._importlib_metadata import entry_points _EXPORTER_OTLP = "otlp" _EXPORTER_OTLP_PROTO_GRPC = "otlp_proto_grpc" @@ -90,21 +90,37 @@ 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 = [] + + component_implementations = [] + for selected_component in selected_components: - entry_point = component_entry_points.get(selected_component, None) - if not entry_point: + try: + component_implementations.append( + ( + selected_component, + next( + iter( + entry_points( + group=entry_point_name, name=selected_component + ) + ) + ).load(), + ) + ) + except KeyError: + raise RuntimeError( - f"Requested component '{selected_component}' not found in entry points for '{entry_point_name}'" + f"Requested entry point '{entry_point_name}' not found" ) - component_impl = entry_point.load() - component_impls.append((selected_component, component_impl)) + except StopIteration: + + raise RuntimeError( + f"Requested component '{selected_component}' not found in " + f"entry point '{entry_point_name}'" + ) - 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 781f42e41a..7b21d92d2a 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/error_handler/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/error_handler/__init__.py @@ -62,7 +62,7 @@ def _handle(self, error: Exception, *args, **kwargs): from abc import ABC, abstractmethod from logging import getLogger -from pkg_resources import iter_entry_points +from opentelemetry.util._importlib_metadata import entry_points logger = getLogger(__name__) @@ -118,9 +118,11 @@ def __exit__(self, exc_type, exc_value, traceback): plugin_handled = False - for error_handler_entry_point in iter_entry_points( - "opentelemetry_error_handler" - ): + 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 56707415e5..c46b87f89c 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py @@ -64,14 +64,13 @@ from json import dumps from urllib import parse -import pkg_resources - from opentelemetry.attributes import BoundedAttributes from opentelemetry.sdk.environment_variables import ( OTEL_RESOURCE_ATTRIBUTES, OTEL_SERVICE_NAME, ) from opentelemetry.semconv.resource import ResourceAttributes +from opentelemetry.util._importlib_metadata import version from opentelemetry.util.types import AttributeValue LabelValue = AttributeValue @@ -136,9 +135,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 1712894464..116771dc9a 100644 --- a/opentelemetry-sdk/tests/error_handler/test_error_handler.py +++ b/opentelemetry-sdk/tests/error_handler/test_error_handler.py @@ -25,16 +25,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,7 +54,7 @@ class AssertionErrorHandler(ErrorHandler, AssertionError): **{"load.return_value": AssertionErrorHandler} ) - mock_iter_entry_points.configure_mock( + mock_entry_points.configure_mock( **{ "return_value": [ mock_entry_point_zero_division_error_handler, @@ -78,8 +78,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,7 +91,7 @@ def _handle(self, error: Exception): **{"load.return_value": ErrorErrorHandler} ) - mock_iter_entry_points.configure_mock( + mock_entry_points.configure_mock( **{"return_value": [mock_entry_point_error_error_handler]} ) @@ -102,10 +102,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,7 +116,7 @@ def __new__(cls): **{"load.return_value": MockErrorHandlerClass} ) - mock_iter_entry_points.configure_mock( + mock_entry_points.configure_mock( **{"return_value": [mock_entry_point_error_handler]} ) diff --git a/opentelemetry-sdk/tests/test_configurator.py b/opentelemetry-sdk/tests/test_configurator.py index a27c7a49a1..e07c5e3c78 100644 --- a/opentelemetry-sdk/tests/test_configurator.py +++ b/opentelemetry-sdk/tests/test_configurator.py @@ -14,12 +14,14 @@ # type: ignore # pylint: skip-file -import logging +from logging import getLogger from os import environ from typing import Dict, Iterable, Optional, Sequence 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, @@ -349,13 +352,14 @@ 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( + @patch("opentelemetry.sdk._configuration.entry_points") + def test_trace_init_custom_id_generator(self, mock_entry_points): + 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,10 +376,10 @@ 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( + def test_trace_init_custom_sampler_with_env(self, mock_entry_points): + mock_entry_points.configure_mock( return_value=[ IterEntryPoint( "custom_sampler_factory", @@ -383,18 +387,19 @@ def test_trace_init_custom_sampler_with_env(self, mock_iter_entry_points): ) ] ) + 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( + mock_entry_points.configure_mock( return_value=[ IterEntryPoint( "custom_sampler_factory", @@ -402,13 +407,14 @@ def test_trace_init_custom_sampler_with_env_bad_factory( ) ] ) + 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,9 +423,9 @@ 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( + mock_entry_points.configure_mock( return_value=[ IterEntryPoint( "custom_sampler_factory", @@ -427,13 +433,14 @@ def test_trace_init_custom_sampler_with_env_unused_arg( ) ] ) + 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,10 +448,8 @@ 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( + def test_trace_init_custom_ratio_sampler_with_env(self, mock_entry_points): + mock_entry_points.configure_mock( return_value=[ IterEntryPoint( "custom_ratio_sampler_factory", @@ -452,6 +457,7 @@ def test_trace_init_custom_ratio_sampler_with_env( ) ] ) + sampler_name = _get_sampler() sampler = _import_sampler(sampler_name) _init_tracing({}, sampler=sampler) @@ -459,7 +465,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,9 +474,9 @@ 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( + mock_entry_points.configure_mock( return_value=[ IterEntryPoint( "custom_ratio_sampler_factory", @@ -478,13 +484,14 @@ def test_trace_init_custom_ratio_sampler_with_env_bad_arg( ) ] ) + 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,9 +499,9 @@ 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( + mock_entry_points.configure_mock( return_value=[ IterEntryPoint( "custom_ratio_sampler_factory", @@ -502,13 +509,14 @@ def test_trace_init_custom_ratio_sampler_with_env_missing_arg( ) ] ) + 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 +525,17 @@ 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( + mock_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, - ), ] ) + sampler_name = _get_sampler() sampler = _import_sampler(sampler_name) _init_tracing({}, sampler=sampler) @@ -569,7 +570,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 @@ -605,7 +606,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( @@ -782,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'", + ) diff --git a/opentelemetry-semantic-conventions/tests/test_semconv.py b/opentelemetry-semantic-conventions/tests/test_semconv.py index f8e827145a..a7362a8af7 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 131bb70b91..0419ab44a2 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 c8d490063b..4e76c87a03 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 d76fffe3b3..adf99e76b2 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 df382c3463..6fa5974d79 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 22d59fbca6..b48a5dbc68 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 14958418a3..58970a223c 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 6f5022ccc1..fdc0549d62 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 86a47c6a73..d27e51ca88 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 de8acb70bf..2cd43d7e70 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 66999f04bf..d0f0a6a577 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 b810db1f01..39d0a3d1d4 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 3754367878..bbfb620a84 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 dfb567c745..6e8b405cce 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 b6b8277d38..f00258624c 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 1f86b2dfba..955298537d 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 bd08ee6d09..653f9bd810 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 ea15aafb9a..0d003c9062 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 147931eb6b..fd357987a6 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