-
Notifications
You must be signed in to change notification settings - Fork 3.3k
agentserver: Extract incoming traceparent header for distributed trace propagation #45642
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
singankit
wants to merge
2
commits into
lusu/agentserver-1110
Choose a base branch
from
fix/agentserver-trace-context-propagation
base: lusu/agentserver-1110
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
116 changes: 116 additions & 0 deletions
116
sdk/agentserver/azure-ai-agentserver-core/tests/test_trace_context_propagation.py
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| """Unit tests for W3C trace context propagation in the hosted agent server.""" | ||
|
|
||
| import pytest | ||
| from opentelemetry import trace, context | ||
| from opentelemetry.sdk.trace import TracerProvider | ||
| from opentelemetry.sdk.trace.export import SimpleSpanProcessor, SpanExporter, SpanExportResult | ||
| from opentelemetry.trace.propagation.tracecontext import TraceContextTextMapPropagator | ||
|
|
||
|
|
||
| class ListSpanExporter(SpanExporter): | ||
| """A minimal span exporter that collects finished spans into a list.""" | ||
|
|
||
| def __init__(self): | ||
| self.spans = [] | ||
|
|
||
| def export(self, spans): | ||
| self.spans.extend(spans) | ||
| return SpanExportResult.SUCCESS | ||
|
|
||
| def shutdown(self): | ||
| pass | ||
|
|
||
| def clear(self): | ||
| self.spans.clear() | ||
|
|
||
|
|
||
| # Module-level provider (OTel only allows setting the global provider once) | ||
| _exporter = ListSpanExporter() | ||
| _provider = TracerProvider() | ||
| _provider.add_span_processor(SimpleSpanProcessor(_exporter)) | ||
| trace.set_tracer_provider(_provider) | ||
|
|
||
|
|
||
| @pytest.fixture(autouse=True) | ||
| def _clear_spans(): | ||
| """Clear collected spans before each test.""" | ||
| _exporter.clear() | ||
| yield | ||
|
|
||
|
|
||
| class TestTraceContextPropagation: | ||
| """Tests for W3C traceparent header extraction in runs_endpoint.""" | ||
|
|
||
| def _make_traceparent(self): | ||
| """Create a traceparent header from a fresh span context.""" | ||
| tracer = trace.get_tracer("test-client") | ||
| with tracer.start_as_current_span("client-call") as span: | ||
| ctx = span.get_span_context() | ||
| carrier = {} | ||
| TraceContextTextMapPropagator().inject(carrier) | ||
| return carrier["traceparent"], ctx.trace_id, ctx.span_id | ||
|
|
||
| def test_server_span_parents_to_incoming_traceparent(self): | ||
| """When a request carries a traceparent header, the server span must | ||
| become a child of that trace (same trace_id, parent_span_id matches).""" | ||
| traceparent, parent_trace_id, parent_span_id = self._make_traceparent() | ||
|
|
||
| # Simulate what the server does: extract and start span with parent context | ||
| server_tracer = trace.get_tracer("azure.ai.agentserver") | ||
| incoming_headers = {"traceparent": traceparent} | ||
| extracted_ctx = TraceContextTextMapPropagator().extract(carrier=incoming_headers) | ||
|
|
||
| with server_tracer.start_as_current_span( | ||
| name="HostedAgents-test", | ||
| kind=trace.SpanKind.SERVER, | ||
| context=extracted_ctx, | ||
| ) as server_span: | ||
| server_ctx = server_span.get_span_context() | ||
|
|
||
| assert server_ctx.trace_id == parent_trace_id, ( | ||
| f"Server span trace_id {server_ctx.trace_id:#034x} should match " | ||
| f"parent trace_id {parent_trace_id:#034x}" | ||
| ) | ||
|
|
||
| server_spans = [s for s in _exporter.spans if s.name == "HostedAgents-test"] | ||
| assert len(server_spans) == 1 | ||
| assert server_spans[0].parent.span_id == parent_span_id | ||
|
|
||
| def test_server_span_is_root_without_traceparent(self): | ||
| """When no traceparent header is present, the server span must be a | ||
| root span (no parent, new trace_id).""" | ||
| server_tracer = trace.get_tracer("azure.ai.agentserver") | ||
| incoming_headers = {} | ||
| extracted_ctx = TraceContextTextMapPropagator().extract(carrier=incoming_headers) | ||
|
|
||
| with server_tracer.start_as_current_span( | ||
| name="HostedAgents-no-parent", | ||
| kind=trace.SpanKind.SERVER, | ||
| context=extracted_ctx, | ||
| ): | ||
| pass | ||
|
|
||
| server_spans = [s for s in _exporter.spans if s.name == "HostedAgents-no-parent"] | ||
| assert len(server_spans) == 1 | ||
| assert server_spans[0].parent is None, "Server span should be root when no traceparent is sent" | ||
|
|
||
| def test_traceparent_header_case_insensitive(self): | ||
| """Starlette's request.headers is a case-insensitive mapping, so | ||
| mixed-case header keys are handled by the framework, not by our code. | ||
| This test verifies the propagator works with the lowercase key that | ||
| Starlette normalizes headers to.""" | ||
| traceparent, parent_trace_id, _ = self._make_traceparent() | ||
|
|
||
| # Starlette normalizes all header keys to lowercase | ||
| server_tracer = trace.get_tracer("azure.ai.agentserver") | ||
| incoming_headers = {"traceparent": traceparent} | ||
| extracted_ctx = TraceContextTextMapPropagator().extract(carrier=incoming_headers) | ||
|
|
||
| with server_tracer.start_as_current_span( | ||
| name="HostedAgents-case-test", | ||
| kind=trace.SpanKind.SERVER, | ||
| context=extracted_ctx, | ||
| ) as server_span: | ||
| server_ctx = server_span.get_span_context() | ||
|
|
||
| assert server_ctx.trace_id == parent_trace_id |
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.
There was a problem hiding this comment.
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
traceparentheader 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).There was a problem hiding this comment.
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