Skip to content

Commit 1d248bd

Browse files
authored
chore(django): remove django.process_exception event/handler (#14293)
We can remove the need for `with ctx.span` and calling an additional `django.process_exception` event by adding support for `should_set_traceback` to the `_finish_span` trace handler. This will help to further remove span references/usage in the django integration. ## Checklist - [x] PR author has checked that all the criteria below are met - The PR description includes an overview of the change - The PR description articulates the motivation for the change - The change includes tests OR the PR description describes a testing strategy - The PR description notes risks associated with the change, if any - Newly-added code is easy to change - The change follows the [library release note guidelines](https://ddtrace.readthedocs.io/en/stable/releasenotes.html) - The change includes or references documentation updates if necessary - Backport labels are set (if [applicable](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting)) ## Reviewer Checklist - [ ] Reviewer has checked that all the criteria below are met - Title is accurate - All changes are related to the pull request's stated goal - Avoids breaking [API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces) changes - Testing strategy adequately addresses listed risks - Newly-added code is easy to change - Release note makes sense to a user of the library - If necessary, author has acknowledged and discussed the performance implications of this PR as reported in the benchmarks PR comment - Backport labels are set in a manner that is consistent with the [release branch maintenance policy](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting)
1 parent 7306059 commit 1d248bd

File tree

2 files changed

+7
-11
lines changed

2 files changed

+7
-11
lines changed

ddtrace/_trace/trace_handlers.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,8 @@ def _finish_span(
161161
exc_type, exc_value, exc_traceback = exc_info
162162
if exc_type and exc_value and exc_traceback:
163163
span.set_exc_info(exc_type, exc_value, exc_traceback)
164+
elif ctx.get_item("should_set_traceback", False):
165+
span.set_traceback()
164166
span.finish()
165167

166168

@@ -546,11 +548,6 @@ def _on_django_func_wrapped(_unused1, _unused2, _unused3, ctx, ignored_excs):
546548
ctx.span._ignore_exception(exc)
547549

548550

549-
def _on_django_process_exception(ctx: core.ExecutionContext, should_set_traceback: bool):
550-
if should_set_traceback:
551-
ctx.span.set_traceback()
552-
553-
554551
def _on_django_block_request(ctx: core.ExecutionContext, metadata: Dict[str, str], django_config, url: str, query: str):
555552
for tk, tv in metadata.items():
556553
ctx.span.set_tag_str(tk, tv)
@@ -890,7 +887,6 @@ def listen():
890887
core.on("django.start_response", _on_django_start_response)
891888
core.on("django.cache", _on_django_cache)
892889
core.on("django.func.wrapped", _on_django_func_wrapped)
893-
core.on("django.process_exception", _on_django_process_exception)
894890
core.on("django.block_request_callback", _on_django_block_request)
895891
core.on("django.after_request_headers.post", _on_django_after_request_headers_post)
896892
core.on("botocore.patched_api_call.exception", _on_botocore_patched_api_call_exception)
@@ -989,7 +985,7 @@ def listen():
989985
):
990986
core.on(f"context.started.{context_name}", _start_span)
991987

992-
for name in ("django.template.render",):
988+
for name in ("django.template.render", "django.process_exception"):
993989
core.on(f"context.ended.{name}", _finish_span)
994990

995991

ddtrace/contrib/internal/django/patch.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -360,11 +360,11 @@ def wrapped(django, pin, func, instance, args, kwargs):
360360
tags = {COMPONENT: config_django.integration_name}
361361
with core.context_with_data(
362362
"django.process_exception", span_name=name, resource=resource, tags=tags, pin=pin
363-
) as ctx, ctx.span:
363+
) as ctx:
364364
resp = func(*args, **kwargs)
365-
core.dispatch(
366-
"django.process_exception", (ctx, hasattr(resp, "status_code") and 500 <= resp.status_code < 600)
367-
)
365+
366+
# Tell finish span that we should collect the traceback
367+
ctx.set_item("should_set_traceback", hasattr(resp, "status_code") and 500 <= resp.status_code < 600)
368368
return resp
369369

370370
return trace_utils.with_traced_module(wrapped)(django)

0 commit comments

Comments
 (0)