Redis

Learn about importing the Redis integration.

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

Install

Copied
pip install "sentry-sdk"

Configure

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

Copied
import sentry_sdk

sentry_sdk.init(
    dsn="___PUBLIC_DSN___",
    # Add data like request headers and IP for users, if applicable;
    # see https://docs.sentry.io/platforms/python/data-management/data-collected/ for more info
    send_default_pii=True,
    # ___PRODUCT_OPTION_START___ performance
    # Set traces_sample_rate to 1.0 to capture 100%
    # of transactions for tracing.
    traces_sample_rate=1.0,
    # ___PRODUCT_OPTION_END___ performance
    # ___PRODUCT_OPTION_START___ profiling
    # To collect profiles for all profile sessions,
    # set `profile_session_sample_rate` to 1.0.
    profile_session_sample_rate=1.0,
    # Profiles will be automatically collected while
    # there is an active span.
    profile_lifecycle="trace",
    # ___PRODUCT_OPTION_END___ profiling
    # ___PRODUCT_OPTION_START___ logs

    # Enable logs to be sent to Sentry
    enable_logs=True,
    # ___PRODUCT_OPTION_END___ logs
)

Verify

Standard Redis

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

Redis Cluster

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

Async Redis client

Copied
import asyncio
import redis.asyncio as redis

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

    with sentry_sdk.start_transaction(name="testing_sentry"):
        await r.set("foo", "bar")
        await r.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.

Behavior

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.

Options

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.

Supported Versions

  • Python: 3.6+

The versions above apply for the current major version of the Python SDK. If you're looking to use Sentry with older Python or framework versions, consider using an older major version of the SDK.

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