The `opentelemetry-instrumentation-aiohttp-client` package enables distributed tracing for HTTP requests made using the `aiohttp` client library. It is part of the `opentelemetry-python-contrib` project, currently at version `0.61b0`, and follows a monthly release cadence, aligned with the broader OpenTelemetry Python Contrib releases.
pip install opentelemetry-instrumentation-aiohttp-client aiohttpVerified import paths — ran on the pinned version, not inferred.
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.
Consult the official OpenTelemetry Python Contrib CHANGELOG and documentation for each upgrade, especially for changes to semantic conventions.
Always initialize your OpenTelemetry SDK at the earliest possible point in your application's lifecycle, typically at startup, before any instrumented libraries are used or configured.
Regularly review the OpenTelemetry Python Contrib documentation and the OpenTelemetry semantic conventions specification for HTTP to stay updated on current and deprecated attributes. Consider using schema URL configuration if supported by your collector.
To enable header capture, set environment variables like `OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_CLIENT_REQUEST='Content-Type,X-Custom-Header'` and `OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_CLIENT_RESPONSE='Content-Type,Server'`. To redact sensitive header values, use `OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SANITIZE_FIELDS='Authorization,Cookie,Set-Cookie'` with a comma-delimited list of header names/regex patterns.
Set the environment variable `OTEL_PYTHON_AIOHTTP_CLIENT_EXCLUDED_URLS` (specific to aiohttp client) or `OTEL_PYTHON_EXCLUDED_URLS` (global for Python instrumentations) to a comma-delimited string of regular expressions that match the URLs to be excluded (e.g., `'^.*/healthcheck,^.*/metrics'`).
Install the OpenTelemetry SDK package: `pip install opentelemetry-sdk`
Ensure `opentelemetry-sdk` is included in your project's dependencies (e.g., `pip install opentelemetry-sdk opentelemetry-api` or listed in `requirements.txt`). This package is distinct from `opentelemetry-instrumentation-aiohttp-client`.