OpenTelemetry aiohttp server Integration
This library allows tracing HTTP requests made by the aiohttp server library.
Installation
pip install opentelemetry-instrumentation-aiohttp-server
The opentelemetry-instrumentation-aiohttp-server package allows tracing HTTP requests made by the aiohttp server library.
Usage
from aiohttp import web
from opentelemetry.instrumentation.aiohttp_server import (
AioHttpServerInstrumentor
)
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.sampling import ParentBased, TraceIdRatioBased
# Optional: configure non-default TracerProvider, resource, sampler
resource = Resource(attributes={"service.name": "my-aiohttp-service"})
sampler = ParentBased(root=TraceIdRatioBased(rate=0.25)) # sample 25% of traces
AioHttpServerInstrumentor().instrument(tracer_provider=TracerProvider(resource=resource, sampler=sampler))
async def hello(request):
return web.Response(text="Hello, world")
app = web.Application()
app.add_routes([web.get('/', hello)])
web.run_app(app)
Configuration
Exclude lists
To exclude certain URLs from tracking, set the environment variable OTEL_PYTHON_AIOHTTP_SERVER_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_SERVER_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_SERVER_REQUEST to a comma delimited list of HTTP header names.
For example,
export OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_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_SERVER_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_SERVER_REQUEST to ".*".
export OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_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
list containing 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_SERVER_RESPONSE to a comma delimited list of HTTP header names.
For example,
export OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_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_SERVER_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_SERVER_RESPONSE to ".*".
export OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_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,
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_server.get_default_span_name(request)[source]
Returns the span name. :type request:
Request:param request: the request object itself.- Return type:
- Returns:
The span name as “{method} {canonical_name}” of a resource if possible or just “{method}”.
- opentelemetry.instrumentation.aiohttp_server.collect_request_attributes(request, sem_conv_opt_in_mode=_StabilityMode.DEFAULT)[source]
Collects HTTP request attributes from the aiohttp request context and returns a dictionary to be used as span creation attributes.
- Return type:
- opentelemetry.instrumentation.aiohttp_server.set_status_code(span, status_code, duration_attrs=None, sem_conv_opt_in_mode=_StabilityMode.DEFAULT)[source]
Adds HTTP response attributes to span using the status_code argument.
- Return type:
- class opentelemetry.instrumentation.aiohttp_server.AiohttpGetter[source]
Bases:
GetterExtract current trace from headers
- opentelemetry.instrumentation.aiohttp_server.create_aiohttp_middleware(tracer_provider=None)[source]
- async opentelemetry.instrumentation.aiohttp_server.middleware(request, handler)
Middleware for aiohttp implementing tracing logic
- opentelemetry.instrumentation.aiohttp_server.create_instrumented_application(tracer_provider=None)[source]
- class opentelemetry.instrumentation.aiohttp_server.AioHttpServerInstrumentor(*args, **kwargs)[source]
Bases:
BaseInstrumentorAn instrumentor for aiohttp.web.Application
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:
- 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.