Skip to content

Commit 646d41f

Browse files
committed
Fix linting issues
1 parent 0efcc73 commit 646d41f

File tree

4 files changed

+39
-27
lines changed

4 files changed

+39
-27
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.23.dev0
5252

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

Lines changed: 25 additions & 18 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
@@ -26,11 +26,11 @@
2626
get_global_response_propagator,
2727
)
2828
from opentelemetry.instrumentation.utils import extract_attributes_from_object
29+
from opentelemetry.instrumentation.wsgi import add_response_attributes
2930
from opentelemetry.instrumentation.wsgi import (
30-
add_response_attributes,
3131
collect_request_attributes as wsgi_collect_request_attributes,
32-
wsgi_getter,
3332
)
33+
from opentelemetry.instrumentation.wsgi import wsgi_getter
3434
from opentelemetry.propagate import extract
3535
from opentelemetry.semconv.trace import SpanAttributes
3636
from opentelemetry.trace import Span, SpanKind, use_span
@@ -76,11 +76,11 @@ def __call__(self, request):
7676
ASGIRequest = None
7777

7878
try:
79+
from opentelemetry.instrumentation.asgi import asgi_getter
7980
from opentelemetry.instrumentation.asgi import (
80-
asgi_getter,
8181
collect_request_attributes as asgi_collect_request_attributes,
82-
set_status_code,
8382
)
83+
from opentelemetry.instrumentation.asgi import set_status_code
8484

8585
_is_asgi_supported = True
8686
except ImportError:
@@ -113,6 +113,10 @@ def __call__(self, request):
113113
]
114114

115115

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

@@ -154,9 +158,6 @@ def _get_span_name(request):
154158
except Resolver404:
155159
return "HTTP {}".format(request.method)
156160

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

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

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

266-
is_asgi_request = self._is_asgi_request(request)
271+
is_asgi_request = _is_asgi_request(request)
267272
if is_asgi_request and not _is_asgi_supported:
268-
return
273+
return response
269274

270275
activation = request.META.pop(self._environ_activation_key, None)
271276
span = request.META.pop(self._environ_span_key, None)
@@ -276,7 +281,9 @@ def process_response(self, request, response):
276281
else:
277282
add_response_attributes(
278283
span,
279-
"{} {}".format(response.status_code, response.reason_phrase),
284+
"{} {}".format(
285+
response.status_code, response.reason_phrase
286+
),
280287
response,
281288
)
282289

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
@@ -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

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)