Skip to content

Commit d3f416f

Browse files
committed
feat: Add queue.submit span when a Django task is enqueued
1 parent 3eac23c commit d3f416f

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

sentry_sdk/consts.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -936,6 +936,7 @@ class OP:
936936
QUEUE_SUBMIT_RAY = "queue.submit.ray"
937937
QUEUE_TASK_RAY = "queue.task.ray"
938938
QUEUE_TASK_DRAMATIQ = "queue.task.dramatiq"
939+
QUEUE_SUBMIT_DJANGO = "queue.submit.django"
939940
SUBPROCESS = "subprocess"
940941
SUBPROCESS_WAIT = "subprocess.wait"
941942
SUBPROCESS_COMMUNICATE = "subprocess.communicate"

sentry_sdk/integrations/django/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
)
6363
from sentry_sdk.integrations.django.middleware import patch_django_middlewares
6464
from sentry_sdk.integrations.django.signals_handlers import patch_signals
65+
from sentry_sdk.integrations.django.tasks import patch_tasks
6566
from sentry_sdk.integrations.django.views import patch_views
6667

6768
if DJANGO_VERSION[:2] > (1, 8):
@@ -271,6 +272,7 @@ def _django_queryset_repr(value, hint):
271272
patch_views()
272273
patch_templates()
273274
patch_signals()
275+
patch_tasks()
274276
add_template_context_repr_sequence()
275277

276278
if patch_caching is not None:
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
from functools import wraps
2+
3+
import sentry_sdk
4+
from sentry_sdk.consts import OP
5+
6+
try:
7+
# django.tasks were added in Django 6.0
8+
from django.tasks.base import Task
9+
except ImportError:
10+
Task = None
11+
12+
13+
def patch_tasks():
14+
# type: () -> None
15+
if Task is None:
16+
return
17+
18+
old_task_enqueue = Task.enqueue
19+
20+
@wraps(old_task_enqueue)
21+
def _sentry_enqueue(self, *args, **kwargs):
22+
# type: (Any, Any) -> Any
23+
from sentry_sdk.integrations.django import DjangoIntegration
24+
25+
integration = sentry_sdk.get_client().get_integration(DjangoIntegration)
26+
if integration is None:
27+
return old_task_enqueue(*args, **kwargs)
28+
29+
name = (
30+
getattr(self.func, "__name__", repr(self.func)) or "<unknown Django task>"
31+
)
32+
33+
with sentry_sdk.start_span(
34+
op=OP.QUEUE_SUBMIT_DJANGO, name=name, origin=DjangoIntegration.origin
35+
):
36+
return old_task_enqueue(*args, **kwargs)
37+
38+
Task.enqueue = _sentry_enqueue

0 commit comments

Comments
 (0)