Redis

Learn about importing the Redis integration.

The Redis integration hooks into the Redis client for Python and logs all Redis commands as breadcrumbs.

Copied
pip install --upgrade 'sentry-sdk'

If you have the redis Python package in your dependencies, the Redis integration will be enabled automatically.

Configuration should happen as early as possible in your application's lifecycle.

Copied
import sentry_sdk

sentry_sdk.init(
    dsn="https://examplePublicKey@o0.ingest.sentry.io/0",
    # Set traces_sample_rate to 1.0 to capture 100%
    # of transactions for tracing.
    traces_sample_rate=1.0,
    # Set profiles_sample_rate to 1.0 to profile 100%
    # of sampled transactions.
    # We recommend adjusting this value in production.
    profiles_sample_rate=1.0,
)

Copied
import redis

def main():
    sentry_sdk.init(...)  # same as above
    r = redis.Redis(host='localhost', port=6379, decode_responses=True)

    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)

    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
    rc = RedisCluster(host='localhost', port=16379)

    with sentry_sdk.start_transaction(name="testing_sentry"):
        rc.set("foo", "bar")
        rc.get("foo")

asyncio.run(main())

These examples will create a transaction called testing_sentry in the Performance section of sentry.io, and create spans for the redis commands.

It takes a couple of moments for the data to appear in sentry.io.

With Redis integration the following information will be available to you on Sentry.io:

  • Performance information about requests to redis will be available in the waterfall diagram in the Performance section on Sentry.io.
  • Redis commands will be added as breadcrumbs.
  • If send_default_pii is set to True you will also see the data used in your redis commands.
  • Data of the AUTH command will never be collected.

By adding RedisIntegration explicitly to your sentry_sdk.init() call you can 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

By default RedisIntegration() will trim data collected after 1024 characters. You can change this behavior with the max_data_size parameter:

You can set max_data_size to an integer to control how many characters should be collected.

When you set max_data_size to a value that evaluates to False (like 0 or None), no trimming will take place. The whole Redis command will be recorded.

  • cache_prefixes

You can specify a list of prefixes to Redis keys to define a key space that should be considered 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 to ["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.

  • Python: 3.6+

The versions above apply for Sentry Python SDK version 2.0+, which drops support for some legacy Python and framework versions. If you're looking to use Sentry with older Python or framework versions, consider using an SDK version from the 1.x major line of releases.

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").