Skip to content

StarletteIASTelemetryMiddleware does not propagate W3C baggage headers into OTel context #87

@hoperawr

Description

@hoperawr

Describe the Problem

When using auto_instrument(middlewares=[StarletteIASTelemetryMiddleware(...)]), W3C baggage headers from incoming HTTP requests are not visible on spans as baggage is not extracted from the request headers into the OTel context.

Propose a Solution

In _IASMiddleware.dispatch(), extract and attach the OTel context.

from opentelemetry import context as otel_context, propagate

async def dispatch(self, request: Request, call_next: Any) -> Response:
    token = self._attrs_var.set(_extract_ias_attrs(request))
    ctx = propagate.extract(request.headers)
    ctx_token = otel_context.attach(ctx)
    try:
        return await call_next(request)
    finally:
        otel_context.detach(ctx_token)
        self._attrs_var.reset(token)

Describe Alternatives

Users would need to separately instrument their server.

Affected Development Phase

Development

Impact

Inconvenience

Timeline

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions