From 0ff85a6b67f2a6858bc97facbef54e159e741217 Mon Sep 17 00:00:00 2001 From: Abhinav Kunal Date: Fri, 12 Jan 2024 01:11:34 +0000 Subject: [PATCH] Fixes #1970 --- .../instrumentation/bootstrap.py | 45 ++++++++++++++----- .../tests/test_dependencies.py | 18 +++++++- 2 files changed, 52 insertions(+), 11 deletions(-) diff --git a/opentelemetry-instrumentation/src/opentelemetry/instrumentation/bootstrap.py b/opentelemetry-instrumentation/src/opentelemetry/instrumentation/bootstrap.py index 6fa36f0463..cf2da1f22b 100644 --- a/opentelemetry-instrumentation/src/opentelemetry/instrumentation/bootstrap.py +++ b/opentelemetry-instrumentation/src/opentelemetry/instrumentation/bootstrap.py @@ -19,7 +19,8 @@ import subprocess import sys -import pkg_resources +from importlib_metadata import PackageNotFoundError, distribution +from packaging import version from opentelemetry.instrumentation.bootstrap_gen import ( default_instrumentations, @@ -83,21 +84,45 @@ def _pip_check(): raise RuntimeError(f"Dependency conflict found: {pip_check}") +# def _is_installed(req): +# if req in sys.modules: +# return True + +# try: +# dist = distribution(req).version +# except PackageNotFoundError: +# return False +# except pkg_resources.VersionConflict as exc: +# logger.warning( +# "instrumentation for package %s is available but version %s is installed. Skipping.", +# exc.req, +# exc.dist.as_requirement(), # pylint: disable=no-member +# ) +# return False +# return True + + def _is_installed(req): if req in sys.modules: return True try: - pkg_resources.get_distribution(req) - except pkg_resources.DistributionNotFound: - return False - except pkg_resources.VersionConflict as exc: - logger.warning( - "instrumentation for package %s is available but version %s is installed. Skipping.", - exc.req, - exc.dist.as_requirement(), # pylint: disable=no-member - ) + dist = distribution(req) + # Assuming 'req' is in format 'package==version' + # Modify this as per the format of your 'req' string + required_version = req.split("==")[1] if "==" in req else None + if required_version and version.parse(dist.version) != version.parse( + required_version + ): + logger.warning( + "Instrumentation for package %s is available but version %s is installed. Skipping.", + req, + dist.version, + ) + return False + except PackageNotFoundError: return False + return True diff --git a/opentelemetry-instrumentation/tests/test_dependencies.py b/opentelemetry-instrumentation/tests/test_dependencies.py index 04bcf476ea..6d77586dad 100644 --- a/opentelemetry-instrumentation/tests/test_dependencies.py +++ b/opentelemetry-instrumentation/tests/test_dependencies.py @@ -14,7 +14,8 @@ # pylint: disable=protected-access -import pkg_resources +import importlib_metadata +from packaging import version import pytest from opentelemetry.instrumentation.dependencies import ( @@ -26,6 +27,21 @@ class TestDependencyConflicts(TestBase): + def _check_version(self, package_spec): + package_name, _, package_version = package_spec.partition('==') + package_name = package_name.strip() + package_version = package_version.strip() + + try: + installed_distribution = importlib_metadata.distribution(package_name) + except importlib_metadata.PackageNotFoundError: + return None + + installed_version = version.parse(installed_distribution.version) + if package_version and installed_version != version.parse(package_version): + return f'{package_name} {installed_distribution.version}' + return package_name + def test_get_dependency_conflicts_empty(self): self.assertIsNone(get_dependency_conflicts([]))