Skip to content

Commit 566c3c9

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

File tree

4 files changed

+32
-19
lines changed

4 files changed

+32
-19
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: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,14 @@
1919

2020

2121
class TestAwsAuthSession(TestCase):
22-
@patch("pkg_resources.get_distribution", side_effect=ImportError("test error"))
23-
@patch.dict("sys.modules", {"botocore": None}, clear=False)
22+
@patch("amazon.opentelemetry.distro._utils.version")
23+
@patch.dict("sys.modules", {"botocore": None})
2424
@patch("requests.Session.request", return_value=requests.Response())
25-
def test_aws_auth_session_no_botocore(self, _, __):
25+
def test_aws_auth_session_no_botocore(self, mock_request, mock_version):
2626
"""Tests that aws_auth_session will not inject SigV4 Headers if botocore is not installed."""
27+
from importlib.metadata import PackageNotFoundError
28+
29+
mock_version.side_effect = PackageNotFoundError("botocore")
2730

2831
session = AwsAuthSession("us-east-1", "xray")
2932
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 & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
from unittest.mock import MagicMock, patch
1010

1111
import gevent.monkey
12-
import pkg_resources
1312
from botocore.response import StreamingBody
1413

1514
from amazon.opentelemetry.distro.patches._instrumentation_patch import (
@@ -38,7 +37,7 @@
3837
_LAMBDA_SOURCE_MAPPING_ID: str = "lambdaEventSourceMappingID"
3938

4039
# Patch names
41-
GET_DISTRIBUTION_PATCH: str = "amazon.opentelemetry.distro._utils.pkg_resources.get_distribution"
40+
GET_DISTRIBUTION_PATCH: str = "amazon.opentelemetry.distro._utils.version"
4241

4342

4443
class TestInstrumentationPatch(TestCase):
@@ -73,7 +72,7 @@ def test_instrumentation_patch(self):
7372

7473
def _run_patch_behaviour_tests(self):
7574
# Test setup
76-
self.method_patches[GET_DISTRIBUTION_PATCH].return_value = "CorrectDistributionObject"
75+
self.method_patches[GET_DISTRIBUTION_PATCH].return_value = "1.0.0"
7776
# Test setup to not patch gevent
7877
os.environ[AWS_GEVENT_PATCH_MODULES] = "none"
7978

@@ -359,16 +358,14 @@ def _test_botocore_installed_flag(self):
359358
"amazon.opentelemetry.distro.patches._botocore_patches._apply_botocore_instrumentation_patches"
360359
) as mock_apply_patches:
361360
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()
361+
from importlib.metadata import PackageNotFoundError
365362

366-
get_distribution_patch.side_effect = pkg_resources.VersionConflict("botocore==1.0.0", "botocore==0.0.1")
363+
get_distribution_patch.side_effect = PackageNotFoundError
367364
apply_instrumentation_patches()
368365
mock_apply_patches.assert_not_called()
369366

370367
get_distribution_patch.side_effect = None
371-
get_distribution_patch.return_value = "CorrectDistributionObject"
368+
get_distribution_patch.return_value = "1.0.0"
372369
apply_instrumentation_patches()
373370
mock_apply_patches.assert_called()
374371

0 commit comments

Comments
 (0)