|
1 |
| -## Job Monitoring |
| 1 | +## Celery Beat Auto Discovery |
2 | 2 |
|
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. |
4 | 4 |
|
5 |
| -First, set up your Celery beat configuration: |
| 5 | +First, set up your Celery beat schedule: |
6 | 6 |
|
7 | 7 | ```python
|
8 |
| -from celery import Celery, signals |
| 8 | +# tasks.py |
| 9 | +from celery import Celery |
9 | 10 | from celery.schedules import crontab
|
10 | 11 |
|
11 |
| -import sentry_sdk |
12 |
| -from sentry_sdk.crons import monitor |
13 |
| -from sentry_sdk.integrations.celery import CeleryIntegration |
14 | 12 |
|
15 |
| -app = Celery('mytasks', broker='redis://localhost:6379/0') |
| 13 | +app = Celery('tasks', broker='...') |
16 | 14 | app.conf.beat_schedule = {
|
17 | 15 | 'set-in-beat-schedule': {
|
18 | 16 | 'task': 'tasks.tell_the_world',
|
19 | 17 | 'schedule': crontab(hour='10', minute='15'),
|
20 |
| - 'args': ("in beat_schedule set", ), |
| 18 | + 'args': ("Some important message!", ), |
21 | 19 | },
|
22 | 20 | }
|
23 | 21 | ```
|
24 | 22 |
|
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`: |
26 | 24 |
|
27 | 25 | ```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 |
30 | 35 | def init_sentry(**kwargs):
|
31 | 36 | sentry_sdk.init(
|
32 | 37 | dsn='___PUBLIC_DSN___',
|
33 |
| - integrations=[CeleryIntegration()], |
| 38 | + integrations=[CeleryIntegration(monitor_beat_tasks=True)], # 👈 |
34 | 39 | environment="local.dev.grace",
|
35 |
| - release="v1.0.7-a1", |
| 40 | + release="v1.0", |
36 | 41 | )
|
37 | 42 | ```
|
38 | 43 |
|
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> |
40 | 63 |
|
41 | 64 | ```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 | + |
42 | 85 | @app.task
|
43 |
| -@monitor(monitor_slug='<monitor-slug>') |
| 86 | +@sentry_sdk.monitor(monitor_slug='<monitor-slug>') # 👈 this is the new line. |
44 | 87 | def tell_the_world(msg):
|
45 | 88 | print(msg)
|
46 | 89 | ```
|
0 commit comments