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
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]>
Copy file name to clipboardExpand all lines: docs/platforms/python/integrations/celery/crons.mdx
+23-28Lines changed: 23 additions & 28 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 a cron monitor will only be created the first time your task runs.
14
14
</Note>
15
15
16
-
First, set up your Celery beat schedule:
16
+
Get started by setting 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 schedules that can be parsed by crontab will be successfully
35
+
updated or inserted.
35
36
</Note>
36
37
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
+
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.
# We recommend adjusting this value in production.
74
71
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
+
+ ],
80
77
environment="local.dev.grace",
81
78
release="v1.0",
82
79
)
@@ -98,17 +95,16 @@ 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
-
```python
98
+
```python {diff}
103
99
sentry_sdk.init(
104
100
# ...
105
101
integrations=[
106
102
CeleryIntegration(
107
103
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
+
+ ]
112
108
),
113
109
],
114
110
)
@@ -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 {diff} {filename:tasks.py}
132
128
# tasks.py
133
129
from celery import Celery, signals
134
130
@@ -143,9 +139,8 @@ def init_sentry(**kwargs):
143
139
# same as above
144
140
)
145
141
146
-
147
142
@app.task
148
-
@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
+
<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.
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.
23
29
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.
25
31
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
27
33
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
# We recommend adjusting this value in production.
56
+
profiles_sample_rate=1.0,
57
+
)
58
+
59
+
# Task definitions go here
60
+
@app.task
61
+
defadd(x, y):
62
+
return x + y
63
+
```
34
64
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.
# 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
+
```
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
+
### 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.
46
101
47
102
## Verify
48
103
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()`:
0 commit comments