Redis for Python
Learn how to set up Sentry's Redis integration and how to capture Redis commands as breadcrumbs.
If you're using stream mode, this page's references to "transaction" should be applied to service spans instead. See Streamed Spans for more information.
You need:
Follow our quick start guide to set up Sentry in your Python application (make sure to enable tracing). Sentry's Redis integration is enabled automatically if you have the redis package installed.
The following information will be available to you on Sentry:
- Performance information about requests to Redis will be available in the waterfall diagram in the Traces section on Sentry.
- Redis commands will be added as breadcrumbs.
- If
send_default_piiis set toTruein your app'ssentry_sdk.init()call, you will also see the data used in your Redis commands. - Sentry will never collect data of the
AUTHcommand.
To verify that Sentry captures your Redis commands, add this code to your app, which will create a transaction called testing_sentry in the Traces section:
import redis
def main():
sentry_sdk.init(...) # same as above
r = redis.Redis(host='localhost', port=6379, decode_responses=True)
# or sentry_sdk.traces.start_span(name="testing_sentry", parent_span=None) in stream mode
with sentry_sdk.start_transaction(name="testing_sentry"):
r.set("foo", "bar")
r.get("foo")
main()
import redis
def main():
sentry_sdk.init(...) # same as above
r = redis.Redis(host='localhost', port=6379, decode_responses=True)
# or sentry_sdk.traces.start_span(name="testing_sentry", parent_span=None) in stream mode
with sentry_sdk.start_transaction(name="testing_sentry"):
r.set("foo", "bar")
r.get("foo")
main()
from redis.cluster import RedisCluster
def main():
sentry_sdk.init(...) # same as above
rc = RedisCluster(host='localhost', port=16379)
# or sentry_sdk.traces.start_span(name="testing_sentry", parent_span=None) in stream mode
with sentry_sdk.start_transaction(name="testing_sentry"):
rc.set("foo", "bar")
rc.get("foo")
main()
from redis.cluster import RedisCluster
def main():
sentry_sdk.init(...) # same as above
rc = RedisCluster(host='localhost', port=16379)
# or sentry_sdk.traces.start_span(name="testing_sentry", parent_span=None) in stream mode
with sentry_sdk.start_transaction(name="testing_sentry"):
rc.set("foo", "bar")
rc.get("foo")
main()
import asyncio
import redis.asyncio as redis
async def main():
sentry_sdk.init(...) # same as above
r = redis.Redis(host='localhost', port=6379)
# or sentry_sdk.traces.start_span(name="testing_sentry", parent_span=None) in stream mode
with sentry_sdk.start_transaction(name="testing_sentry"):
await r.set("foo", "bar")
await r.get("foo")
asyncio.run(main())
import asyncio
import redis.asyncio as redis
async def main():
sentry_sdk.init(...) # same as above
r = redis.Redis(host='localhost', port=6379)
# or sentry_sdk.traces.start_span(name="testing_sentry", parent_span=None) in stream mode
with sentry_sdk.start_transaction(name="testing_sentry"):
await r.set("foo", "bar")
await r.get("foo")
asyncio.run(main())
Add RedisIntegration explicitly to your sentry_sdk.init() call to set options for RedisIntegration to change its behavior:
import sentry_sdk
from sentry_sdk.integrations.redis import RedisIntegration
sentry_sdk.init(
# ...
integrations=[
RedisIntegration(
max_data_size=None,
cache_prefixes=["mycache", "template.cache"],
),
],
)
import sentry_sdk
from sentry_sdk.integrations.redis import RedisIntegration
sentry_sdk.init(
# ...
integrations=[
RedisIntegration(
max_data_size=None,
cache_prefixes=["mycache", "template.cache"],
),
],
)
You can pass the following keyword arguments to RedisIntegration():
max_data_size
| Type | int |
|---|---|
| Default | 1024 |
Set max_data_size to an integer to control how many characters should be collected. When you set this option to a value that evaluates to False (like 0 or None), no trimming will take place, and the entire Redis command will be recorded.
By default, the integration trims data collected after 1024 characters.
cache_prefixes
| Type | list[string] |
|---|
Specify a list of prefixes for Redis keys to define a key space to consider as cache. Cache keys will show up in the cache-monitoring dashboard, giving you more insight into your caching strategy.
For example, if you set cache_prefixes=["template.cache", "middleware.cache"], then access to all Redis keys starting with template.cache or middleware.cache will show up in your cache-monitoring dashboard.
At this point, you should have integrated Sentry into your application and should already be sending data to your Sentry project.
Now's a good time to customize your setup and look into more advanced topics. Our next recommended steps for you are:
- Explore practical guides on what to monitor, log, track, and investigate after setup
- Continue to customize your configuration
- Learn more about manually capturing errors or messages
- Dive straight into the API with our API docs
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").