Skip to content

Regression test: W3C traceparent upstream context in FastAPI (issue #879) - #919

Open
CagriYonca wants to merge 1 commit into
mainfrom
test/fastapi-traceparent-regression
Open

CagriYonca wants to merge 1 commit into
mainfrom
test/fastapi-traceparent-regression

Conversation

@CagriYonca

Copy link
Copy Markdown
Contributor

What & Why

FastAPI >= 0.142.0 introduced its own telemetry middleware (fastapi/middleware/asyncexitstack.py
→ fastapi/telemetry/_asgi.py). When an incoming request carries a W3C traceparent header,
this middleware builds a native OpenTelemetry Context and passes it directly to
tracer.start_span(name, context=parent, ...).

Before the fix merged in PR #904, InstanaTracer.start_span() enforced a strict type check:

if parent_context and not isinstance(parent_context, SpanContext):
    raise TypeError("parent_context must be an Instana SpanContext or None.")

This caused every request carrying an upstream traceparent header to return HTTP 500,
silently breaking distributed trace continuity for any FastAPI service sitting behind another
instrumented service.

What this PR adds

A regression test — test_w3c_traceparent_upstream_context in
tests/frameworks/test_fastapi.py — that guards against this regression coming back.

The test:

  1. Constructs a real W3C traceparent header with a 128-bit trace ID and 64-bit span ID.
  2. Sends a GET / request via TestClient with that header.
  3. Asserts HTTP 200 — a 500 here means the TypeError is still present.
  4. Asserts that X-INSTANA-T, X-INSTANA-S, and Server-Timing response headers are
    set, confirming Instana's trace context is still propagated downstream.
  5. Asserts that the recorded ASGI span's trace_id (lower 64 bits) matches the upstream
    trace ID, preserving cross-service trace continuity.
  6. Asserts that the ASGI span's parent_id matches the upstream span ID, so the UI renders
    the correct parent–child relationship.

Test result evidence

Commit tracer.py state Test result
1f34ca7c (before fix) raise TypeError(...) ❌ FAIL — HTTP 500
main (after fix, a449cebb +) OTel → Instana SpanContext conversion ✅ PASS — HTTP 200

Scope

Related

…xt (issue [#879](#879))

Signed-off-by: Cagri Yonca <cagri@ibm.com>
@CagriYonca
CagriYonca force-pushed the test/fastapi-traceparent-regression branch from 4d0c21d to c3e0c1a Compare October 15, 2026 07:58
@CagriYonca
CagriYonca marked this pull request as ready for review October 15, 2026 09:02
@CagriYonca
CagriYonca requested a review from a team as a code owner October 15, 2026 09:02
@CagriYonca CagriYonca self-assigned this Oct 15, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant