Skip to content

Commit 565bd8e

Browse files
committed
remove spurious activation check
1 parent 054665b commit 565bd8e

File tree

1 file changed

+58
-59
lines changed
  • instrumentation/opentelemetry-instrumentation-django/src/opentelemetry/instrumentation/django/middleware

1 file changed

+58
-59
lines changed

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

Lines changed: 58 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -343,74 +343,73 @@ def process_response(self, request, response):
343343
)
344344
request_start_time = request.META.pop(self._environ_timer_key, None)
345345

346-
if activation and span:
347-
if is_asgi_request:
348-
set_status_code(
349-
span,
350-
response.status_code,
351-
metric_attributes=duration_attrs,
352-
sem_conv_opt_in_mode=self._sem_conv_opt_in_mode,
353-
)
346+
if is_asgi_request:
347+
set_status_code(
348+
span,
349+
response.status_code,
350+
metric_attributes=duration_attrs,
351+
sem_conv_opt_in_mode=self._sem_conv_opt_in_mode,
352+
)
354353

355-
if span.is_recording() and span.kind == SpanKind.SERVER:
356-
custom_headers = {}
357-
for key, value in response.items():
358-
asgi_setter.set(custom_headers, key, value)
354+
if span.is_recording() and span.kind == SpanKind.SERVER:
355+
custom_headers = {}
356+
for key, value in response.items():
357+
asgi_setter.set(custom_headers, key, value)
359358

360-
custom_res_attributes = asgi_collect_custom_headers_attributes(
361-
custom_headers,
362-
SanitizeValue(
363-
get_custom_headers(
364-
OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SANITIZE_FIELDS
365-
)
366-
),
359+
custom_res_attributes = asgi_collect_custom_headers_attributes(
360+
custom_headers,
361+
SanitizeValue(
367362
get_custom_headers(
368-
OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_RESPONSE
369-
),
370-
normalise_response_header_name,
371-
)
372-
for key, value in custom_res_attributes.items():
373-
span.set_attribute(key, value)
374-
else:
375-
add_response_attributes(
376-
span,
377-
f"{response.status_code} {response.reason_phrase}",
378-
response.items(),
379-
duration_attrs=duration_attrs,
380-
sem_conv_opt_in_mode=self._sem_conv_opt_in_mode,
381-
)
382-
if span.is_recording() and span.kind == SpanKind.SERVER:
383-
custom_attributes = (
384-
wsgi_collect_custom_response_headers_attributes(
385-
response.items()
363+
OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SANITIZE_FIELDS
386364
)
365+
),
366+
get_custom_headers(
367+
OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_RESPONSE
368+
),
369+
normalise_response_header_name,
370+
)
371+
for key, value in custom_res_attributes.items():
372+
span.set_attribute(key, value)
373+
else:
374+
add_response_attributes(
375+
span,
376+
f"{response.status_code} {response.reason_phrase}",
377+
response.items(),
378+
duration_attrs=duration_attrs,
379+
sem_conv_opt_in_mode=self._sem_conv_opt_in_mode,
380+
)
381+
if span.is_recording() and span.kind == SpanKind.SERVER:
382+
custom_attributes = (
383+
wsgi_collect_custom_response_headers_attributes(
384+
response.items()
387385
)
388-
if len(custom_attributes) > 0:
389-
span.set_attributes(custom_attributes)
386+
)
387+
if len(custom_attributes) > 0:
388+
span.set_attributes(custom_attributes)
390389

391-
propagator = get_global_response_propagator()
392-
if propagator:
393-
propagator.inject(response)
390+
propagator = get_global_response_propagator()
391+
if propagator:
392+
propagator.inject(response)
394393

395-
# record any exceptions raised while processing the request
396-
exception = request.META.pop(self._environ_exception_key, None)
394+
# record any exceptions raised while processing the request
395+
exception = request.META.pop(self._environ_exception_key, None)
397396

398-
if _DjangoMiddleware._otel_response_hook:
399-
try:
400-
_DjangoMiddleware._otel_response_hook( # pylint: disable=not-callable
401-
span, request, response
402-
)
403-
except Exception: # pylint: disable=broad-exception-caught
404-
_logger.exception("Exception raised by response_hook")
405-
406-
if exception:
407-
activation.__exit__(
408-
type(exception),
409-
exception,
410-
getattr(exception, "__traceback__", None),
397+
if _DjangoMiddleware._otel_response_hook:
398+
try:
399+
_DjangoMiddleware._otel_response_hook( # pylint: disable=not-callable
400+
span, request, response
411401
)
412-
else:
413-
activation.__exit__(None, None, None)
402+
except Exception: # pylint: disable=broad-exception-caught
403+
_logger.exception("Exception raised by response_hook")
404+
405+
if exception:
406+
activation.__exit__(
407+
type(exception),
408+
exception,
409+
getattr(exception, "__traceback__", None),
410+
)
411+
else:
412+
activation.__exit__(None, None, None)
414413

415414
if request_start_time is not None:
416415
duration_s = default_timer() - request_start_time

0 commit comments

Comments
 (0)