forked from getsentry/sentry-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_tasks.py
More file actions
306 lines (250 loc) · 8.61 KB
/
Copy pathtest_tasks.py
File metadata and controls
306 lines (250 loc) · 8.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
import pytest
import sentry_sdk
from sentry_sdk.consts import OP
from sentry_sdk.integrations.django import DjangoIntegration
try:
from django.tasks import task
HAS_DJANGO_TASKS = True
except ImportError:
HAS_DJANGO_TASKS = False
@pytest.fixture
def immediate_backend(settings):
"""Configure Django to use the immediate task backend for synchronous testing."""
settings.TASKS = {
"default": {"BACKEND": "django.tasks.backends.immediate.ImmediateBackend"}
}
if HAS_DJANGO_TASKS:
@task
def simple_task():
return "result"
@task
def add_numbers(a, b):
return a + b
@task
def greet(name, greeting="Hello"):
return f"{greeting}, {name}!"
@task
def failing_task():
raise ValueError("Task failed!")
@task
def task_one():
return 1
@task
def task_two():
return 2
@pytest.mark.skipif(
not HAS_DJANGO_TASKS,
reason="Django tasks are only available in Django 6.0+",
)
@pytest.mark.parametrize("span_streaming", [True, False])
def test_task_span_is_created(
sentry_init,
capture_events,
capture_items,
immediate_backend,
span_streaming,
):
"""Test that the queue.submit.django span is created when a task is enqueued."""
sentry_init(
integrations=[DjangoIntegration()],
traces_sample_rate=1.0,
_experiments={"trace_lifecycle": "stream" if span_streaming else "static"},
)
if span_streaming:
items = capture_items("span")
with sentry_sdk.traces.start_span(name="custom parent"):
simple_task.enqueue()
sentry_sdk.flush()
spans = [item.payload for item in items]
queue_submit_spans = [
span
for span in spans
if span["attributes"].get("sentry.op") == OP.QUEUE_SUBMIT_DJANGO
]
assert len(queue_submit_spans) == 1
assert (
queue_submit_spans[0]["name"]
== "tests.integrations.django.test_tasks.simple_task"
)
assert (
queue_submit_spans[0]["attributes"]["sentry.origin"] == "auto.http.django"
)
else:
events = capture_events()
with sentry_sdk.start_transaction(name="test_transaction"):
simple_task.enqueue()
(event,) = events
assert event["type"] == "transaction"
queue_submit_spans = [
span for span in event["spans"] if span["op"] == OP.QUEUE_SUBMIT_DJANGO
]
assert len(queue_submit_spans) == 1
assert (
queue_submit_spans[0]["description"]
== "tests.integrations.django.test_tasks.simple_task"
)
assert queue_submit_spans[0]["origin"] == "auto.http.django"
@pytest.mark.skipif(
not HAS_DJANGO_TASKS,
reason="Django tasks are only available in Django 6.0+",
)
def test_task_enqueue_returns_result(sentry_init, immediate_backend):
"""Test that the task enqueuing behavior is unchanged from the user perspective."""
sentry_init(
integrations=[DjangoIntegration()],
traces_sample_rate=1.0,
)
result = add_numbers.enqueue(3, 5)
assert result is not None
assert result.return_value == 8
@pytest.mark.skipif(
not HAS_DJANGO_TASKS,
reason="Django tasks are only available in Django 6.0+",
)
@pytest.mark.parametrize("span_streaming", [True, False])
def test_task_enqueue_with_kwargs(
sentry_init,
immediate_backend,
capture_events,
capture_items,
span_streaming,
):
"""Test that task enqueuing works correctly with keyword arguments."""
sentry_init(
integrations=[DjangoIntegration()],
traces_sample_rate=1.0,
_experiments={"trace_lifecycle": "stream" if span_streaming else "static"},
)
if span_streaming:
items = capture_items("span")
with sentry_sdk.traces.start_span(name="custom parent"):
result = greet.enqueue(name="World", greeting="Hi")
assert result.return_value == "Hi, World!"
sentry_sdk.flush()
spans = [item.payload for item in items]
queue_submit_spans = [
span
for span in spans
if span["attributes"].get("sentry.op") == OP.QUEUE_SUBMIT_DJANGO
]
assert len(queue_submit_spans) == 1
assert (
queue_submit_spans[0]["name"]
== "tests.integrations.django.test_tasks.greet"
)
else:
events = capture_events()
with sentry_sdk.start_transaction(name="test_transaction"):
result = greet.enqueue(name="World", greeting="Hi")
assert result.return_value == "Hi, World!"
(event,) = events
queue_submit_spans = [
span for span in event["spans"] if span["op"] == OP.QUEUE_SUBMIT_DJANGO
]
assert len(queue_submit_spans) == 1
assert (
queue_submit_spans[0]["description"]
== "tests.integrations.django.test_tasks.greet"
)
@pytest.mark.skipif(
not HAS_DJANGO_TASKS,
reason="Django tasks are only available in Django 6.0+",
)
@pytest.mark.parametrize("span_streaming", [True, False])
def test_task_error_reporting(
sentry_init,
immediate_backend,
capture_events,
capture_items,
span_streaming,
):
"""Test that errors in tasks are correctly reported and don't break the span."""
sentry_init(
integrations=[DjangoIntegration()],
traces_sample_rate=1.0,
_experiments={"trace_lifecycle": "stream" if span_streaming else "static"},
)
if span_streaming:
items = capture_items("span")
with sentry_sdk.traces.start_span(name="custom parent"):
result = failing_task.enqueue()
with pytest.raises(ValueError, match="Task failed"):
_ = result.return_value
sentry_sdk.flush()
spans = [item.payload for item in items]
queue_submit_spans = [
span
for span in spans
if span["attributes"].get("sentry.op") == OP.QUEUE_SUBMIT_DJANGO
]
assert len(queue_submit_spans) == 1
assert (
queue_submit_spans[0]["name"]
== "tests.integrations.django.test_tasks.failing_task"
)
else:
events = capture_events()
with sentry_sdk.start_transaction(name="test_transaction"):
result = failing_task.enqueue()
with pytest.raises(ValueError, match="Task failed"):
_ = result.return_value
assert len(events) == 2
transaction_event = events[-1]
assert transaction_event["type"] == "transaction"
queue_submit_spans = [
span
for span in transaction_event["spans"]
if span["op"] == OP.QUEUE_SUBMIT_DJANGO
]
assert len(queue_submit_spans) == 1
assert (
queue_submit_spans[0]["description"]
== "tests.integrations.django.test_tasks.failing_task"
)
@pytest.mark.skipif(
not HAS_DJANGO_TASKS,
reason="Django tasks are only available in Django 6.0+",
)
@pytest.mark.parametrize("span_streaming", [True, False])
def test_multiple_task_enqueues_create_multiple_spans(
sentry_init,
capture_events,
capture_items,
immediate_backend,
span_streaming,
):
"""Test that enqueueing multiple tasks creates multiple spans."""
sentry_init(
integrations=[DjangoIntegration()],
traces_sample_rate=1.0,
_experiments={"trace_lifecycle": "stream" if span_streaming else "static"},
)
if span_streaming:
items = capture_items("span")
with sentry_sdk.traces.start_span(name="custom parent"):
task_one.enqueue()
task_two.enqueue()
task_one.enqueue()
sentry_sdk.flush()
spans = [item.payload for item in items]
queue_submit_spans = [
span
for span in spans
if span["attributes"].get("sentry.op") == OP.QUEUE_SUBMIT_DJANGO
]
assert len(queue_submit_spans) == 3
span_names = [span["name"] for span in queue_submit_spans]
else:
events = capture_events()
with sentry_sdk.start_transaction(name="test_transaction"):
task_one.enqueue()
task_two.enqueue()
task_one.enqueue()
(event,) = events
queue_submit_spans = [
span for span in event["spans"] if span["op"] == OP.QUEUE_SUBMIT_DJANGO
]
assert len(queue_submit_spans) == 3
span_names = [span["description"] for span in queue_submit_spans]
assert span_names.count("tests.integrations.django.test_tasks.task_one") == 2
assert span_names.count("tests.integrations.django.test_tasks.task_two") == 1