agentserver: Extract incoming traceparent header for distributed trace propagation#45642
Open
singankit wants to merge 2 commits intolusu/agentserver-1110from
Open
agentserver: Extract incoming traceparent header for distributed trace propagation#45642singankit wants to merge 2 commits intolusu/agentserver-1110from
singankit wants to merge 2 commits intolusu/agentserver-1110from
Conversation
Contributor
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
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).
Contributor
Author
There was a problem hiding this comment.
Test are added as part of this commit eb0143e
sdk/agentserver/azure-ai-agentserver-core/azure/ai/agentserver/core/server/base.py
Outdated
Show resolved
Hide resolved
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
eb0143e to
f4dccc7
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
request.headers via TraceContextTextMapPropagator().extract() and pass the resulting context to start_as_current_span(context=parent_ctx).