Skip to content

Commit 0f4cb5b

Browse files
feat(python): Document default span attrs in Celery, update for 3.0 (#14365)
Document what span attributes will be attached to transactions by default by our builtin integrations. This PR updates Celery. Rest will follow. I created a new 3.x page for this since it's new behavior in 3.0. --------- Co-authored-by: Alex Krawiec <[email protected]>
1 parent dc36cd5 commit 0f4cb5b

File tree

1 file changed

+266
-0
lines changed

1 file changed

+266
-0
lines changed
Lines changed: 266 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,266 @@
1+
---
2+
title: Celery
3+
description: "Learn about using Sentry with Celery."
4+
---
5+
6+
The Celery integration adds support for the [Celery Task Queue System](https://docs.celeryq.dev/).
7+
8+
## Install
9+
10+
Install `sentry-sdk` from PyPI with the `celery` extra:
11+
12+
```bash {tabTitle:pip}
13+
pip install "sentry-sdk[celery]"
14+
```
15+
```bash {tabTitle:uv}
16+
uv add "sentry-sdk[celery]"
17+
```
18+
19+
## Configure
20+
21+
If you have the `celery` package in your dependencies, the Celery integration will be enabled automatically when you initialize the Sentry SDK.
22+
23+
<Alert>
24+
Make sure you call `sentry_sdk.init()` when the worker process starts,
25+
not just in the module that defines your tasks. Otherwise, the
26+
initialization may happen too late, causing events to go unreported.
27+
</Alert>
28+
29+
### Set up Celery Without Django
30+
31+
When using Celery without Django, you'll need to initialize the Sentry SDK in both your application and the Celery worker processes spawned by the Celery daemon.
32+
33+
In addition to capturing errors, you can use Sentry for [distributed tracing](/concepts/key-terms/tracing/) and [profiling](/product/explore/profiling/). Select what you'd like to install to get the corresponding installation and configuration instructions below.
34+
35+
#### Set up Sentry in Celery Daemon or Worker Processes
36+
37+
<OnboardingOptionButtons
38+
options={["error-monitoring", "performance", "profiling"]}
39+
/>
40+
41+
```python {filename:tasks.py}
42+
from celery import Celery, signals
43+
import sentry_sdk
44+
45+
# Initializing Celery
46+
app = Celery("tasks", broker="...")
47+
48+
# Initialize Sentry SDK on Celery startup
49+
@signals.celeryd_init.connect
50+
def init_sentry(**_kwargs):
51+
sentry_sdk.init(
52+
dsn="___PUBLIC_DSN___",
53+
# Add request headers and IP for users,
54+
# see https://docs.sentry.io/platforms/python/data-management/data-collected/ for more info
55+
send_default_pii=True,
56+
# ___PRODUCT_OPTION_START___ performance
57+
# Set traces_sample_rate to 1.0 to capture 100%
58+
# of transactions for tracing.
59+
traces_sample_rate=1.0,
60+
# ___PRODUCT_OPTION_END___ performance
61+
# ___PRODUCT_OPTION_START___ profiling
62+
# To collect profiles for all profile sessions,
63+
# set `profile_session_sample_rate` to 1.0.
64+
profile_session_sample_rate=1.0,
65+
# Profiles will be automatically collected while
66+
# there is an active span.
67+
profile_lifecycle="trace",
68+
# ___PRODUCT_OPTION_END___ profiling
69+
)
70+
71+
# Task definitions go here
72+
@app.task
73+
def add(x, y):
74+
return x + y
75+
```
76+
77+
The [`celeryd_init`](https://docs.celeryq.dev/en/stable/userguide/signals.html?#celeryd-init) signal is triggered when the Celery daemon starts, before the worker processes are spawned. If you need to initialize Sentry for each individual worker process, use the [`worker_init`](https://docs.celeryq.dev/en/stable/userguide/signals.html?#worker-init) signal instead.
78+
79+
#### Set up Sentry in Your Application
80+
81+
<OnboardingOptionButtons
82+
options={["error-monitoring", "performance", "profiling"]}
83+
/>
84+
85+
```python {filename:main.py}
86+
from tasks import add
87+
import sentry_sdk
88+
89+
def main():
90+
# Initializing Sentry SDK in our process
91+
sentry_sdk.init(
92+
dsn="___PUBLIC_DSN___",
93+
# Add data like request headers and IP for users, if applicable;
94+
# see https://docs.sentry.io/platforms/python/data-management/data-collected/ for more info
95+
send_default_pii=True,
96+
# ___PRODUCT_OPTION_START___ performance
97+
# Set traces_sample_rate to 1.0 to capture 100%
98+
# of transactions for tracing.
99+
traces_sample_rate=1.0,
100+
# ___PRODUCT_OPTION_END___ performance
101+
# ___PRODUCT_OPTION_START___ profiling
102+
# To collect profiles for all profile sessions,
103+
# set `profile_session_sample_rate` to 1.0.
104+
profile_session_sample_rate=1.0,
105+
# Profiles will be automatically collected while
106+
# there is an active span.
107+
profile_lifecycle="trace",
108+
# ___PRODUCT_OPTION_END___ profiling
109+
)
110+
111+
# Enqueueing a task to be processed by Celery
112+
with sentry_sdk.start_transaction(name="calling-a-celery-task"):
113+
result = add.delay(4, 4)
114+
115+
if __name__ == "__main__":
116+
main()
117+
```
118+
119+
### Set up Celery With Django
120+
121+
If you're using Celery with Django in a typical setup, have initialized the SDK in your `settings.py` file (as described in the [Django integration documentation](/platforms/python/integrations/django/#configure)), and have your Celery configured to use the same settings as [`config_from_object`](https://docs.celeryq.dev/en/stable/django/first-steps-with-django.html), there's no need to initialize the Celery SDK separately.
122+
123+
## Verify
124+
125+
To confirm that your SDK is initialized on worker start, pass `debug=True` to `sentry_sdk.init()`. This will add extra output to your Celery logs when the SDK is initialized. If you see the output during worker startup, and not just after a task has started, then it's working correctly.
126+
127+
The snippet below includes an intentional `ZeroDivisionError` in the Celery task that will be captured by Sentry. To trigger the error call `debug_sentry.delay()`:
128+
129+
```python {filename:tasks.py}
130+
from celery import Celery, signals
131+
import sentry_sdk
132+
133+
app = Celery("tasks", broker="...")
134+
135+
@signals.celeryd_init.connect
136+
def init_sentry(**_kwargs):
137+
sentry_sdk.init(...) # same as above
138+
139+
@app.task
140+
def debug_sentry():
141+
1/0
142+
```
143+
144+
<Alert title="Note on distributed tracing">
145+
146+
Sentry uses custom message headers for distributed tracing. For Celery versions 4.x, with [message protocol of version 1](https://docs.celeryq.dev/en/stable/internals/protocol.html#version-1), this functionality is broken, and Celery fails to propagate custom headers to the worker. Protocol version 2, which is the default since Celery version 4.0, is not affected.
147+
148+
The fix for the custom headers propagation issue was introduced to Celery project ([PR](https://github.com/celery/celery/pull/6374)) starting with version 5.0.1. However, the fix was not backported to versions 4.x.
149+
150+
</Alert>
151+
152+
## Options
153+
154+
To set options on `CeleryIntegration` to change its behavior, add it explicitly to your `sentry_sdk.init()`:
155+
156+
```python
157+
import sentry_sdk
158+
from sentry_sdk.integrations.celery import CeleryIntegration
159+
160+
sentry_sdk.init(
161+
# same as above
162+
integrations=[
163+
CeleryIntegration(
164+
monitor_beat_tasks=True,
165+
exclude_beat_tasks=[
166+
"unimportant-task",
167+
"payment-check-.*"
168+
],
169+
),
170+
],
171+
)
172+
```
173+
174+
You can pass the following keyword arguments to `CeleryIntegration()`:
175+
176+
- `propagate_traces`
177+
178+
Propagate Sentry tracing information to the Celery task. This makes it possible to link Celery task errors to the function that triggered the task.
179+
180+
If this is set to `False`:
181+
182+
- errors in Celery tasks won't be matched to the triggering function.
183+
- your Celery tasks will start a new trace and won't be connected to the trace in the calling function.
184+
185+
The default is `True`.
186+
187+
See [Distributed Traces](#distributed-traces) below to learn how to get more fine-grained control over distributed tracing in Celery tasks.
188+
189+
- `monitor_beat_tasks`:
190+
191+
Turn auto-instrumentation on or off for Celery Beat tasks using Sentry Crons.
192+
193+
See <PlatformLink to="/crons/#celery-beat-auto-discovery">Celery Beat Auto Discovery</PlatformLink> to learn more.
194+
195+
The default is `False`.
196+
197+
- `exclude_beat_tasks`:
198+
199+
A list of Celery Beat tasks that should be excluded from auto-instrumentation using Sentry Crons. Only applied if `monitor_beat_tasks` is set to `True`.
200+
201+
The list can contain strings with the names of tasks in the Celery Beat schedule to be excluded. It can also include regular expressions to match multiple tasks. For example, if you include `"payment-check-.*"` every task starting with `payment-check-` will be excluded from auto-instrumentation.
202+
203+
See <PlatformLink to="/crons/#celery-beat-auto-discovery">Celery Beat Auto Discovery</PlatformLink> to learn more.
204+
205+
The default is `None`.
206+
207+
## Behavior
208+
209+
### Distributed Traces
210+
211+
Distributed tracing connects the trace of your Celery task to the trace of the code that started the task, giving you a complete view of the entire workflow.
212+
213+
You can disable this globally with the `propagate_traces` parameter, documented above. If you set `propagate_traces` to `False`, all Celery tasks will start their own trace.
214+
215+
If you want to have more fine-grained control over trace distribution, you can override the `propagate_traces` option by passing the `sentry-propagate-traces` header when starting the Celery task:
216+
217+
**Note:** The `CeleryIntegration` does not utilize the `traces_sample_rate` config option for deciding if a trace should be propagated into a Celery task.
218+
219+
```python
220+
import sentry_sdk
221+
222+
# Enable global distributed traces (this is the default, just to be explicit)
223+
sentry_sdk.init(
224+
# same as above
225+
integrations=[
226+
CeleryIntegration(
227+
propagate_traces=True
228+
),
229+
],
230+
)
231+
232+
# This will propagate the trace:
233+
my_task_a.delay("some parameter")
234+
235+
# This will propagate the trace:
236+
my_task_b.apply_async(
237+
args=("some_parameter", )
238+
)
239+
240+
# This will NOT propagate the trace. The task will start its own trace:
241+
my_task_b.apply_async(
242+
args=("some_parameter", ),
243+
headers={"sentry-propagate-traces": False},
244+
)
245+
246+
# Note: overriding the tracing behaviour using `task_x.delay()` is not possible.
247+
```
248+
249+
### Default Span Attributes
250+
251+
A set of predefined span attributes will be attached to Celery transactions by default. These can also be used for sampling since they will also be accessible via the `sampling_context` dictionary in the [`traces_sampler`](/platforms/python/configuration/options/#traces_sampler).
252+
253+
| Span Attribute | Description |
254+
| --------------------------- | ----------------------------------------------------- |
255+
| `celery.job.args.{index}` | Positional arguments provided to the task, serialized |
256+
| `celery.job.kwargs.{kwarg}` | Keyword arguments provided to the task, serialized |
257+
| `celery.job.task` | Task name |
258+
259+
These attributes will also be sent to Sentry. If you don't want that, you can filter them out using a custom [`before_send`](/platforms/python/configuration/options/#before_send) function.
260+
261+
## Supported Versions
262+
263+
- Celery: 4.4.7+
264+
- Python: 3.7+
265+
266+
<Include name="python-use-older-sdk-for-legacy-support.mdx" />

0 commit comments

Comments
 (0)