Skip to content

Commit 75da304

Browse files
committed
Update tests
1 parent 1280063 commit 75da304

File tree

2 files changed

+57
-112
lines changed

2 files changed

+57
-112
lines changed

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

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import types
1615
import logging
16+
import types
1717
from logging import getLogger
1818
from time import time
1919
from timeit import default_timer
@@ -138,8 +138,10 @@ def filter(self, record: logging.LogRecord) -> bool:
138138
record.request = str(request)
139139
return True
140140

141-
_wsgi_logger = logging.getLogger("django.request")
142-
_wsgi_logger.addFilter(RequestFilter())
141+
142+
_wsgi_request_logger = logging.getLogger("django.request")
143+
_wsgi_request_logger.addFilter(RequestFilter())
144+
143145

144146
def _is_asgi_request(request: HttpRequest) -> bool:
145147
return ASGIRequest is not None and isinstance(request, ASGIRequest)
@@ -174,14 +176,6 @@ class _DjangoMiddleware(MiddlewareMixin):
174176
_otel_response_hook: Callable[[Span, HttpRequest, HttpResponse], None] = (
175177
None
176178
)
177-
178-
@staticmethod
179-
def format_request_objects_in_headers(attributes):
180-
for _, value_list in attributes.items():
181-
for index, value in enumerate(value_list):
182-
if isinstance(value, HttpRequest):
183-
value_list[index] = str(value)
184-
return attributes
185179

186180
@staticmethod
187181
def _get_span_name(request):
@@ -294,10 +288,6 @@ def process_request(self, request):
294288
custom_attributes = (
295289
wsgi_collect_custom_request_headers_attributes(carrier)
296290
)
297-
# Process custom attributes to handle WSGIRequest objects
298-
custom_attributes = self.format_request_objects_in_headers(
299-
custom_attributes
300-
)
301291

302292
if len(custom_attributes) > 0:
303293
span.set_attributes(custom_attributes)

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

Lines changed: 52 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@
1515
# pylint: disable=E0611
1616
# pylint: disable=too-many-lines
1717

18+
import logging
1819
from sys import modules
1920
from timeit import default_timer
21+
from unittest import TestCase
2022
from unittest.mock import Mock, patch
2123

