OpenTelemetry aiohttp client Instrumentation

The opentelemetry-instrumentation-aiohttp-client package allows tracing HTTP requests made by the aiohttp client library.

Usage

Explicitly instrumenting a single client session:

import asyncio
import aiohttp
from opentelemetry.instrumentation.aiohttp_client import create_trace_config
import yarl

def strip_query_params(url: yarl.URL) -> str:
    return str(url.with_query(None))

async def get(url):
    async with aiohttp.ClientSession(trace_configs=[create_trace_config(
        # Remove all query params from the URL attribute on the span.
        url_filter=strip_query_params,
    )]) as session:
        async with session.get(url) as response:
            await response.text()

asyncio.run(get("https://example.com"))

Instrumenting all client sessions:

import asyncio
import aiohttp
from opentelemetry.instrumentation.aiohttp_client import (
    AioHttpClientInstrumentor
)

# Enable instrumentation
AioHttpClientInstrumentor().instrument()

# Create a session and make an HTTP get request
async def get(url):
    async with aiohttp.ClientSession() as session:
        async with session.get(url) as response:
            await response.text()

asyncio.run(get("https://example.com"))

Configuration

Request/Response hooks

Utilize request/response hooks to execute custom logic to be performed before/after performing a request.

def request_hook(span: Span, params: aiohttp.TraceRequestStartParams):
   if span and span.is_recording():
         span.set_attribute("custom_user_attribute_from_request_hook", "some-value")

def response_hook(span: Span, params: typing.Union[
             aiohttp.TraceRequestEndParams,
             aiohttp.TraceRequestExceptionParams,
         ]):
     if span and span.is_recording():
         span.set_attribute("custom_user_attribute_from_response_hook", "some-value")

AioHttpClientInstrumentor().instrument(request_hook=request_hook, response_hook=response_hook)

Exclude lists

To exclude certain URLs from tracking, set the environment variable OTEL_PYTHON_AIOHTTP_CLIENT_EXCLUDED_URLS (or OTEL_PYTHON_EXCLUDED_URLS to cover all instrumentations) to a string of comma delimited regexes that match the URLs.

For example,

export OTEL_PYTHON_AIOHTTP_CLIENT_EXCLUDED_URLS="client/.*/info,healthcheck"

will exclude requests such as https://site/client/123/info and https://site/xyz/healthcheck.

Capture HTTP request and response headers

You can configure the agent to capture specified HTTP headers as span attributes, according to the semantic conventions.

Request headers

To capture HTTP request headers as span attributes, set the environment variable OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_CLIENT_REQUEST to a comma delimited list of HTTP header names.

For example using the environment variable,

export OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_CLIENT_REQUEST="content-type,custom_request_header"

will extract content-type and custom_request_header from the request headers and add them as span attributes.

Request header names in aiohttp are case-insensitive. So, giving the header name as CUStom-Header in the environment variable will capture the header named custom-header.

Regular expressions may also be used to match multiple headers that correspond to the given pattern. For example:

export OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_CLIENT_REQUEST="Accept.*,X-.*"

Would match all request headers that start with Accept and X-.

To capture all request headers, set OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_CLIENT_REQUEST to ".*".

export OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_CLIENT_REQUEST=".*"

The name of the added span attribute will follow the format http.request.header.<header_name> where <header_name> is the normalized HTTP header name (lowercase, with - replaced by _). The value of the attribute will be a single item list containing all the header values.

For example: http.request.header.custom_request_header = ["<value1>", "<value2>"]

Response headers

To capture HTTP response headers as span attributes, set the environment variable OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_CLIENT_RESPONSE to a comma delimited list of HTTP header names.

For example using the environment variable,

export OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_CLIENT_RESPONSE="content-type,custom_response_header"

will extract content-type and custom_response_header from the response headers and add them as span attributes.

Response header names in aiohttp are case-insensitive. So, giving the header name as CUStom-Header in the environment variable will capture the header named custom-header.

