{"library":"opentelemetry-instrumentation-redis","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`.","tag":null,"tag_description":null,"last_tested":"2026-04-24","results":[{"runtime":"python:3.10-alpine","exit_code":1},{"runtime":"python:3.10-slim","exit_code":1},{"runtime":"python:3.11-alpine","exit_code":1},{"runtime":"python:3.11-slim","exit_code":1},{"runtime":"python:3.12-alpine","exit_code":1},{"runtime":"python:3.12-slim","exit_code":1},{"runtime":"python:3.13-alpine","exit_code":1},{"runtime":"python:3.13-slim","exit_code":1},{"runtime":"python:3.9-alpine","exit_code":1},{"runtime":"python:3.9-slim","exit_code":1}]}