Skip to content

Commit 44c9eeb

Browse files
committed
Addressed feedback
1 parent 3ae4a58 commit 44c9eeb

File tree

3 files changed

+12
-37
lines changed

3 files changed

+12
-37
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
8787
([#3673](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3673))
8888
- `opentelemetry-instrumentation-starlette`/`opentelemetry-instrumentation-fastapi`: Fixes a crash when host-based routing is used
8989
([#3507](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3507))
90+
- `opentelemetry-instrumentation-django`: Fixes invalid type at WSGI request headers and attributes collection.
91+
([#3731](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3731))
9092
- Fix documentation order of sections and headers for Django, Flask, MySQL, mysqlclient, psycopg, psycopg2, pymysql, sqlalchemy instrumentations.
9193
([#3719](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3719))
9294
- `opentelemetry-instrumentation-asgi` Fixed an issue where FastAPI reports IP instead of URL.

instrumentation/opentelemetry-instrumentation-django/src/opentelemetry/instrumentation/django/middleware/otel_middleware.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
from django import VERSION as django_version
2323
from django.http import HttpRequest, HttpResponse
24+
from django.core.handlers.wsgi import WSGIRequest
2425

2526
from opentelemetry.context import detach
2627
from opentelemetry.instrumentation._semconv import (
@@ -176,19 +177,16 @@ class _DjangoMiddleware(MiddlewareMixin):
176177
)
177178

178179
@staticmethod
179-
def _format_request_objects_in_headers(attributes):
180+
def format_request_objects_in_headers(attributes):
180181
for key, value_list in list(attributes.items()):
181182
new_values = []
182183
for value in value_list:
183184
if hasattr(value, "__class__"):
184-
if value.__class__.__name__ in (
185-
"HttpRequest",
186-
"WSGIRequest",
187-
):
185+
if isinstance(value, (HttpRequest, WSGIRequest)):
188186
try:
189187
method = getattr(value, "method", "UNKNOWN")
190-
path = getattr(value, "path", "UNKNOWN")
191-
new_values.append(f"HttpRequest({method} {path})")
188+
request_path = getattr(value, "path", "UNKNOWN")
189+
new_values.append(f"HttpRequest({method} {request_path})")
192190
except (AttributeError, ValueError, TypeError):
193191
new_values.append("HttpRequest(...)")
194192
else:
@@ -311,7 +309,7 @@ def process_request(self, request):
311309
)
312310
# Process custom attributes to handle WSGIRequest objects
313311
custom_attributes = (
314-
self._format_request_objects_in_headers(
312+
self.format_request_objects_in_headers(
315313
custom_attributes
316314
)
317315
)

instrumentation/opentelemetry-instrumentation-django/tests/test_middleware.py

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,31 +1020,6 @@ def tearDownClass(cls):
10201020
super().tearDownClass()
10211021
conf.settings = conf.LazySettings()
10221022

1023-
@staticmethod
1024-
def _format_request_objects_in_headers(attributes):
1025-
for key, value_list in list(attributes.items()):
1026-
new_values = []
1027-
for value in value_list:
1028-
if hasattr(value, "__class__"):
1029-
if value.__class__.__name__ in (
1030-
"HttpRequest",
1031-
"WSGIRequest",
1032-
):
1033-
try:
1034-
method = getattr(value, "method", "UNKNOWN")
1035-
request_path = getattr(value, "path", "UNKNOWN")
1036-
new_values.append(
1037-
f"HttpRequest({method} {request_path})"
1038-
)
1039-
except (AttributeError, ValueError, TypeError):
1040-
new_values.append("HttpRequest(...)")
1041-
else:
1042-
new_values.append(value)
1043-
else:
1044-
new_values.append(value)
1045-
attributes[key] = new_values
1046-
return attributes
1047-
10481023
def test_wsgi_request_in_header_is_properly_formatted(self):
10491024
mock_wsgi_request = Mock(spec=WSGIRequest)
10501025
mock_wsgi_request.method = "GET"
@@ -1060,7 +1035,7 @@ def test_wsgi_request_in_header_is_properly_formatted(self):
10601035
]
10611036
}
10621037

1063-
formatted_attributes = TestMiddlewareWsgiWithCustomHeaders._format_request_objects_in_headers(
1038+
formatted_attributes =_DjangoMiddleware.format_request_objects_in_headers(
10641039
input_attributes
10651040
)
10661041

@@ -1081,7 +1056,7 @@ def test_wsgi_request_handles_extraction_error(self):
10811056
"http.request.header.test_wsgirequest_header": ["HttpRequest(...)"]
10821057
}
10831058

1084-
formatted_attributes = TestMiddlewareWsgiWithCustomHeaders._format_request_objects_in_headers(
1059+
formatted_attributes = _DjangoMiddleware.format_request_objects_in_headers(
10851060
input_attributes
10861061
)
10871062

@@ -1102,7 +1077,7 @@ def test_handles_http_request_as_well(self):
11021077
]
11031078
}
11041079

1105-
formatted_attributes = TestMiddlewareWsgiWithCustomHeaders._format_request_objects_in_headers(
1080+
formatted_attributes = _DjangoMiddleware.format_request_objects_in_headers(
11061081
input_attributes
11071082
)
11081083

@@ -1125,7 +1100,7 @@ def test_regular_header_values_are_preserved(self):
11251100
"http.request.header.test_regular_header": ["regular-value"],
11261101
}
11271102

1128-
formatted_attributes = TestMiddlewareWsgiWithCustomHeaders._format_request_objects_in_headers(
1103+
formatted_attributes = _DjangoMiddleware.format_request_objects_in_headers(
11291104
input_attributes
11301105
)
11311106

0 commit comments

Comments
 (0)