Skip to content

Commit 151f4cb

Browse files
antonpirkerlizokm
andauthored
Added documentation for Celery Beat auto monitoring (#6526)
* Added documentation for Celery Beat auto monitoring --------- Co-authored-by: Liza Mock <liza.mock@sentry.io>
1 parent 4a7954c commit 151f4cb

File tree

2 files changed

+67
-18
lines changed

2 files changed

+67
-18
lines changed
Lines changed: 59 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,89 @@
1-
## Job Monitoring
1+
## Celery Beat Auto Discovery
22

3-
Use the Celery Beat integration to monitor and notify you if your job 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.
3+
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.
44

5-
First, set up your Celery beat configuration:
5+
First, set up your Celery beat schedule:
66

77
```python
8-
from celery import Celery, signals
8+
# tasks.py
9+
from celery import Celery
910
from celery.schedules import crontab
1011

11-
import sentry_sdk
12-
from sentry_sdk.crons import monitor
13-
from sentry_sdk.integrations.celery import CeleryIntegration
1412

15-
app = Celery('mytasks', broker='redis://localhost:6379/0')
13+
app = Celery('tasks', broker='...')
1614
app.conf.beat_schedule = {
1715
'set-in-beat-schedule': {
1816
'task': 'tasks.tell_the_world',
1917
'schedule': crontab(hour='10', minute='15'),
20-
'args': ("in beat_schedule set", ),
18+
'args': ("Some important message!", ),
2119
},
2220
}
2321
```
2422

25-
Initialize Sentry either in `celeryd_init` or `beat_init` signal:
23+
Initialize Sentry in the `celeryd_init` or `beat_init` signal. Make sure to set `monitor_beat_tasks=True` in `CeleryIntegration`:
2624

2725
```python
28-
#@signals.celeryd_init.connect
29-
@signals.beat_init.connect
26+
# tasks.py
27+
from celery import signals
28+
29+
import sentry_sdk
30+
from sentry_sdk.integrations.celery import CeleryIntegration
31+
32+
33+
#@signals.beat_init.connect
34+
@signals.celeryd_init.connect
3035
def init_sentry(**kwargs):
3136
sentry_sdk.init(
3237
dsn='___PUBLIC_DSN___',
33-
integrations=[CeleryIntegration()],
38+
integrations=[CeleryIntegration(monitor_beat_tasks=True)], # 👈
3439
environment="local.dev.grace",
35-
release="v1.0.7-a1",
40+
release="v1.0",
3641
)
3742
```
3843

39-
Finally, link your Celery task to a Sentry Cron Monitor:
44+
Once Sentry Crons is set up, tasks in your Celery beat schedule will be auto-discoverable, and telemetry data will be captured when a task is started, when it finishes, and when it fails.
45+
46+
Start your Celery beat and worker services and see your tasks being monitored at [https://sentry.io/crons/](https://sentry.io/crons/).
47+
48+
<Note>
49+
50+
You don't need to create Cron Monitors for your tasks on Sentry.io, we'll do it for you.
51+
52+
</Note>
53+
54+
## Manual Task Monitoring
55+
56+
We provide a lightweight decorator to make monitoring individual tasks easier. To use it, add `@sentry_sdk.monitor` to your Celery task (or any function), then supply a `monitor_slug` of a monitor created previously on Sentry.io. Once this is done, every time the task (or function) is executed, telemetry data will be captured when a task is started, when it finishes, and when it fails.
57+
58+
<Note>
59+
60+
Make sure the Sentry `@sentry_sdk.monitor` decorator is below Celery's `@app.task` decorator.
61+
62+
</Note>
4063

4164
```python
65+
# tasks.py
66+
from celery import Celery, signals
67+
68+
import sentry_sdk
69+
from sentry_sdk.integrations.celery import CeleryIntegration
70+
71+
72+
app = Celery('tasks', broker='...')
73+
74+
75+
@signals.celeryd_init.connect
76+
def init_sentry(**kwargs):
77+
sentry_sdk.init(
78+
dsn='___PUBLIC_DSN___',
79+
integrations=[CeleryIntegration()],
80+
environment="local.dev.grace",
81+
release="v1.0",
82+
)
83+
84+
4285
@app.task
43-
@monitor(monitor_slug='<monitor-slug>')
86+
@sentry_sdk.monitor(monitor_slug='<monitor-slug>') # 👈 this is the new line.
4487
def tell_the_world(msg):
4588
print(msg)
4689
```

src/platform-includes/crons/setup/python.mdx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
## Job Monitoring
22

3-
Use the Python SDK to monitor and notify you if your job 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.
3+
Use the Python SDK to monitor and notify you if your periodic 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.
4+
5+
<Alert level="info" title="Note on Celery">
6+
7+
If you're using **Celery Beat** to run your periodic tasks, have a look at our [Celery Beat Auto Discovery documentation](/platforms/python/guides/celery/crons/).
8+
9+
</Alert>
410

511
```python
612
import sentry_sdk
713
from sentry_sdk.crons import monitor
814

9-
# Add the @monitor decorator to your job
15+
# Add the @monitor decorator to your task
1016
@monitor(monitor_slug='<monitor-slug>')
1117
def tell_the_world(msg):
1218
# Scheduled task here...

0 commit comments

Comments
 (0)