Skip to content

agentserver: Extract incoming traceparent header for distributed trace propagation#45642

Open
singankit wants to merge 2 commits intolusu/agentserver-1110from
fix/agentserver-trace-context-propagation
Open

agentserver: Extract incoming traceparent header for distributed trace propagation#45642
singankit wants to merge 2 commits intolusu/agentserver-1110from
fix/agentserver-trace-context-propagation

Conversation

@singankit
Copy link
Contributor

@singankit singankit commented Mar 11, 2026

Description

The hosted agent server now extracts the W3C trace context (\ raceparent\ header) from incoming HTTP requests and uses it as the parent context for the root \HostedAgents\ server span.

This enables end-to-end distributed tracing where a caller's client span is the parent of the server span, producing a single connected trace in Application Insights / any OTel backend.

When no \ raceparent\ header is present, behavior is unchanged — the server span becomes the root of a new trace.

Changes

  • /azure-ai-agentserver-core/azure/ai/agentserver/core/server/base.py: Extract traceparent from
    request.headers via TraceContextTextMapPropagator().extract() and pass the resulting context to start_as_current_span(context=parent_ctx).

Copilot AI review requested due to automatic review settings March 11, 2026 05:58
@github-actions github-actions bot added the Hosted Agents sdk/agentserver/* label Mar 11, 2026
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds distributed trace propagation support to the hosted agent server by extracting incoming W3C trace context and using it as the parent for the root server span, enabling end-to-end traces across caller and server.

Changes:

  • Extract incoming W3C trace context from HTTP request headers via TraceContextTextMapPropagator().extract(...).
  • Start the HostedAgents-* server span with the extracted context as its parent.

Comment on lines +90 to +100
# Extract W3C trace context from incoming request headers so the
# server span becomes a child of the caller's span when a
# traceparent header is present.
parent_ctx = TraceContextTextMapPropagator().extract(
carrier=dict(request.headers)
)
with self.tracer.start_as_current_span(
name=f"HostedAgents-{context.response_id}",
attributes=ctx,
kind=trace.SpanKind.SERVER,
context=parent_ctx,
Copy link

Copilot AI Mar 11, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new distributed tracing behavior (extracting W3C context from incoming headers and using it as the parent span context) is not covered by tests. Consider adding a test that sends a request with a known traceparent header and asserts the created server span uses that trace/span as its parent (e.g., via an in-memory exporter or a test span processor).

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test are added as part of this commit eb0143e

The hosted agent server now extracts the W3C trace context (traceparent
header) from incoming HTTP requests and uses it as the parent context for
the root HostedAgents server span. This enables end-to-end distributed
tracing where the caller's client span is the parent of the server span.

When no traceparent header is present, behavior is unchanged — the server
span becomes the root of a new trace.
…n tests

- Use request.headers as carrier instead of dict(request.headers) since
  Starlette Headers is already a valid mapping type
- Add unit tests for W3C trace context propagation:
  - Server span becomes child when traceparent header is present
  - Server span is root when no traceparent header is present
  - Validates lowercase header normalization works correctly
@singankit singankit force-pushed the fix/agentserver-trace-context-propagation branch from eb0143e to f4dccc7 Compare March 11, 2026 21:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Hosted Agents sdk/agentserver/*

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants