Skip to content

Commit 1c1f419

Browse files
committed
Fixed lint
1 parent 1bfe335 commit 1c1f419

File tree

2 files changed

+65
-25
lines changed

2 files changed

+65
-25
lines changed

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -170,11 +170,14 @@ def _format_request_objects_in_headers(attributes):
170170
for key, value_list in list(attributes.items()):
171171
new_values = []
172172
for value in value_list:
173-
if hasattr(value, '__class__'):
174-
if value.__class__.__name__ in ('HttpRequest', 'WSGIRequest'):
173+
if hasattr(value, "__class__"):
174+
if value.__class__.__name__ in (
175+
"HttpRequest",
176+
"WSGIRequest",
177+
):
175178
try:
176-
method = getattr(value, 'method', 'UNKNOWN')
177-
path = getattr(value, 'path', 'UNKNOWN')
179+
method = getattr(value, "method", "UNKNOWN")
180+
path = getattr(value, "path", "UNKNOWN")
178181
new_values.append(f"HttpRequest({method} {path})")
179182
except (AttributeError, ValueError, TypeError):
180183
new_values.append("HttpRequest(...)")
@@ -297,7 +300,11 @@ def process_request(self, request):
297300
wsgi_collect_custom_request_headers_attributes(carrier)
298301
)
299302
# Process custom attributes to handle WSGIRequest objects
300-
custom_attributes = self._format_request_objects_in_headers(custom_attributes)
303+
custom_attributes = (
304+
self._format_request_objects_in_headers(
305+
custom_attributes
306+
)
307+
)
301308

302309
if len(custom_attributes) > 0:
303310
span.set_attributes(custom_attributes)

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

Lines changed: 53 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@
2020
from unittest.mock import Mock, patch
2121

2222
from django import VERSION, conf
23+
from django.core.handlers.wsgi import WSGIRequest
2324
from django.http import HttpRequest, HttpResponse
2425
from django.test.client import Client
2526
from django.test.utils import setup_test_environment, teardown_test_environment
26-
from django.core.handlers.wsgi import WSGIRequest
2727

