Skip to content

Commit 5b523af

Browse files
committed
Fix linting issues
1 parent c101ea9 commit 5b523af

File tree

4 files changed

+36
-26
lines changed

4 files changed

+36
-26
lines changed

instrumentation/opentelemetry-instrumentation-django/setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ install_requires =
4646

4747
[options.extras_require]
4848
asgi =
49-
opentelemetry-instrumentation-asgi == 0.22.dev0
49+
opentelemetry-instrumentation-asgi == 0.23.dev0
5050
test =
5151
opentelemetry-test == 0.24b0
5252

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

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

15+
import types
1516
from logging import getLogger
1617
from time import time
17-
import types
1818
from typing import Callable
1919

2020
from django import VERSION as django_version
@@ -25,11 +25,11 @@
2525
get_global_response_propagator,
2626
)
2727
from opentelemetry.instrumentation.utils import extract_attributes_from_object
28+
from opentelemetry.instrumentation.wsgi import add_response_attributes
2829
from opentelemetry.instrumentation.wsgi import (
29-
add_response_attributes,
3030
collect_request_attributes as wsgi_collect_request_attributes,
31-
wsgi_getter,
3231
)
32+
from opentelemetry.instrumentation.wsgi import wsgi_getter
3333
from opentelemetry.propagate import extract
3434
from opentelemetry.semconv.trace import SpanAttributes
3535
from opentelemetry.trace import Span, SpanKind, use_span
@@ -75,11 +75,11 @@ def __call__(self, request):
7575
ASGIRequest = None
7676

7777
try:
78+
from opentelemetry.instrumentation.asgi import asgi_getter
7879
from opentelemetry.instrumentation.asgi import (
79-
asgi_getter,
8080
collect_request_attributes as asgi_collect_request_attributes,
81-
set_status_code,
8281
)
82+
from opentelemetry.instrumentation.asgi import set_status_code
8383

8484
_is_asgi_supported = True
8585
except ImportError:
@@ -112,6 +112,10 @@ def __call__(self, request):
112112
]
113113

114114

115+
def _is_asgi_request(request: HttpRequest) -> bool:
116+
return bool(ASGIRequest and isinstance(request, ASGIRequest))
117+
118+
115119
class _DjangoMiddleware(MiddlewareMixin):
116120
"""Django Middleware for OpenTelemetry"""
117121

@@ -153,9 +157,6 @@ def _get_span_name(request):
153157
except Resolver404:
154158
return f"HTTP {request.method}"
155159

156-
def _is_asgi_request(self, request):
157-
return ASGIRequest and isinstance(request, ASGIRequest)
158-
159160
def process_request(self, request):
160161
# request.META is a dictionary containing all available HTTP headers
161162
# Read more about request.META here:
@@ -164,7 +165,7 @@ def process_request(self, request):
164165
if self._excluded_urls.url_disabled(request.build_absolute_uri("?")):
165166
return
166167

167-
is_asgi_request = self._is_asgi_request(request)
168+
is_asgi_request = _is_asgi_request(request)
168169
if is_asgi_request and not _is_asgi_supported:
169170
return
170171

@@ -202,13 +203,17 @@ def process_request(self, request):
202203
# contents, for the extract_attributes_from_object function to be able to retrieve
203204
# attributes from it.
204205
attributes = extract_attributes_from_object(
205-
types.SimpleNamespace(**{
206-
**request.__dict__,
206+
types.SimpleNamespace(
207207
**{
208-
name.decode('latin1'): value.decode('latin1')
209-
for name, value in request.scope.get('headers', [])
210-
},
211-
}),
208+
**request.__dict__,
209+
**{
210+
name.decode("latin1"): value.decode("latin1")
211+
for name, value in request.scope.get(
212+
"headers", []
213+
)
214+
},
215+
}
216+
),
212217
self._traced_request_attrs,
213218
attributes,
214219
)
@@ -262,9 +267,9 @@ def process_response(self, request, response):
262267
if self._excluded_urls.url_disabled(request.build_absolute_uri("?")):
263268
return response
264269

265-
is_asgi_request = self._is_asgi_request(request)
270+
is_asgi_request = _is_asgi_request(request)
266271
if is_asgi_request and not _is_asgi_supported:
267-
return
272+
return response
268273

269274
activation = request.META.pop(self._environ_activation_key, None)
270275
span = request.META.pop(self._environ_span_key, None)

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
from unittest.mock import Mock, patch
1717

1818
from django import VERSION, conf
19-
from django.conf.urls import url
2019
from django.http import HttpRequest, HttpResponse
2120
from django.test.client import Client
2221
from django.test.utils import setup_test_environment, teardown_test_environment
@@ -362,6 +361,7 @@ def test_trace_response_headers(self):
362361
class TestMiddlewareWithTracerProvider(TestBase, WsgiTestBase):
363362
@classmethod
364363
def setUpClass(cls):
364+
conf.settings.configure(ROOT_URLCONF=modules[__name__])
365365
super().setUpClass()
366366

367367
def setUp(self):
@@ -380,6 +380,11 @@ def tearDown(self):
380380
teardown_test_environment()
381381
_django_instrumentor.uninstrument()
382382

383+
@classmethod
384+
def tearDownClass(cls):
385+
super().tearDownClass()
386+
conf.settings = conf.LazySettings()
387+
383388
def test_tracer_provider_traced(self):
384389
Client().post("/traced/")
385390

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,12 @@
1515
from sys import modules
1616
from unittest.mock import Mock, patch
1717

18+
import pytest
1819
from django import VERSION, conf
19-
from django.conf.urls import url
2020
from django.http import HttpRequest, HttpResponse
2121
from django.test import SimpleTestCase
2222
from django.test.utils import setup_test_environment, teardown_test_environment
2323
from django.urls import re_path
24-
import pytest
2524

2625
from opentelemetry.instrumentation.django import (
2726
DjangoInstrumentor,
@@ -56,11 +55,6 @@
5655

5756
DJANGO_3_1 = VERSION >= (3, 1)
5857

59-
if DJANGO_3_1:
60-
from django.test.client import AsyncClient
61-
else:
62-
AsyncClient = None
63-
6458
urlpatterns = [
6559
re_path(r"^traced/", async_traced),
6660
re_path(r"^route/(?P<year>[0-9]{4})/template/$", async_traced_template),
@@ -351,6 +345,7 @@ async def test_trace_response_headers(self):
351345
class TestMiddlewareAsgiWithTracerProvider(SimpleTestCase, TestBase):
352346
@classmethod
353347
def setUpClass(cls):
348+
conf.settings.configure(ROOT_URLCONF=modules[__name__])
354349
super().setUpClass()
355350

356351
def setUp(self):
@@ -369,6 +364,11 @@ def tearDown(self):
369364
teardown_test_environment()
370365
_django_instrumentor.uninstrument()
371366

367+
@classmethod
368+
def tearDownClass(cls):
369+
super().tearDownClass()
370+
conf.settings = conf.LazySettings()
371+
372372
async def test_tracer_provider_traced(self):
373373
await self.async_client.post("/traced/")
374374

0 commit comments

Comments
 (0)