{"library":"opentelemetry-instrumentation-redis","title":"OpenTelemetry Redis Instrumentation","description":"The `opentelemetry-instrumentation-redis` library provides automatic instrumentation for the Python `redis` client, capturing Redis commands as OpenTelemetry spans. It is part of the `opentelemetry-python-contrib` project and is currently in a beta state, with the latest version being 0.61b0. New versions are released as part of the broader OpenTelemetry Python Contrib release cadence.","status":"active","version":"0.61b0","language":"en","source_language":"en","source_url":"https://github.com/open-telemetry/opentelemetry-python-contrib/tree/main/instrumentation/opentelemetry-instrumentation-redis","tags":["opentelemetry","observability","tracing","instrumentation","redis","distributed-tracing","python"],"install":[{"cmd":"pip install opentelemetry-instrumentation-redis redis","lang":"bash","label":"Install library and Redis client"}],"dependencies":[{"reason":"This instrumentation library wraps the `redis` Python client, so `redis` must be installed separately.","package":"redis","optional":false},{"reason":"Core OpenTelemetry API for defining telemetry.","package":"opentelemetry-api","optional":false},{"reason":"OpenTelemetry SDK for processing and exporting telemetry.","package":"opentelemetry-sdk","optional":false}],"imports":[{"symbol":"RedisInstrumentor","correct":"from opentelemetry.instrumentation.redis import RedisInstrumentor"},{"note":"Used to temporarily disable instrumentation for specific code blocks.","symbol":"suppress_instrumentation","correct":"from opentelemetry.instrumentation.utils import suppress_instrumentation"}],"quickstart":{"code":"import os\nfrom opentelemetry import trace\nfrom opentelemetry.sdk.resources import Resource, SERVICE_NAME\nfrom opentelemetry.sdk.trace import TracerProvider\nfrom opentelemetry.sdk.trace.export import BatchSpanProcessor, ConsoleSpanExporter\nfrom opentelemetry.instrumentation.redis import RedisInstrumentor\nimport redis\n\n# 1. Configure OpenTelemetry TracerProvider\nresource = Resource.create({\"service.name\": os.environ.get('OTEL_SERVICE_NAME', 'redis-app')})\ntracer_provider = TracerProvider(resource=resource)\ntracer_provider.add_span_processor(\n    BatchSpanProcessor(ConsoleSpanExporter())\n)\ntrace.set_tracer_provider(tracer_provider)\n\n# 2. Instrument Redis\nRedisInstrumentor().instrument()\n\n# 3. Use Redis client (operations will be traced)\ntry:\n    client = redis.StrictRedis(host=os.environ.get('REDIS_HOST', 'localhost'), port=int(os.environ.get('REDIS_PORT', 6379)), db=0)\n    client.ping()\n    print(\"Successfully connected to Redis.\")\n    client.set(\"mykey\", \"myvalue\")\n    value = client.get(\"mykey\")\n    print(f\"Retrieved from Redis: {value.decode('utf-8')}\")\n    client.delete(\"mykey\")\nexcept redis.exceptions.ConnectionError as e:\n    print(f\"Could not connect to Redis: {e}. Please ensure Redis is running.\")\n\n# Example of suppressing instrumentation for a specific call\n# from opentelemetry.instrumentation.utils import suppress_instrumentation\n# with suppress_instrumentation():\n#     client.get(\"untraced-key\")","lang":"python","description":"This quickstart demonstrates how to set up OpenTelemetry with the Redis instrumentation. It configures a simple `TracerProvider` with a `ConsoleSpanExporter` (for demonstration purposes), then enables Redis instrumentation globally using `RedisInstrumentor().instrument()`. Any subsequent Redis client operations will generate spans. Ensure a Redis server is running at `localhost:6379` or configure environment variables `REDIS_HOST` and `REDIS_PORT`."},"warnings":[{"fix":"Monitor `opentelemetry-python-contrib` releases for stable versions and breaking changes. Review your code when upgrading to newer beta versions.","message":"This instrumentation library is currently in beta. The API and behavior may change in future versions, and it should generally not be used in production environments without careful consideration.","severity":"beta","affected_versions":"All versions up to 0.61b0"},{"fix":"For granular control, explicitly call `instrument_client()` on the desired `redis.Redis` or `redis.StrictRedis` objects. Alternatively, use `suppress_instrumentation()` for operations that should not be traced.","message":"By default, `RedisInstrumentor().instrument()` instruments *all* `redis` client instances created *after* the `instrument()` call. If you only want to instrument specific Redis clients, use `RedisInstrumentor().instrument_client(client_instance)` instead.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Review the configuration options for `db.statement` serialization (if available, consult documentation for specific options) or implement custom hooks to further filter or mask sensitive data before it's recorded in spans.","message":"The instrumentation records Redis commands in the `db.statement` attribute. While efforts are made to obfuscate sensitive arguments by default, be cautious about logging or exposing this attribute, especially if sensitive data could be part of Redis commands.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Regularly benchmark your application with and without OpenTelemetry instrumentation. Use sampling strategies to reduce the volume of traces if overhead is a concern.","message":"OpenTelemetry auto-instrumentation adds performance overhead. While generally optimized, it's crucial to benchmark your application with instrumentation enabled to understand its impact on CPU and memory usage, especially in high-throughput scenarios.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-06T00:00:00.000Z","next_check":"2026-07-05T00:00:00.000Z"}