Skip to content

Commit 8dff2f4

Browse files
antonpirkergetsantry[bot]shanamatthews
authored
Getting Started: rq (#7837)
* Getting Started: rq * style(lint): Auto commit lint changes * fix * Apply suggestions from code review Co-authored-by: Shana Matthews <shana.l.matthews@gmail.com> --------- Co-authored-by: getsantry[bot] <66042841+getsantry[bot]@users.noreply.github.com> Co-authored-by: Shana Matthews <shana.l.matthews@gmail.com>
1 parent 54c1145 commit 8dff2f4

File tree

1 file changed

+81
-17
lines changed

1 file changed

+81
-17
lines changed

src/platforms/python/guides/rq/index.mdx

Lines changed: 81 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,31 @@ description: "Learn about using Sentry with RQ."
88

99
The RQ integration adds support for the [RQ Job Queue System](https://python-rq.org/).
1010

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+
1123
Create a file called `mysettings.py` with the following content:
1224

1325
<SignInNote />
1426

1527
```python {filename:mysettings.py}
28+
# mysettings.py
1629
import sentry_sdk
17-
from sentry_sdk.integrations.rq import RqIntegration
1830

1931
sentry_sdk.init(
20-
dsn="___PUBLIC_DSN___",
21-
integrations=[
22-
RqIntegration(),
23-
],
24-
2532
# Set traces_sample_rate to 1.0 to capture 100%
2633
# of transactions for performance monitoring.
27-
# We recommend adjusting this value in production,
2834
traces_sample_rate=1.0,
35+
dsn="___PUBLIC_DSN___",
2936
)
3037
```
3138

@@ -34,45 +41,102 @@ Start your worker with:
3441
```shell
3542
rq worker \
3643
-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
3845
```
3946

4047
The integration will automatically report errors from all RQ jobs.
4148

4249
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.
4350

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.
4552

4653
<SignInNote />
4754

4855
```python {filename:app.py}
56+
# app.py
4957
import sentry_sdk
50-
from sentry_sdk.integrations.flask import FlaskIntegration
51-
from sentry_sdk.integrations.rq import RqIntegration
5258

5359
sentry_sdk.init(
5460
dsn=___PUBLIC_DSN___,
55-
integrations=[
56-
FlaskIntegration(),
57-
RqIntegration(),
58-
],
59-
6061
# Set traces_sample_rate to 1.0 to capture 100%
6162
# of transactions for performance monitoring.
62-
# We recommend adjusting this value in production,
6363
traces_sample_rate=1.0,
6464
)
6565
```
6666

6767
The worker configuration `mysettings.py` then becomes:
6868

6969
```python {filename:mysettings.py}
70+
# mysettings.py
7071
# This import causes the Sentry SDK to be initialized
7172
import app
7273
```
7374

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+
def hello(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+
74133
## The `--sentry-dsn` CLI option
75134

76135
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.
77136

78137
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.
138+
139+
## Supported Versions
140+
141+
- RQ: 0.6+
142+
- Python: 2.7+ (RQ 0.6+), 3.5+ (RQ 1.4+)

0 commit comments

Comments
 (0)