Redis for Python

Learn how to set up Sentry's Redis integration and how to capture Redis commands as breadcrumbs.

You need:

  • A Sentry account and project
  • Your application up and running
  • Python 3.6+

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_pii is set to True in your app's sentry_sdk.init() call, you will also see the data used in your Redis commands.
  • Sentry will never collect data of the AUTH command.

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:

Copied
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()

Copied
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()

Copied
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:

Copied
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

Typeint
Default1024

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

Typelist[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:

Are you having problems setting up the SDK or integration?
Was this helpful?
Help improve this content
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").