Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
abhinav283 committed Jan 21, 2024
1 parent 5ea5517 commit 30fee67
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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


Expand Down
18 changes: 17 additions & 1 deletion opentelemetry-instrumentation/tests/test_dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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([]))

Expand Down

0 comments on commit 30fee67

Please sign in to comment.