{"id":2640,"library":"opentelemetry-instrumentation-pika","title":"OpenTelemetry Pika Instrumentation","description":"This library provides OpenTelemetry instrumentation for the `pika` Python library, enabling automatic tracing of RabbitMQ interactions. It is part of the `opentelemetry-python-contrib` project, which frequently releases beta versions, with stable releases typically occurring less often. The library requires Python 3.9 or newer.","status":"active","version":"0.62b0","language":"en","source_language":"en","source_url":"https://github.com/open-telemetry/opentelemetry-python-contrib/tree/main/instrumentation/opentelemetry-instrumentation-pika","tags":["opentelemetry","observability","tracing","pika","rabbitmq","instrumentation"],"install":[{"cmd":"pip install opentelemetry-instrumentation-pika opentelemetry-sdk pika","lang":"bash","label":"Install core, SDK, and Pika"}],"dependencies":[{"reason":"The library instruments 'pika' for RabbitMQ communication.","package":"pika","optional":false},{"reason":"Required for OpenTelemetry API and SDK functionalities.","package":"opentelemetry-sdk","optional":false}],"imports":[{"symbol":"PikaInstrumentor","correct":"from opentelemetry.instrumentation.pika import PikaInstrumentor"}],"quickstart":{"code":"import pika\nfrom opentelemetry import trace\nfrom opentelemetry.sdk.resources import Resource\nfrom opentelemetry.sdk.trace import TracerProvider\nfrom opentelemetry.sdk.trace.export import ConsoleSpanExporter, SimpleSpanProcessor\nfrom opentelemetry.instrumentation.pika import PikaInstrumentor\n\n# Configure OpenTelemetry SDK\nresource = Resource.create({\"service.name\": \"pika-test-app\"})\nprovider = TracerProvider(resource=resource)\nprocessor = SimpleSpanProcessor(ConsoleSpanExporter())\nprovider.add_span_processor(processor)\ntrace.set_tracer_provider(provider)\n\n# Instrument Pika\nPikaInstrumentor().instrument()\n\n# Pika usage (example: publish a message)\ndef send_message():\n    print(\"Sending message...\")\n    connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))\n    channel = connection.channel()\n    channel.queue_declare(queue='hello')\n    channel.basic_publish(exchange='',\n                          routing_key='hello',\n                          body=b'Hello World!')\n    print(\"Message sent.\")\n    connection.close()\n\nif __name__ == \"__main__\":\n    # Ensure RabbitMQ is running or adjust connection parameters\n    try:\n        send_message()\n        print(\"Traces should be printed to the console.\")\n    except Exception as e:\n        print(f\"Error connecting to RabbitMQ or sending message: {e}\")\n        print(\"Please ensure RabbitMQ is running and accessible at 'localhost'.\")","lang":"python","description":"This quickstart demonstrates how to instrument the Pika library to automatically generate OpenTelemetry traces. It initializes the OpenTelemetry SDK with a console exporter, instruments Pika, and then performs a basic message publish operation. Ensure a RabbitMQ instance is accessible at `localhost` for this example to run successfully."},"warnings":[{"fix":"Always place OpenTelemetry SDK setup and instrumentation calls at the very beginning of your application's entry point, before any `import pika` or related code is executed.","message":"OpenTelemetry Python SDK and instrumentations must be initialized before the instrumented library (pika) is imported or used. Importing pika before calling `PikaInstrumentor().instrument()` may result in uninstrumented operations.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Refer to the `opentelemetry-python-contrib` changelog for specific breaking changes in new releases. Pin exact versions in production environments to avoid unexpected behavior.","message":"As a beta library (`0.x.y.b0` versioning), `opentelemetry-instrumentation-pika` may introduce breaking changes to its API, configuration, or generated telemetry attributes between minor versions. While efforts are made to align with semantic conventions, these can evolve rapidly.","severity":"breaking","affected_versions":"All beta versions (e.g., < 1.0.0)"},{"fix":"For Gunicorn and similar pre-fork servers, consider using a single worker process if automatic metric collection is critical, or use programmatic instrumentation to configure an exporter per worker process. Alternatively, explore zero-code instrumentation via `opentelemetry-instrument` with specific configurations or ensure metrics are pushed via OTLP directly.","message":"Automatic instrumentation in multi-process/forking environments (e.g., Gunicorn with multiple workers) can be problematic. OpenTelemetry SDK components, especially `PeriodicExportingMetricReader`, may not function correctly across forked processes due to issues with background threads and locks.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Ensure `pika` is installed and its version meets the requirements specified by `opentelemetry-instrumentation-pika`. Check the `instrumentation_dependencies` method in the source code or `pyproject.toml` for precise version constraints.","message":"The instrumentation relies on `pika` being installed and compatible. Recent changes to dependency checks mean that if `pika` is missing or its version is outside the supported range, instrumentation might fail silently or raise an `ImportError` or `DependencyConflictError`.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-10T00:00:00.000Z","next_check":"2026-07-09T00:00:00.000Z"}