Skip to content

Commit d7956d5

Browse files
committed
Fix linting issues
1 parent 1750103 commit d7956d5

File tree

3 files changed

+38
-26
lines changed

3 files changed

+38
-26
lines changed

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

+25-18
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.http import HttpRequest, HttpResponse
@@ -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
@@ -54,11 +54,11 @@
5454
ASGIRequest = None
5555

5656
try:
57+
from opentelemetry.instrumentation.asgi import asgi_getter
5758
from opentelemetry.instrumentation.asgi import (
58-
asgi_getter,
5959
collect_request_attributes as asgi_collect_request_attributes,
60-
set_status_code,
6160
)
61+
from opentelemetry.instrumentation.asgi import set_status_code
6262

6363
_is_asgi_supported = True
6464
except ImportError:
@@ -91,6 +91,10 @@
9191
]
9292

9393

94+
def _is_asgi_request(request):
95+
return ASGIRequest and isinstance(request, ASGIRequest)
96+
97+
9498
class _DjangoMiddleware(MiddlewareMixin):
9599
"""Django Middleware for OpenTelemetry"""
96100

@@ -132,9 +136,6 @@ def _get_span_name(request):
132136
except Resolver404:
133137
return "HTTP {}".format(request.method)
134138

135-
def _is_asgi_request(self, request):
136-
return ASGIRequest and isinstance(request, ASGIRequest)
137-
138139
def process_request(self, request):
139140
# request.META is a dictionary containing all available HTTP headers
140141
# Read more about request.META here:
@@ -143,7 +144,7 @@ def process_request(self, request):
143144
if self._excluded_urls.url_disabled(request.build_absolute_uri("?")):
144145
return
145146

146-
is_asgi_request = self._is_asgi_request(request)
147+
is_asgi_request = _is_asgi_request(request)
147148
if is_asgi_request and not _is_asgi_supported:
148149
return
149150

@@ -181,13 +182,17 @@ def process_request(self, request):
181182
# contents, for the extract_attributes_from_object function to be able to retrieve
182183
# attributes from it.
183184
attributes = extract_attributes_from_object(
184-
types.SimpleNamespace(**{
185-
**request.__dict__,
185+
types.SimpleNamespace(
186186
**{
187-
name.decode('latin1'): value.decode('latin1')
188-
for name, value in request.scope.get('headers', [])
189-
},
190-
}),
187+
**request.__dict__,
188+
**{
189+
name.decode("latin1"): value.decode("latin1")
190+
for name, value in request.scope.get(
191+
"headers", []
192+
)
193+
},
194+
}
195+
),
191196
self._traced_request_attrs,
192197
attributes,
193198
)
@@ -241,9 +246,9 @@ def process_response(self, request, response):
241246
if self._excluded_urls.url_disabled(request.build_absolute_uri("?")):
242247
return response
243248

244-
is_asgi_request = self._is_asgi_request(request)
249+
is_asgi_request = _is_asgi_request(request)
245250
if is_asgi_request and not _is_asgi_supported:
246-
return
251+
return response
247252

248253
activation = request.META.pop(self._environ_activation_key, None)
249254
span = request.META.pop(self._environ_span_key, None)
@@ -254,7 +259,9 @@ def process_response(self, request, response):
254259
else:
255260
add_response_attributes(
256261
span,
257-
"{} {}".format(response.status_code, response.reason_phrase),
262+
"{} {}".format(
263+
response.status_code, response.reason_phrase
264+
),
258265
response,
259266
)
260267

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

+6-1
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
@@ -365,6 +364,7 @@ def test_trace_response_headers(self):
365364
class TestMiddlewareWithTracerProvider(TestBase, WsgiTestBase):
366365
@classmethod
367366
def setUpClass(cls):
367+
conf.settings.configure(ROOT_URLCONF=modules[__name__])
368368
super().setUpClass()
369369

370370
def setUp(self):
@@ -383,6 +383,11 @@ def tearDown(self):
383383
teardown_test_environment()
384384
_django_instrumentor.uninstrument()
385385

386+
@classmethod
387+
def tearDownClass(cls):
388+
super().tearDownClass()
389+
conf.settings = conf.LazySettings()
390+
386391
def test_tracer_provider_traced(self):
387392
Client().post("/traced/")
388393

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

+7-7
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)