20
20
from unittest .mock import Mock , patch
21
21
22
22
from django import VERSION , conf
23
+ from django .core .handlers .wsgi import WSGIRequest
23
24
from django .http import HttpRequest , HttpResponse
24
25
from django .test .client import Client
25
26
from django .test .utils import setup_test_environment , teardown_test_environment
26
- from django .core .handlers .wsgi import WSGIRequest
27
27
28
28
from opentelemetry import trace
29
29
from opentelemetry .instrumentation ._semconv import (
@@ -1025,12 +1025,17 @@ def _format_request_objects_in_headers(attributes):
1025
1025
for key , value_list in list (attributes .items ()):
1026
1026
new_values = []
1027
1027
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
+ ):
1030
1033
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
+ )
1034
1039
except (AttributeError , ValueError , TypeError ):
1035
1040
new_values .append ("HttpRequest(...)" )
1036
1041
else :
@@ -1046,23 +1051,39 @@ def test_wsgi_request_in_header_is_properly_formatted(self):
1046
1051
mock_wsgi_request .path = "/test/path"
1047
1052
mock_wsgi_request .__class__ .__name__ = "WSGIRequest"
1048
1053
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
+ }
1051
1062
1052
- formatted_attributes = TestMiddlewareWsgiWithCustomHeaders ._format_request_objects_in_headers (input_attributes )
1063
+ formatted_attributes = TestMiddlewareWsgiWithCustomHeaders ._format_request_objects_in_headers (
1064
+ input_attributes
1065
+ )
1053
1066
1054
1067
self .assertEqual (formatted_attributes , expected_attributes )
1055
1068
1056
1069
def test_wsgi_request_handles_extraction_error (self ):
1057
1070
mock_wsgi_request = Mock (spec = WSGIRequest )
1058
1071
mock_wsgi_request .__class__ .__name__ = "WSGIRequest"
1059
1072
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
+ )
1061
1076
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
+ }
1064
1083
1065
- formatted_attributes = TestMiddlewareWsgiWithCustomHeaders ._format_request_objects_in_headers (input_attributes )
1084
+ formatted_attributes = TestMiddlewareWsgiWithCustomHeaders ._format_request_objects_in_headers (
1085
+ input_attributes
1086
+ )
1066
1087
1067
1088
self .assertEqual (formatted_attributes , expected_attributes )
1068
1089
@@ -1072,10 +1093,18 @@ def test_handles_http_request_as_well(self):
1072
1093
mock_http_request .path = "/api/data"
1073
1094
mock_http_request .__class__ .__name__ = "HttpRequest"
1074
1095
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
+ }
1077
1104
1078
- formatted_attributes = TestMiddlewareWsgiWithCustomHeaders ._format_request_objects_in_headers (input_attributes )
1105
+ formatted_attributes = TestMiddlewareWsgiWithCustomHeaders ._format_request_objects_in_headers (
1106
+ input_attributes
1107
+ )
1079
1108
1080
1109
self .assertEqual (formatted_attributes , expected_attributes )
1081
1110
@@ -1087,14 +1116,18 @@ def test_regular_header_values_are_preserved(self):
1087
1116
1088
1117
input_attributes = {
1089
1118
"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" ],
1091
1120
}
1092
1121
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" ],
1095
1126
}
1096
1127
1097
- formatted_attributes = TestMiddlewareWsgiWithCustomHeaders ._format_request_objects_in_headers (input_attributes )
1128
+ formatted_attributes = TestMiddlewareWsgiWithCustomHeaders ._format_request_objects_in_headers (
1129
+ input_attributes
1130
+ )
1098
1131
1099
1132
self .assertEqual (formatted_attributes , expected_attributes )
1100
1133
0 commit comments