Skip to content

Commit 6f675a2

Browse files
antonpirkerlizokmszokeasaurusrex
authored
[Python] Updated celery instrumentation documentation (#11600)
Better description on how and where to setup the Python SDK when you use Celery. --------- Co-authored-by: Liza Mock <[email protected]> Co-authored-by: Daniel Szoke <[email protected]>
1 parent 12096bc commit 6f675a2

File tree

2 files changed

+113
-46
lines changed

2 files changed

+113
-46
lines changed

docs/platforms/python/integrations/celery/crons.mdx

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,16 @@ Sentry Crons allows you to monitor the uptime and performance of any scheduled,
1010
Use the Celery integration to monitor your [Celery periodic tasks](https://docs.celeryq.dev/en/stable/userguide/periodic-tasks.html) and get notified when a task is missed (or doesn't start when expected), if it fails due to a problem in the runtime (such as an error), or if it fails by exceeding its maximum runtime.
1111

1212
<Note>
13-
Please note that monitors will only be created on the task's first run.
13+
Please note that a cron monitor will only be created the first time your task runs.
1414
</Note>
1515

16-
First, set up your Celery beat schedule:
16+
Get started by setting up your Celery beat schedule:
1717

18-
```python
18+
```python {filename:tasks.py}
1919
# tasks.py
2020
from celery import Celery
2121
from celery.schedules import crontab
2222

23-
2423
app = Celery('tasks', broker='...')
2524
app.conf.beat_schedule = {
2625
'set-in-beat-schedule': {
@@ -30,30 +29,28 @@ app.conf.beat_schedule = {
3029
},
3130
}
3231
```
32+
3333
<Note>
34-
Please note that only crontab parseable schedules will be successfully upserted.
34+
Please note that only schedules that can be parsed by crontab will be successfully
35+
updated or inserted.
3536
</Note>
3637

37-
Next, we need to initialize Sentry. Where to do this depends on how you run beat:
38+
Next, initialize Sentry. Where to do this depends on how you run beat:
39+
3840
- If beat is running in your worker process (that is, you're running your worker with the `-B`/`--beat` option), initialize Sentry in either the `celeryd_init` or `beat_init` signal.
39-
- If beat is running in a separate process, you need to initialize Sentry in *both* the `celeryd_init` and `beat_init` signal.
41+
- If beat is running in a separate process, you need to initialize Sentry in _both_ the `celeryd_init` and `beat_init` signal.
4042

4143
Make sure to also set `monitor_beat_tasks=True` in `CeleryIntegration`.
4244

43-
4445
In addition to capturing errors, you can monitor interactions between multiple services or applications by [enabling tracing](/concepts/key-terms/tracing/). You can also collect and analyze performance profiles from real users with [profiling](/product/explore/profiling/).
4546

4647
Select which Sentry features you'd like to install in addition to Error Monitoring to get the corresponding installation and configuration instructions below.
4748

4849
<OnboardingOptionButtons
49-
options={[
50-
'error-monitoring',
51-
'performance',
52-
'profiling',
53-
]}
50+
options={["error-monitoring", "performance", "profiling"]}
5451
/>
5552

56-
```python {"onboardingOptions": {"performance": "12-14", "profiling": "15-18"}}
53+
```python {diff} {filename:tasks.py} {"onboardingOptions": {"performance": "12-14", "profiling": "15-18"}}
5754
# tasks.py
5855
from celery import signals
5956

@@ -72,11 +69,11 @@ def init_sentry(**kwargs):
7269
# of sampled transactions.
7370
# We recommend adjusting this value in production.
7471
profiles_sample_rate=1.0,
75-
integrations=[
76-
CeleryIntegration(
77-
monitor_beat_tasks=True
78-
)
79-
],
72+
+ integrations=[
73+
+ CeleryIntegration(
74+
+ monitor_beat_tasks=True
75+
+ )
76+
+ ],
8077
environment="local.dev.grace",
8178
release="v1.0",
8279
)
@@ -98,17 +95,16 @@ You don't need to create Cron Monitors for your tasks on Sentry.io, we'll do it
9895

9996
You can exclude Celery Beat tasks from being auto-instrumented. To do this, add a list of tasks you want to exclude as option `exclude_beat_tasks` when creating `CeleryIntegration`. The list can contain simple strings with the full task name, as specified in the Celery Beat schedule, or regular expressions to match multiple tasks.
10097

101-
102-
```python
98+
```python {diff}
10399
sentry_sdk.init(
104100
# ...
105101
integrations=[
106102
CeleryIntegration(
107103
monitor_beat_tasks=True,
108-
exclude_beat_tasks=[
109-
"some-task-a",
110-
"payment-check-.*",
111-
]
104+
+ exclude_beat_tasks=[
105+
+ "some-task-a",
106+
+ "payment-check-.*",
107+
+ ]
112108
),
113109
],
114110
)
@@ -128,7 +124,7 @@ Make sure the Sentry `@sentry_sdk.monitor` decorator is below Celery's `@app.tas
128124

129125
</Note>
130126

131-
```python
127+
```python {diff} {filename:tasks.py}
132128
# tasks.py
133129
from celery import Celery, signals
134130

@@ -143,9 +139,8 @@ def init_sentry(**kwargs):
143139
# same as above
144140
)
145141

146-
147142
@app.task
148-
@sentry_sdk.monitor(monitor_slug='<monitor-slug>') # 👈 this is the new line.
143+
+@sentry_sdk.monitor(monitor_slug='<monitor-slug>')
149144
def tell_the_world(msg):
150145
print(msg)
151146
```

docs/platforms/python/integrations/celery/index.mdx

Lines changed: 90 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,36 +17,108 @@ pip install --upgrade 'sentry-sdk[celery]'
1717

1818
If you have the `celery` package in your dependencies, the Celery integration will be enabled automatically when you initialize the Sentry SDK.
1919

20-
Make sure that the **call to `init` is loaded on worker startup**, and not only in the module where your tasks are defined. Otherwise, the initialization happens too late and events might end up not being reported.
20+
<Alert>
21+
Make sure that the call to `sentry_sdk.init()` is loaded on worker startup and
22+
not only in the module where your tasks are defined. Otherwise, the
23+
initialization may happen too late and events might not get reported.
24+
</Alert>
25+
26+
### Set up Celery Without Django
2127

22-
<PlatformContent includePath="getting-started-config" />
28+
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.
2329

24-
### Standalone Setup
30+
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.
2531

26-
If you're using Celery standalone, there are two ways to set this up:
32+
#### Set up Sentry in Celery Daemon or Worker Processes
2733

28-
- Initializing the SDK in the configuration file loaded with Celery's `--config` parameter
29-
- Initializing the SDK by hooking it to either the [`celeryd_init`](https://docs.celeryq.dev/en/stable/userguide/signals.html?#celeryd-init) or [`worker_init`](https://docs.celeryq.dev/en/stable/userguide/signals.html?#worker-init) signals
34+
<OnboardingOptionButtons
35+
options={["error-monitoring", "performance", "profiling"]}
36+
/>
3037

31-
```python
32-
import sentry_sdk
33-
from celery import Celery, signals
38+
```python {filename:tasks.py} {"onboardingOptions": {"performance": "12-14", "profiling": "15-18"}}
39+
from celery import Celery, signals
40+
import sentry_sdk
41+
42+
# Initializing Celery
43+
app = Celery("tasks", broker="...")
44+
45+
# Initialize Sentry SDK on Celery startup
46+
@signals.celeryd_init.connect
47+
def init_sentry(**_kwargs):
48+
sentry_sdk.init(
49+
dsn="___PUBLIC_DSN___",
50+
# Set traces_sample_rate to 1.0 to capture 100%
51+
# of transactions for tracing.
52+
traces_sample_rate=1.0,
53+
# Set profiles_sample_rate to 1.0 to profile 100%
54+
# of sampled transactions.
55+
# We recommend adjusting this value in production.
56+
profiles_sample_rate=1.0,
57+
)
58+
59+
# Task definitions go here
60+
@app.task
61+
def add(x, y):
62+
return x + y
63+
```
3464

35-
app = Celery("myapp")
65+
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, us the [`worker_init`](https://docs.celeryq.dev/en/stable/userguide/signals.html?#worker-init) signal instead.
3666

37-
#@signals.worker_init.connect
38-
@signals.celeryd_init.connect
39-
def init_sentry(**_kwargs):
40-
sentry_sdk.init(...) # same as above
41-
```
67+
#### Set up Sentry in Your Application
4268

43-
### Setup With Django
69+
<OnboardingOptionButtons
70+
options={["error-monitoring", "performance", "profiling"]}
71+
/>
72+
73+
```python {filename:main.py} {"onboardingOptions": {"performance": "8-10", "profiling": "11-14"}}
74+
from tasks import add
75+
import sentry_sdk
76+
77+
def main():
78+
# Initializing Sentry SDK in our process
79+
sentry_sdk.init(
80+
dsn="___PUBLIC_DSN___",
81+
# Set traces_sample_rate to 1.0 to capture 100%
82+
# of transactions for tracing.
83+
traces_sample_rate=1.0,
84+
# Set profiles_sample_rate to 1.0 to profile 100%
85+
# of sampled transactions.
86+
# We recommend adjusting this value in production.
87+
profiles_sample_rate=1.0,
88+
)
89+
90+
# Enqueueing a task to be processed by Celery
91+
with sentry_sdk.start_transaction(name="calling-a-celery-task"):
92+
result = add.delay(4, 4)
93+
94+
if __name__ == "__main__":
95+
main()
96+
```
4497

45-
If you're using Celery with Django in a conventional setup, have already initialized the SDK in [your `settings.py` file](/platforms/python/integrations/django/#configure), and have Celery using the same settings with [`config_from_object`](https://docs.celeryq.dev/en/stable/django/first-steps-with-django.html), you don't need to initialize the SDK separately for Celery.
98+
### Set up Celery With Django
99+
100+
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.
46101

47102
## Verify
48103

49-
To verify if your SDK is initialized on worker start, you can pass `debug=True` to `sentry_sdk.init()` to see extra output when the SDK is initialized. If the output appears during worker startup and not only after a task has started, then it's working properly.
104+
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.
105+
106+
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()`:
107+
108+
```python {filename:tasks.py} {"onboardingOptions": {"performance": "12-14", "profiling": "15-18"}}
109+
from celery import Celery, signals
110+
import sentry_sdk
111+
112+
app = Celery("tasks", broker="...")
113+
114+
@signals.celeryd_init.connect
115+
def init_sentry(**_kwargs):
116+
sentry_sdk.init(...) # same as above
117+
118+
@app.task
119+
def debug_sentry():
120+
1/0
121+
```
50122

51123
<Alert level="info" title="Note on distributed tracing">
52124

0 commit comments

Comments
 (0)