Skip to content

Commit 54c5f11

Browse files
authored
fix python package distrubution name case-sensitive issue in client span code attr… (#546)
…ibutes ### Issue When Django ORM triggers a database query and generates corresponding OTel client span, the client span contains wrong code attributes: ``` "code.function.name": "django.db.backends.utils.CursorDebugWrapper._execute", "code.file.path": "/usr/local/lib/python3.10/site-packages/django/db/backends/utils.py", "db.query.text": "SELECT \"billing_service_billing\".\"id\", \"billing_service_billing\".\"owner_id\", \"billing_service_billing\".\"type\", \"billing_service_billing\".\"type_name\", \"billing_service_billing\".\"pet_id\", \"billing_service_billing\".\"payment\", \"billing_service_billing\".\"status\" FROM \"billing_service_billing\" WHERE NOT (\"billing_service_billing\".\"type_name\" IN (SELECT DISTINCT U0.\"invalid_name\" AS \"invalid_name\" FROM \"check_list\" U0 LIMIT 100)) LIMIT 3810", ``` Function name `django.db.backends.utils.CursorDebugWrapper._execute` and file path `/usr/local/lib/python3.10/site-packages/django/db/backends/utils.py` are from Django, not user code. ### Description of changes The root cause is django distribute name is `Django`(Distribution(name='Django', version='5.2.7')), does not match with the package name `django`. The bug can be fixed by using a case-insensitive string comparison. ### Testing After fix: ``` "attributes": { "code.function.name": "views.orm_example_view", "code.file.path": "/Volumes/workplace/extension/aws-otel-python-instrumentation/samples/django/views.py", "code.line.number": 278, "db.system": "sqlite", "db.name": "", "db.statement": "SELECT \"auth_user\".\"id\", \"auth_user\".\"password\", \"auth_user\".\"last_login\", \"auth_user\".\"is_superuser\", \"auth_user\".\"username\", \"auth_user\".\"first_name\", \"auth_user\".\"last_name\", \"auth_user\".\"email\", \"auth_user\".\"is_staff\", \"auth_user\".\"is_active\", \"auth_user\".\"date_joined\" FROM \"auth_user\" WHERE \"auth_user\".\"is_active\" LIMIT 10" }, ``` By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.
1 parent c398aed commit 54c5f11

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

aws-opentelemetry-distro/src/amazon/opentelemetry/distro/code_correlation/internal/packages_resolver.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,8 @@ def is_third_party_package(file_path: Path) -> bool:
364364
return False
365365

366366
third_party_packages = _load_third_party_packages()
367-
return distribution.name in third_party_packages
367+
# Perform case-insensitive comparison to handle packages like 'Django' vs 'django'
368+
return distribution.name.lower() in third_party_packages
368369

369370

370371
@lru_cache(maxsize=1024)

aws-opentelemetry-distro/tests/amazon/opentelemetry/distro/code_correlation/internal/test_packages_resolver.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,18 @@ def test_is_third_party_package_not_in_list(self, mock_load_packages, mock_resol
577577

578578
self.assertFalse(result)
579579

580+
@patch("amazon.opentelemetry.distro.code_correlation.internal.packages_resolver.resolve_package_from_filename")
581+
@patch("amazon.opentelemetry.distro.code_correlation.internal.packages_resolver._load_third_party_packages")
582+
def test_is_third_party_package_case_insensitive_match(self, mock_load_packages, mock_resolve):
583+
"""Test case-insensitive detection of third-party package."""
584+
# Test with uppercase package name in distribution but lowercase in third-party list
585+
mock_resolve.return_value = Distribution(name="Django", version="4.2.0")
586+
mock_load_packages.return_value = {"django", "requests", "urllib3"}
587+
588+
result = is_third_party_package(Path("/path/to/Django/__init__.py"))
589+
590+
self.assertTrue(result)
591+
580592

581593
class TestIsUserCode(TestCase):
582594
"""Test the is_user_code function."""

0 commit comments

Comments
 (0)