2224
from django import VERSION, conf
23-
from django.core.handlers.wsgi import WSGIRequest
2425
from django.http import HttpRequest, HttpResponse
2526
from django.test.client import Client
2627
from django.test.utils import setup_test_environment, teardown_test_environment
@@ -36,6 +37,9 @@
3637
DjangoInstrumentor,
3738
_DjangoMiddleware,
3839
)
40+
from opentelemetry.instrumentation.django.middleware.otel_middleware import (
41+
RequestFilter,
42+
)
3943
from opentelemetry.instrumentation.propagators import (
4044
TraceResponsePropagator,
4145
set_global_response_propagator,
@@ -101,6 +105,53 @@ def path(path_argument, *args, **kwargs):
101105
_django_instrumentor = DjangoInstrumentor()
102106

103107

108+
# pylint: disable=too-many-public-methods
109+
class TestRequestFilter(TestCase):
110+
def test_converts_http_request_to_string(self):
111+
class DummyRequest:
112+
def __str__(self):
113+
return "<DummyRequest method=GET path=/example/>"
114+
115+
request = DummyRequest()
116+
117+
record = logging.LogRecord(
118+
name="django.request",
119+
level=logging.ERROR,
120+
pathname=__file__,
121+
lineno=0,
122+
msg="test message",
123+
args=(),
124+
exc_info=None,
125+
)
126+
record.request = request
127+
128+
expected_repr = str(request)
129+
130+
request_filter = RequestFilter()
131+
result = request_filter.filter(record)
132+
133+
self.assertTrue(result)
134+
self.assertEqual(record.request, expected_repr)
135+
self.assertIsInstance(record.request, str)
136+
137+
def test_handles_missing_request_attribute(self):
138+
record = logging.LogRecord(
139+
name="django.request",
140+
level=logging.INFO,
141+
pathname=__file__,
142+
lineno=0,
143+
msg="no request",
144+
args=(),
145+
exc_info=None,
146+
)
147+
148+
request_filter = RequestFilter()
149+
result = request_filter.filter(record)
150+
151+
self.assertTrue(result)
152+
self.assertEqual(record.request, "None")
153+
154+
104155
# pylint: disable=too-many-public-methods
105156
class TestMiddleware(WsgiTestBase):
106157
@classmethod
@@ -1020,102 +1071,6 @@ def tearDownClass(cls):
10201071
super().tearDownClass()
10211072
conf.settings = conf.LazySettings()
10221073

1023-
def test_wsgi_request_in_header_is_properly_formatted(self):
1024-
mock_wsgi_request = Mock(spec=WSGIRequest)
1025-
mock_wsgi_request.method = "GET"
1026-
mock_wsgi_request.path = "/test/path"
1027-
mock_wsgi_request.__class__.__name__ = "WSGIRequest"
1028-
1029-
input_attributes = {
1030-
"http.request.header.test_wsgirequest_header": [mock_wsgi_request]
1031-
}
1032-
expected_attributes = {
1033-
"http.request.header.test_wsgirequest_header": [
1034-
str(mock_wsgi_request)
1035-
]
1036-
}
1037-
1038-
formatted_attributes = (
1039-
_DjangoMiddleware.format_request_objects_in_headers(
1040-
input_attributes
1041-
)
1042-
)
1043-
1044-
self.assertEqual(formatted_attributes, expected_attributes)
1045-
1046-
def test_wsgi_request_handles_extraction_error(self):
1047-
mock_wsgi_request = Mock(spec=WSGIRequest)
1048-
mock_wsgi_request.__class__.__name__ = "WSGIRequest"
1049-
1050-
type(mock_wsgi_request).method = property(
1051-
lambda self: (_ for _ in ()).throw(ValueError("Test error"))
1052-
)
1053-
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-
str(mock_wsgi_request)
1060-
]
1061-
}
1062-
1063-
formatted_attributes = (
1064-
_DjangoMiddleware.format_request_objects_in_headers(
1065-
input_attributes
1066-
)
1067-
)
1068-
1069-
self.assertEqual(formatted_attributes, expected_attributes)
1070-
1071-
def test_handles_http_request_as_well(self):
1072-
mock_http_request = Mock(spec=HttpRequest)
1073-
mock_http_request.method = "POST"
1074-
mock_http_request.path = "/api/data"
1075-
mock_http_request.__class__.__name__ = "HttpRequest"
1076-
1077-
input_attributes = {
1078-
"http.request.header.test_httprequest_header": [mock_http_request]
1079-
}
1080-
expected_attributes = {
1081-
"http.request.header.test_httprequest_header": [
1082-
str(mock_http_request)
1083-
]
1084-
}
1085-
1086-
formatted_attributes = (
1087-
_DjangoMiddleware.format_request_objects_in_headers(
1088-
input_attributes
1089-
)
1090-
)
1091-
1092-
self.assertEqual(formatted_attributes, expected_attributes)
1093-
1094-
def test_regular_header_values_are_preserved(self):
1095-
mock_wsgi_request = Mock(spec=WSGIRequest)
1096-
mock_wsgi_request.method = "GET"
1097-
mock_wsgi_request.path = "/test/path"
1098-
mock_wsgi_request.__class__.__name__ = "WSGIRequest"
1099-
1100-
input_attributes = {
1101-
"http.request.header.test_wsgirequest_header": [mock_wsgi_request],
1102-
"http.request.header.test_regular_header": ["regular-value"],
1103-
}
1104-
expected_attributes = {
1105-
"http.request.header.test_wsgirequest_header": [
1106-
str(mock_wsgi_request)
1107-
],
1108-
"http.request.header.test_regular_header": ["regular-value"],
1109-
}
1110-
1111-
formatted_attributes = (
1112-
_DjangoMiddleware.format_request_objects_in_headers(
1113-
input_attributes
1114-
)
1115-
)
1116-
1117-
self.assertEqual(formatted_attributes, expected_attributes)
1118-
11191074
def test_http_custom_request_headers_in_span_attributes(self):
11201075
expected = {
11211076
"http.request.header.custom_test_header_1": (

0 commit comments

Comments
 (0)