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 aiohttp
from opentelemetry.instrumentation.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()
Instrumenting all client sessions:
import aiohttp
from opentelemetry.instrumentation.aiohttp_client import (
AioHttpClientInstrumentor
)
# Enable instrumentation
AioHttpClientInstrumentor().instrument()
# Create a session and make an HTTP get request
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
await response.text()
API¶
-
opentelemetry.instrumentation.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
-
opentelemetry.instrumentation.aiohttp_client.
create_trace_config
(url_filter=None, span_name=None, tracer_provider=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
[[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.
tracer_provider (
Optional
[TracerProvider
]) – optional TracerProvider from which to get a Tracer
- Returns
An object suitable for use with
aiohttp.ClientSession
.- Return type
-
class
opentelemetry.instrumentation.aiohttp_client.
AioHttpClientInstrumentor
[source]¶ Bases:
opentelemetry.instrumentation.instrumentor.BaseInstrumentor
An instrumentor for aiohttp client sessions
See
BaseInstrumentor