You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/platforms/python/integrations/celery/crons.mdx
+10-15Lines changed: 10 additions & 15 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,17 +10,16 @@ Sentry Crons allows you to monitor the uptime and performance of any scheduled,
10
10
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.
11
11
12
12
<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.
14
14
</Note>
15
15
16
16
First, set up your Celery beat schedule:
17
17
18
-
```python
18
+
```python {filename:tasks.py}
19
19
# tasks.py
20
20
from celery import Celery
21
21
from celery.schedules import crontab
22
22
23
-
24
23
app = Celery('tasks', broker='...')
25
24
app.conf.beat_schedule = {
26
25
'set-in-beat-schedule': {
@@ -30,30 +29,28 @@ app.conf.beat_schedule = {
30
29
},
31
30
}
32
31
```
32
+
33
33
<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.
35
36
</Note>
36
37
37
38
Next, we need to initialize Sentry. Where to do this depends on how you run beat:
39
+
38
40
- 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.
40
42
41
43
Make sure to also set `monitor_beat_tasks=True` in `CeleryIntegration`.
42
44
43
-
44
45
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/).
45
46
46
47
Select which Sentry features you'd like to install in addition to Error Monitoring to get the corresponding installation and configuration instructions below.
@@ -98,7 +95,6 @@ You don't need to create Cron Monitors for your tasks on Sentry.io, we'll do it
98
95
99
96
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.
100
97
101
-
102
98
```python
103
99
sentry_sdk.init(
104
100
# ...
@@ -128,7 +124,7 @@ Make sure the Sentry `@sentry_sdk.monitor` decorator is below Celery's `@app.tas
128
124
129
125
</Note>
130
126
131
-
```python
127
+
```python {filename:tasks.py}
132
128
# tasks.py
133
129
from celery import Celery, signals
134
130
@@ -143,7 +139,6 @@ def init_sentry(**kwargs):
143
139
# same as above
144
140
)
145
141
146
-
147
142
@app.task
148
143
@sentry_sdk.monitor(monitor_slug='<monitor-slug>') # 👈 this is the new line.
If you have the `celery` package in your dependencies, the Celery integration will be enabled automatically when you initialize the Sentry SDK.
19
19
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.
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.
25
25
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/).
27
27
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.
# We recommend adjusting this value in production.
54
+
profiles_sample_rate=1.0,
55
+
)
56
+
57
+
# Task definitions go here
58
+
@app.task
59
+
defadd(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.
# 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
+
```
42
95
43
-
### Setup With Django
96
+
### Setup Celery With Django
44
97
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.
46
99
47
100
## Verify
48
101
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.
50
103
51
104
<Alertlevel="info"title="Note on distributed tracing">
0 commit comments