2828
from opentelemetry import trace
2929
from opentelemetry.instrumentation._semconv import (
@@ -1025,12 +1025,17 @@ def _format_request_objects_in_headers(attributes):
10251025
for key, value_list in list(attributes.items()):
10261026
new_values = []
10271027
for value in value_list:
1028-
if hasattr(value, '__class__'):
1029-
if value.__class__.__name__ in ('HttpRequest', 'WSGIRequest'):
1028+
if hasattr(value, "__class__"):
1029+
if value.__class__.__name__ in (
1030+
"HttpRequest",
1031+
"WSGIRequest",
1032+
):
10301033
try:
1031-
method = getattr(value, 'method', 'UNKNOWN')
1032-
path = getattr(value, 'path', 'UNKNOWN')
1033-
new_values.append(f"HttpRequest({method} {path})")
1034+
method = getattr(value, "method", "UNKNOWN")
1035+
request_path = getattr(value, "path", "UNKNOWN")
1036+
new_values.append(
1037+
f"HttpRequest({method} {request_path})"
1038+
)
10341039
except (AttributeError, ValueError, TypeError):
10351040
new_values.append("HttpRequest(...)")
10361041
else:
@@ -1046,23 +1051,39 @@ def test_wsgi_request_in_header_is_properly_formatted(self):
10461051
mock_wsgi_request.path = "/test/path"
10471052
mock_wsgi_request.__class__.__name__ = "WSGIRequest"
10481053

1049-
input_attributes = {"http.request.header.test_wsgirequest_header": [mock_wsgi_request]}
1050-
expected_attributes = {"http.request.header.test_wsgirequest_header": ["HttpRequest(GET /test/path)"]}
1054+
input_attributes = {
1055+
"http.request.header.test_wsgirequest_header": [mock_wsgi_request]
1056+
}
1057+
expected_attributes = {
1058+
"http.request.header.test_wsgirequest_header": [
1059+
"HttpRequest(GET /test/path)"
1060+
]
1061+
}
10511062

1052-
formatted_attributes = TestMiddlewareWsgiWithCustomHeaders._format_request_objects_in_headers(input_attributes)
1063+
formatted_attributes = TestMiddlewareWsgiWithCustomHeaders._format_request_objects_in_headers(
1064+
input_attributes
1065+
)
10531066

10541067
self.assertEqual(formatted_attributes, expected_attributes)
10551068

10561069
def test_wsgi_request_handles_extraction_error(self):
10571070
mock_wsgi_request = Mock(spec=WSGIRequest)
10581071
mock_wsgi_request.__class__.__name__ = "WSGIRequest"
10591072

1060-
type(mock_wsgi_request).method = property(lambda self: (_ for _ in ()).throw(ValueError("Test error")))
1073+
type(mock_wsgi_request).method = property(
1074+
lambda self: (_ for _ in ()).throw(ValueError("Test error"))
1075+
)
10611076

1062-
input_attributes = {"http.request.header.test_wsgirequest_header": [mock_wsgi_request]}
1063-
expected_attributes = {"http.request.header.test_wsgirequest_header": ["HttpRequest(...)"]}
1077+
input_attributes = {
1078+
"http.request.header.test_wsgirequest_header": [mock_wsgi_request]
1079+
}
1080+
expected_attributes = {
1081+
"http.request.header.test_wsgirequest_header": ["HttpRequest(...)"]
1082+
}
10641083

1065-
formatted_attributes = TestMiddlewareWsgiWithCustomHeaders._format_request_objects_in_headers(input_attributes)
1084+
formatted_attributes = TestMiddlewareWsgiWithCustomHeaders._format_request_objects_in_headers(
1085+
input_attributes
1086+
)
10661087

10671088
self.assertEqual(formatted_attributes, expected_attributes)
10681089

@@ -1072,10 +1093,18 @@ def test_handles_http_request_as_well(self):
10721093
mock_http_request.path = "/api/data"
10731094
mock_http_request.__class__.__name__ = "HttpRequest"
10741095

1075-
input_attributes = {"http.request.header.test_httprequest_header": [mock_http_request]}
1076-
expected_attributes = {"http.request.header.test_httprequest_header": ["HttpRequest(POST /api/data)"]}
1096+
input_attributes = {
1097+
"http.request.header.test_httprequest_header": [mock_http_request]
1098+
}
1099+
expected_attributes = {
1100+
"http.request.header.test_httprequest_header": [
1101+
"HttpRequest(POST /api/data)"
1102+
]
1103+
}
10771104

1078-
formatted_attributes = TestMiddlewareWsgiWithCustomHeaders._format_request_objects_in_headers(input_attributes)
1105+
formatted_attributes = TestMiddlewareWsgiWithCustomHeaders._format_request_objects_in_headers(
1106+
input_attributes
1107+
)
10791108

10801109
self.assertEqual(formatted_attributes, expected_attributes)
10811110

@@ -1087,14 +1116,18 @@ def test_regular_header_values_are_preserved(self):
10871116

10881117
input_attributes = {
10891118
"http.request.header.test_wsgirequest_header": [mock_wsgi_request],
1090-
"http.request.header.test_regular_header": ["regular-value"]
1119+
"http.request.header.test_regular_header": ["regular-value"],
10911120
}
10921121
expected_attributes = {
1093-
"http.request.header.test_wsgirequest_header": ["HttpRequest(GET /test/path)"],
1094-
"http.request.header.test_regular_header": ["regular-value"]
1122+
"http.request.header.test_wsgirequest_header": [
1123+
"HttpRequest(GET /test/path)"
1124+
],
1125+
"http.request.header.test_regular_header": ["regular-value"],
10951126
}
10961127

1097-
formatted_attributes = TestMiddlewareWsgiWithCustomHeaders._format_request_objects_in_headers(input_attributes)
1128+
formatted_attributes = TestMiddlewareWsgiWithCustomHeaders._format_request_objects_in_headers(
1129+
input_attributes
1130+
)
10981131

10991132
self.assertEqual(formatted_attributes, expected_attributes)
11001133

0 commit comments

Comments
 (0)