Regular expressions may also be used to match multiple headers that correspond to the given pattern. For example:

export OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_CLIENT_RESPONSE="Content.*,X-.*"

Would match all response headers that start with Content and X-.

To capture all response headers, set OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_CLIENT_RESPONSE to ".*".

export OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_CLIENT_RESPONSE=".*"

The name of the added span attribute will follow the format http.response.header.<header_name> where <header_name> is the normalized HTTP header name (lowercase, with - replaced by _). The value of the attribute will be a list containing the header values.

For example: http.response.header.custom_response_header = ["<value1>", "<value2>"]

Sanitizing headers

In order to prevent storing sensitive data such as personally identifiable information (PII), session keys, passwords, etc, set the environment variable OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SANITIZE_FIELDS to a comma delimited list of HTTP header names to be sanitized.

Regexes may be used, and all header names will be matched in a case-insensitive manner.

For example using the environment variable,

export OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SANITIZE_FIELDS=".*session.*,set-cookie"

will replace the value of headers such as session-id and set-cookie with [REDACTED] in the span.

Note

The environment variable names used to capture HTTP headers are still experimental, and thus are subject to change.

API

opentelemetry.instrumentation.aiohttp_client.create_trace_config(url_filter=None, request_hook=None, response_hook=None, tracer_provider=None, meter_provider=None, sem_conv_opt_in_mode=_StabilityMode.DEFAULT, captured_request_headers=None, captured_response_headers=None, sensitive_headers=None)[source]

Create an aiohttp-compatible trace configuration.

One span is created for the entire HTTP request, including initial TCP/TLS setup if the connection doesn’t exist.

By default the span name is set to the HTTP request method.

Example usage:

import aiohttp
from opentelemetry.instrumentation.aiohttp_client import create_trace_config

async with aiohttp.ClientSession(trace_configs=[create_trace_config()]) as session:
    async with session.get(url) as response:
        await response.text()
Parameters:
  • url_filter (Optional[Callable[[URL], str]]) – A callback to process the requested URL prior to adding it as a span attribute. This can be useful to remove sensitive data such as API keys or user personal information.

  • request_hook (Callable) – Optional callback that can modify span name and request params.

  • response_hook (Callable) – Optional callback that can modify span name and response params.

  • tracer_provider (Optional[TracerProvider]) – optional TracerProvider from which to get a Tracer

  • meter_provider (Optional[MeterProvider]) – optional Meter provider to use

  • captured_request_headers (Optional[list[str]]) – List of HTTP request header regexes to capture as span attributes. Header names matching these patterns will be added as span attributes with the format http.request.header.<header_name>.

  • captured_response_headers (Optional[list[str]]) – List of HTTP response header regexes to capture as span attributes. Header names matching these patterns will be added as span attributes with the format http.response.header.<header_name>.

  • sensitive_headers (Optional[list[str]]) – List of HTTP header regexes whose values should be sanitized (redacted) when captured. Header values matching these patterns will be replaced with [REDACTED].

Returns:

An object suitable for use with aiohttp.ClientSession.

Return type:

aiohttp.TraceConfig

class opentelemetry.instrumentation.aiohttp_client.AioHttpClientInstrumentor(*args, **kwargs)[source]

Bases: BaseInstrumentor

An instrumentor for aiohttp client sessions

See BaseInstrumentor

instrumentation_dependencies()[source]

Return a list of python packages with versions that the will be instrumented.

The format should be the same as used in requirements.txt or pyproject.toml.

For example, if an instrumentation instruments requests 1.x, this method should look like: :rtype: Collection[str]

def instrumentation_dependencies(self) -> Collection[str]:

return [‘requests ~= 1.0’]

This will ensure that the instrumentation will only be used when the specified library is present in the environment.

static uninstrument_session(client_session)[source]

Disables instrumentation for the given session