{"library":"opentelemetry-instrumentation-aiohttp-client","code":"import asyncio\nimport aiohttp\nfrom opentelemetry import trace\nfrom opentelemetry.sdk.trace import TracerProvider\nfrom opentelemetry.sdk.trace.export import ConsoleSpanExporter, BatchSpanProcessor\nfrom opentelemetry.instrumentation.aiohttp_client import AioHttpClientInstrumentor\n\n# 1. Initialize OpenTelemetry SDK (must happen before instrumenting libraries)\nprovider = TracerProvider()\nprocessor = BatchSpanProcessor(ConsoleSpanExporter()) # Use OTLPSpanExporter for real usage\nprovider.add_span_processor(processor)\ntrace.set_tracer_provider(provider)\n\n# 2. Instrument aiohttp client\nAioHttpClientInstrumentor().instrument()\n\nasync def fetch_url(session, url):\n    async with session.get(url) as response:\n        # Accessing response.status and response.text() will also be part of the trace\n        print(f\"Fetched {url} with status {response.status}\")\n        return await response.text()\n\nasync def main():\n    print(\"Making a traced aiohttp client request...\")\n    async with aiohttp.ClientSession() as session:\n        # This request will be automatically traced\n        await fetch_url(session, \"http://httpbin.org/get?param=value\")\n    print(\"Request complete.\")\n\nif __name__ == \"__main__\":\n    # Ensure an event loop is running for aiohttp\n    asyncio.run(main())\n","lang":"python","description":"This example demonstrates how to set up the OpenTelemetry SDK with a console exporter and then automatically instrument all `aiohttp.ClientSession` requests. The `AioHttpClientInstrumentor().instrument()` call should occur after the SDK is initialized to ensure proper tracing.","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}]}