Skip to content

Commit 4c02feb

Browse files
committed
remove pkg_resources and use alternative to align with upstream otel >= 1.28.0
1 parent 9f6a181 commit 4c02feb

File tree

4 files changed

+31
-20
lines changed

4 files changed

+31
-20
lines changed

aws-opentelemetry-distro/src/amazon/opentelemetry/distro/_utils.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33

44
import os
55
import sys
6+
from importlib.metadata import PackageNotFoundError, version
67
from logging import Logger, getLogger
78

8-
import pkg_resources
9+
from packaging.requirements import Requirement
910

1011
_logger: Logger = getLogger(__name__)
1112

@@ -18,10 +19,23 @@ def is_installed(req: str) -> bool:
1819
if req in sys.modules and sys.modules[req] is not None:
1920
return True
2021

22+
return _is_installed(req)
23+
24+
25+
def _is_installed(req):
26+
req = Requirement(req)
27+
2128
try:
22-
pkg_resources.get_distribution(req)
23-
except Exception as exc: # pylint: disable=broad-except
24-
_logger.debug("Skipping instrumentation patch: package %s, exception: %s", req, exc)
29+
dist_version = version(req.name)
30+
except PackageNotFoundError:
31+
return False
32+
33+
if not req.specifier.filter(dist_version):
34+
_logger.warning(
35+
"instrumentation for package %s is available but version %s is installed. Skipping.",
36+
req,
37+
dist_version,
38+
)
2539
return False
2640
return True
2741

aws-opentelemetry-distro/tests/amazon/opentelemetry/distro/test_aws_auth_session.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
# SPDX-License-Identifier: Apache-2.0
3+
from importlib.metadata import PackageNotFoundError
34
from unittest import TestCase
45
from unittest.mock import patch
56

@@ -19,11 +20,12 @@
1920

2021

2122
class TestAwsAuthSession(TestCase):
22-
@patch("pkg_resources.get_distribution", side_effect=ImportError("test error"))
23-
@patch.dict("sys.modules", {"botocore": None}, clear=False)
23+
@patch("amazon.opentelemetry.distro._utils.version")
24+
@patch.dict("sys.modules", {"botocore": None})
2425
@patch("requests.Session.request", return_value=requests.Response())
25-
def test_aws_auth_session_no_botocore(self, _, __):
26+
def test_aws_auth_session_no_botocore(self, mock_request, mock_version):
2627
"""Tests that aws_auth_session will not inject SigV4 Headers if botocore is not installed."""
28+
mock_version.side_effect = PackageNotFoundError("botocore")
2729

2830
session = AwsAuthSession("us-east-1", "xray")
2931
actual_headers = {"test": "test"}
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
# SPDX-License-Identifier: Apache-2.0
3+
from importlib.metadata import PackageNotFoundError, version
34
from unittest import TestCase
45

5-
from pkg_resources import DistributionNotFound, require
6-
76

87
class TestAwsOpenTelemetryDistro(TestCase):
98
def test_package_available(self):
109
try:
11-
require(["aws-opentelemetry-distro"])
12-
except DistributionNotFound:
10+
version("aws-opentelemetry-distro")
11+
except PackageNotFoundError:
1312
self.fail("aws-opentelemetry-distro not installed")

aws-opentelemetry-distro/tests/amazon/opentelemetry/distro/test_instrumentation_patch.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
from unittest.mock import MagicMock, patch
1010

1111
import gevent.monkey
12-
import pkg_resources
1312
from botocore.response import StreamingBody
13+
from importlib.metadata import PackageNotFoundError
1414

1515
from amazon.opentelemetry.distro.patches._instrumentation_patch import (
1616
AWS_GEVENT_PATCH_MODULES,
@@ -38,7 +38,7 @@
3838
_LAMBDA_SOURCE_MAPPING_ID: str = "lambdaEventSourceMappingID"
3939

4040
# Patch names
41-
GET_DISTRIBUTION_PATCH: str = "amazon.opentelemetry.distro._utils.pkg_resources.get_distribution"
41+
GET_DISTRIBUTION_PATCH: str = "amazon.opentelemetry.distro._utils.version"
4242

4343

4444
class TestInstrumentationPatch(TestCase):
@@ -73,7 +73,7 @@ def test_instrumentation_patch(self):
7373

7474
def _run_patch_behaviour_tests(self):
7575
# Test setup
76-
self.method_patches[GET_DISTRIBUTION_PATCH].return_value = "CorrectDistributionObject"
76+
self.method_patches[GET_DISTRIBUTION_PATCH].return_value = "1.0.0"
7777
# Test setup to not patch gevent
7878
os.environ[AWS_GEVENT_PATCH_MODULES] = "none"
7979

@@ -359,16 +359,12 @@ def _test_botocore_installed_flag(self):
359359
"amazon.opentelemetry.distro.patches._botocore_patches._apply_botocore_instrumentation_patches"
360360
) as mock_apply_patches:
361361
get_distribution_patch: patch = self.method_patches[GET_DISTRIBUTION_PATCH]
362-
get_distribution_patch.side_effect = pkg_resources.DistributionNotFound
363-
apply_instrumentation_patches()
364-
mock_apply_patches.assert_not_called()
365-
366-
get_distribution_patch.side_effect = pkg_resources.VersionConflict("botocore==1.0.0", "botocore==0.0.1")
362+
get_distribution_patch.side_effect = PackageNotFoundError
367363
apply_instrumentation_patches()
368364
mock_apply_patches.assert_not_called()
369365

370366
get_distribution_patch.side_effect = None
371-
get_distribution_patch.return_value = "CorrectDistributionObject"
367+
get_distribution_patch.return_value = "1.0.0"
372368
apply_instrumentation_patches()
373369
mock_apply_patches.assert_called()
374370

0 commit comments

Comments
 (0)