Redis
See every command your app sends to Redis (which command ran, how long it took, and which ones failed) as a span (one unit of work with a name, a start, and a duration) in Logfire. Related spans link together into a trace (the full journey of one request), so a slow lookup shows up right next to the code that triggered it.
- Each command as a span, with its duration and any errors
- Which Redis server the command went to
- Optionally, the command itself (off by default; see below)
You’ll need a Logfire project. Open Add data in your project (top navigation) and follow the
setup for your language: it signs your machine in with logfire auth (a browser sign-in, no token
to copy) and, for production or other languages, creates a write token (the credential your app
uses to send data). New to Logfire? Start with Getting Started.
Install logfire with the redis extra:
pip install 'logfire[redis]'
uv add 'logfire[redis]'
Add two lines to your app: logfire.configure() to connect to your project, and
logfire.instrument_redis() to record every command.
The example below connects to a local Redis server. If you don’t have one running, you can start one with Docker:
docker run --name redis -p 127.0.0.1:6379:6379 -d redis:latest
import redis
import logfire
logfire.configure()
logfire.instrument_redis()
client = redis.StrictRedis(host='localhost', port=6379)
client.set('my-key', 'my-value')
async def main():
client = redis.asyncio.Redis(host='localhost', port=6379)
await client.get('my-key')
if __name__ == '__main__':
import asyncio
asyncio.run(main())
Run it with python main.py.
Run your program, then open your project in the Logfire web app and go to the Live view. Within a few seconds you should see a span for each command the script ran. Click one to see how long it took.
Not seeing your commands in Logfire? Check these first:
logfire.configure()runs beforelogfire.instrument_redis(). Configure the connection first, then instrument.- You call
instrument_redis()exactly once. - Your write token is set. In local development, run
logfire projects use <your-project>; in production, set theLOGFIRE_TOKENenvironment variable. See Getting Started. - You actually ran a command. Spans appear only after a command executes.
By default, the command sent to Redis isn’t recorded, since it can contain sensitive data. To include
it, pass capture_statement=True:
import logfire
logfire.configure()
logfire.instrument_redis(capture_statement=True)
Turning this on sends the command (including any values in it) to Logfire, so avoid it if your commands carry secrets or personally identifiable information (PII).
logfire.instrument_redis() accepts additional keyword arguments
and passes them to the OpenTelemetry Redis instrumentation. See
their documentation for the full list.
- Underlying OpenTelemetry package: Redis instrumentation
def instrument_redis(
capture_statement: bool = False,
request_hook: RedisRequestHook | None = None,
response_hook: RedisResponseHook | None = None,
**kwargs: Any,
) -> None
Instrument the redis module so that spans are automatically created for each operation.
Uses the
OpenTelemetry Redis Instrumentation
library, specifically RedisInstrumentor().instrument(), to which it passes **kwargs.
capture_statement : bool Default: False
Set to True to capture the statement in the span attributes.
request_hook : RedisRequestHook | None Default: None
A function that is called before performing the request.
response_hook : RedisResponseHook | None Default: None
A function that is called after receiving the response.
**kwargs : Any Default: {}
Additional keyword arguments to pass to the OpenTelemetry instrument methods for future compatibility.
Bases: Protocol
A hook that is called before the request is sent.
def __call__(span: Span, instance: Connection, *args: Any, **kwargs: Any) -> None
Call the hook.
The span that is being created.
The connection instance.
*args : Any Default: ()
The arguments that are passed to the command.
**kwargs : Any Default: {}
The keyword arguments that are passed to the command.
Bases: Protocol
A hook that is called after the response is received.
def __call__(span: Span, instance: Connection, response: Any) -> None
Call the hook.
The span that is being created.
The connection instance.
response : Any
The response that is received.