Redis
The Redis integration hooks into the Redis client for Python and logs all Redis commands as breadcrumbs.
Install
pip install --upgrade 'sentry-sdk'
Configure
If you have the redis
Python package in your dependencies, the Redis integration will be enabled automatically. There is nothing to do for you except initializing the Sentry SDK.
import sentry_sdk
from sentry_sdk.integrations.redis import RedisIntegration
sentry_sdk.init(
dsn='https://examplePublicKey@o0.ingest.sentry.io/0',
# Set traces_sample_rate to 1.0 to capture 100%
# of transactions for performance monitoring.
traces_sample_rate=1.0,
)
The Redis integration is enabled automatically if you have the redis
package installed.
Verify
Standard Redis
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
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
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.
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 toTrue
you will also see the data used in your redis commands. - Data of the
AUTH
command will never be collected.
Integration Options
If you want to change the default behavior of the Redis integration, you need to instantiate the integration manually to pass options to the integration and then pass it to Sentry's init function.
max_data_size
By default RedisIntegration()
will trim data collected after 1024
characters. You can change this behavior with the max_data_size
parameter:
- When you set
max_data_size
to a value that evaluates toFalse
(like0
orNone
) no trimming will take place. - You can set
max_data_size
to an integer to control how many characters should be collected.
Example for disabling data trimming, the whole Redis command will be recorded:
import sentry_sdk
from sentry_sdk.integrations.redis import RedisIntegration
sentry_sdk.init(
dsn='https://examplePublicKey@o0.ingest.sentry.io/0',
integrations=[
RedisIntegration(max_data_size=None),
]
)
Example of keeping just a maximum of 50 characters of Redis commands:
import sentry_sdk
from sentry_sdk.integrations.redis import RedisIntegration
sentry_sdk.init(
dsn='https://examplePublicKey@o0.ingest.sentry.io/0',
integrations=[
RedisIntegration(max_data_size=50),
]
)
Supported Versions
- Python: 2.7+
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) to suggesting an update ("yeah, this would be better").
- Package:
- pypi:sentry-sdk
- Version:
- 1.31.0
- Repository:
- https://github.com/getsentry/sentry-python