OpenTelemetry aiohttp client Integration

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

Usage

import aiohttp
from opentelemetry.ext.aiohttp_client import (
    create_trace_config,
    url_path_span_name
)
import yarl

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

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,
        # Use the URL's path as the span name.
        span_name=url_path_span_name
)]) as session:
    async with session.get(url) as response:
        await response.text()
opentelemetry.ext.aiohttp_client.url_path_span_name(params)[source]

Extract a span name from the request URL path.

A simple callable to extract the path portion of the requested URL for use as the span name.

Parameters

params (aiohttp.TraceRequestStartParams) – Parameters describing the traced request.

Returns

The URL path.

Return type

str

opentelemetry.ext.aiohttp_client.create_trace_config(url_filter=None, span_name=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.ext.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[[str], 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.

  • span_name (str) – Override the default span name.

Returns

An object suitable for use with aiohttp.ClientSession.

Return type

aiohttp.TraceConfig