Skip to content

Commit 785bdbb

Browse files
committed
Updated celery instrumentation documentation
1 parent 5d6caaf commit 785bdbb

File tree

2 files changed

+81
-33
lines changed

2 files changed

+81
-33
lines changed

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

Lines changed: 10 additions & 15 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 monitors will only be created on the task's first run.
1414
</Note>
1515

1616
First, set 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 crontab parseable schedules will be successfully
35+
upserted.
3536
</Note>
3637

3738
Next, we need to 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 {filename:tasks.py} {"onboardingOptions": {"performance": "12-14", "profiling": "15-18"}}
5754
# tasks.py
5855
from celery import signals
5956

@@ -98,7 +95,6 @@ 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-
10298
```python
10399
sentry_sdk.init(
104100
# ...
@@ -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 {filename:tasks.py}
132128
# tasks.py
133129
from celery import Celery, signals
134130

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

146-
147142
@app.task
148143
@sentry_sdk.monitor(monitor_slug='<monitor-slug>') # 👈 this is the new line.
149144
def tell_the_world(msg):

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

Lines changed: 71 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,36 +17,89 @@ 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+
Make sure that the **call to `sentry_sdk.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.
2121

22-
<PlatformContent includePath="getting-started-config" />
22+
### Setup Celery (Without Django)
2323

24-
### Standalone Setup
24+
To get the most out of Sentry make sure to initialize the Sentry SDK in your Celery worker processes as well as your application that is sending messages to Celery.
2525

26-
If you're using Celery standalone, there are two ways to set this up:
26+
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/).
2727

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
28+
Select which Sentry features you'd like to install in addition to Error Monitoring to get the corresponding installation and configuration instructions below.
3029

31-
```python
32-
import sentry_sdk
33-
from celery import Celery, signals
30+
#### Setup in Celery
3431

35-
app = Celery("myapp")
32+
<OnboardingOptionButtons
33+
options={["error-monitoring", "performance", "profiling"]}
34+
/>
3635

37-
#@signals.worker_init.connect
38-
@signals.celeryd_init.connect
39-
def init_sentry(**_kwargs):
40-
sentry_sdk.init(...) # same as above
41-
```
36+
```python {filename:tasks.py} {"onboardingOptions": {"performance": "12-14", "profiling": "15-18"}}
37+
from celery import Celery, signals
38+
import sentry_sdk
39+
40+
# Initializing Celery
41+
app = Celery("tasks", broker="...")
42+
43+
# Initialize Sentry SDK on Celery startup
44+
@signals.celeryd_init.connect
45+
def init_sentry(**_kwargs):
46+
sentry_sdk.init(
47+
dsn="___PUBLIC_DSN___",
48+
# Set traces_sample_rate to 1.0 to capture 100%
49+
# of transactions for tracing.
50+
traces_sample_rate=1.0,
51+
# Set profiles_sample_rate to 1.0 to profile 100%
52+
# of sampled transactions.
53+
# We recommend adjusting this value in production.
54+
profiles_sample_rate=1.0,
55+
)
56+
57+
# Task definitions go here
58+
@app.task
59+
def add(x, y):
60+
return x + y
61+
```
62+
63+
The [`celeryd_init`](https://docs.celeryq.dev/en/stable/userguide/signals.html?#celeryd-init) signal is triggered when the Celery deamon is started, before the worker processes are spawned. You can use the [`worker_init`](https://docs.celeryq.dev/en/stable/userguide/signals.html?#worker-init) signal instead if you want to initialize Sentry on start of each worker process.
64+
65+
#### Setup in Your Application
66+
67+
<OnboardingOptionButtons
68+
options={["error-monitoring", "performance", "profiling"]}
69+
/>
70+
71+
```python {filename:main.py} {"onboardingOptions": {"performance": "8-10", "profiling": "11-14"}}
72+
from tasks import add
73+
import sentry_sdk
74+
75+
def main():
76+
# Initializing Sentry SDK in our process
77+
sentry_sdk.init(
78+
dsn="___PUBLIC_DSN___",
79+
# Set traces_sample_rate to 1.0 to capture 100%
80+
# of transactions for tracing.
81+
traces_sample_rate=1.0,
82+
# Set profiles_sample_rate to 1.0 to profile 100%
83+
# of sampled transactions.
84+
# We recommend adjusting this value in production.
85+
profiles_sample_rate=1.0,
86+
)
87+
88+
# Enqueueing a task to be processed by Celery
89+
with sentry_sdk.start_transaction(name="calling-a-celery-task"):
90+
result = add.delay(4, 4)
91+
92+
if __name__ == "__main__":
93+
main()
94+
```
4295

43-
### Setup With Django
96+
### Setup Celery With Django
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+
If you're using Celery with Django in a conventional setup, have already initialized the SDK in your `settings.py` file as described in the [Django integration documentation](/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.
4699

47100
## Verify
48101

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.
102+
To verify if your SDK is initialized on worker start, you can pass `debug=True` to `sentry_sdk.init()` to see extra output in your Celery logs 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.
50103

51104
<Alert level="info" title="Note on distributed tracing">
52105

0 commit comments

Comments
 (0)