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: src/platforms/python/guides/rq/index.mdx
+81-17Lines changed: 81 additions & 17 deletions
Original file line number
Diff line number
Diff line change
@@ -8,24 +8,31 @@ description: "Learn about using Sentry with RQ."
8
8
9
9
The RQ integration adds support for the [RQ Job Queue System](https://python-rq.org/).
10
10
11
+
## Install
12
+
13
+
Install `sentry-sdk` from PyPI with the `rq` extra:
14
+
15
+
```bash
16
+
pip install --upgrade 'sentry-sdk[rq]'
17
+
```
18
+
19
+
## Configure
20
+
21
+
If you have the `rq` package in your dependencies, the RQ integration will be enabled automatically when you initialize the Sentry SDK.
22
+
11
23
Create a file called `mysettings.py` with the following content:
12
24
13
25
<SignInNote />
14
26
15
27
```python {filename:mysettings.py}
28
+
# mysettings.py
16
29
import sentry_sdk
17
-
from sentry_sdk.integrations.rq import RqIntegration
18
30
19
31
sentry_sdk.init(
20
-
dsn="___PUBLIC_DSN___",
21
-
integrations=[
22
-
RqIntegration(),
23
-
],
24
-
25
32
# Set traces_sample_rate to 1.0 to capture 100%
26
33
# of transactions for performance monitoring.
27
-
# We recommend adjusting this value in production,
28
34
traces_sample_rate=1.0,
35
+
dsn="___PUBLIC_DSN___",
29
36
)
30
37
```
31
38
@@ -34,45 +41,102 @@ Start your worker with:
34
41
```shell
35
42
rq worker \
36
43
-c mysettings \ # module name of mysettings.py
37
-
--sentry-dsn=""# only necessary for RQ < 1.0
44
+
--sentry-dsn="___PUBLIC_DSN___"# only necessary for RQ < 1.0
38
45
```
39
46
40
47
The integration will automatically report errors from all RQ jobs.
41
48
42
49
Generally, make sure that the **call to `init` is loaded on worker startup**, and not only in the module where your jobs are defined. Otherwise, the initialization happens too late and events might end up not being reported.
43
50
44
-
In addition, make sure that **`init` is called only once** in your app. For example, if you have a Flask app and a worker that depends on the app, we recommend initializing Sentry with a single configuration that is suitable for Flask and RQ, as in:
51
+
In addition, make sure that **`init` is called only once** in your app. For example, if you have a `Flask` app and a worker that depends on the app, we recommend only initializing Sentry once. Note that because the Flask integration is enabled automatically, you don't need to change the configuration shown above.
45
52
46
53
<SignInNote />
47
54
48
55
```python {filename:app.py}
56
+
# app.py
49
57
import sentry_sdk
50
-
from sentry_sdk.integrations.flask import FlaskIntegration
51
-
from sentry_sdk.integrations.rq import RqIntegration
52
58
53
59
sentry_sdk.init(
54
60
dsn=___PUBLIC_DSN___,
55
-
integrations=[
56
-
FlaskIntegration(),
57
-
RqIntegration(),
58
-
],
59
-
60
61
# Set traces_sample_rate to 1.0 to capture 100%
61
62
# of transactions for performance monitoring.
62
-
# We recommend adjusting this value in production,
63
63
traces_sample_rate=1.0,
64
64
)
65
65
```
66
66
67
67
The worker configuration `mysettings.py` then becomes:
68
68
69
69
```python {filename:mysettings.py}
70
+
# mysettings.py
70
71
# This import causes the Sentry SDK to be initialized
71
72
import app
72
73
```
73
74
75
+
## Verify
76
+
77
+
To verify, create a `main.py` script that enqueues a function in RQ, then start an RQ worker to run the function:
78
+
79
+
### Job definition:
80
+
81
+
```python {filename:jobs.py}
82
+
# jobs.py
83
+
defhello(name):
84
+
1/0# raises an error
85
+
return"Hello %s!"% name
86
+
```
87
+
88
+
### Settings for worker
89
+
90
+
```python {filename:mysettings.py}
91
+
# mysettings.py
92
+
import sentry_sdk
93
+
94
+
# Sentry configuration for RQ worker processes
95
+
sentry_sdk.init(
96
+
dsn=___PUBLIC_DSN___,
97
+
# Set traces_sample_rate to 1.0 to capture 100%
98
+
# of transactions for performance monitoring.
99
+
traces_sample_rate=1.0,
100
+
)
101
+
```
102
+
103
+
### Main Python Script
104
+
105
+
```python {filename:main.py}
106
+
# main.py
107
+
from redis import Redis
108
+
from rq import Queue
109
+
110
+
from jobs import hello
111
+
112
+
import sentry_sdk
113
+
114
+
# Sentry configuration for main.py process
115
+
sentry_sdk.init(
116
+
dsn=___PUBLIC_DSN___,
117
+
# Set traces_sample_rate to 1.0 to capture 100%
118
+
# of transactions for performance monitoring.
119
+
traces_sample_rate=1.0,
120
+
)
121
+
122
+
q = Queue(connection=Redis())
123
+
with sentry_sdk.start_transaction(name="testing_sentry"):
124
+
result = q.enqueue(hello, "World")
125
+
```
126
+
127
+
When you run `python main.py` a transaction named `testing_sentry` will be created in the Performance section of [sentry.io](https://sentry.io) and spans for the enqueueing will be created.
128
+
129
+
If you run the RQ worker with `rq worker -c mysettings`, a transaction for the execution of `hello()` will be created. Additionally, an error event will be sent to [sentry.io](https://sentry.io) and will be connected to the transaction.
130
+
131
+
It takes a couple of moments for the data to appear in [sentry.io](https://sentry.io).
132
+
74
133
## The `--sentry-dsn` CLI option
75
134
76
135
Passing `--sentry-dsn=""` to RQ forcibly disables [RQ's shortcut for using Sentry](https://python-rq.org/patterns/sentry/). For RQ versions before 1.0 this is necessary to avoid conflicts, because back then RQ would attempt to use the `raven` package instead of this SDK. Since RQ 1.0 it's possible to use this CLI option and the associated RQ settings for initializing the SDK.
77
136
78
137
We still recommend against using those shortcuts because it would be harder to provide options to the SDK at a later point. See [the GitHub issue about RQ's Sentry integration](https://github.com/rq/rq/issues/1003) for discussion.
0 commit comments