Skip to content

Commit 52607a2

Browse files
committed
Addressed feedback
1 parent 1c1f419 commit 52607a2

File tree

3 files changed

+11
-38
lines changed

3 files changed

+11
-38
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2727
([#3673](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3673))
2828
- `opentelemetry-instrumentation-starlette`/`opentelemetry-instrumentation-fastapi`: Fixes a crash when host-based routing is used
2929
([#3507](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3507))
30-
- `opentelemetry-instrumentation-fastapi`: Fixes issue [#2808](https://github.com/open-telemetry/opentelemetry-python-contrib/issues/2808)
30+
- `opentelemetry-instrumentation-django`: Fixes invalid type at WSGI request headers and attributes collection.
3131
([#3731](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3731))
3232
- Fix documentation order of sections and headers for Django, Flask, MySQL, mysqlclient, psycopg, psycopg2, pymysql, sqlalchemy instrumentations.
3333
([#3719](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3719))

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
@@ -20,6 +20,7 @@
2020

2121
from django import VERSION as django_version
2222
from django.http import HttpRequest, HttpResponse
23+
from django.core.handlers.wsgi import WSGIRequest
2324

2425
from opentelemetry.context import detach
2526
from opentelemetry.instrumentation._semconv import (
@@ -166,19 +167,16 @@ class _DjangoMiddleware(MiddlewareMixin):
166167
)
167168

168169
@staticmethod
169-
def _format_request_objects_in_headers(attributes):
170+
def format_request_objects_in_headers(attributes):
170171
for key, value_list in list(attributes.items()):
171172
new_values = []
172173
for value in value_list:
173174
if hasattr(value, "__class__"):
174-
if value.__class__.__name__ in (
175-
"HttpRequest",
176-
"WSGIRequest",
177-
):
175+
if isinstance(value, (HttpRequest, WSGIRequest)):
178176
try:
179177
method = getattr(value, "method", "UNKNOWN")
180-
path = getattr(value, "path", "UNKNOWN")
181-
new_values.append(f"HttpRequest({method} {path})")
178+
request_path = getattr(value, "path", "UNKNOWN")
179+
new_values.append(f"HttpRequest({method} {request_path})")
182180
except (AttributeError, ValueError, TypeError):
183181
new_values.append("HttpRequest(...)")
184182
else:
@@ -301,7 +299,7 @@ def process_request(self, request):
301299
)
302300
# Process custom attributes to handle WSGIRequest objects
303301
custom_attributes = (
304-
self._format_request_objects_in_headers(
302+
self.format_request_objects_in_headers(
305303
custom_attributes
306304
)
307305
)

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)