fix: use get_span_context() to handle NonRecordingSpan#258
Merged
Conversation
Dependency Review✅ No vulnerabilities or license issues or OpenSSF Scorecard issues found.Scanned FilesNone |
When the token resolver returns None on the first turn, the TracerProvider produces a NonRecordingSpan. The .context attribute only exists on concrete ReadableSpan implementations, causing AttributeError crashes on the first agent message. Replace all span.context accesses with the public get_span_context() interface method, which is available on all Span types including NonRecordingSpan. Also guard the .name access in _end() with getattr() since NonRecordingSpan lacks that attribute as well. Changes: - opentelemetry_scope.py: use get_span_context() in span start/end logging - enriched_span.py: delegate context property via get_span_context() - agent365_exporter.py: use get_span_context() in _map_span() - Update test mocks to set get_span_context() return value - Add test_nonrecording_span.py with 6 targeted tests Ported from: microsoft/opentelemetry-distro-python#168 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
5d2f274 to
aa8dcc6
Compare
Contributor
There was a problem hiding this comment.
Pull request overview
This PR hardens the observability pipeline against NonRecordingSpan instances by switching span-context access from the .context attribute (not present on NonRecordingSpan) to the get_span_context() API, preventing first-turn AttributeError crashes.
Changes:
- Replace
.contextusage withget_span_context()in span logging, span wrappers, and exporter mapping. - Guard span
.nameaccess during scope shutdown to handle spans that don’t exposename. - Update span mocks and add a dedicated test module covering
NonRecordingSpanscenarios.
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/observability/core/test_nonrecording_span.py | Adds targeted tests to ensure NonRecordingSpan does not crash scope lifecycle, span wrapping, or exporter mapping. |
| tests/observability/core/test_agent365_exporter.py | Updates mock spans to provide get_span_context() for exporter tests. |
| tests/observability/core/exporters/test_payload_chunking.py | Updates mock spans to provide get_span_context() for payload chunking tests. |
| tests/observability/core/exporters/test_enriching_span_processor.py | Updates mock spans to provide get_span_context() for processor tests. |
| tests/observability/core/exporters/test_enriched_span.py | Updates mock spans to provide get_span_context() for enriched span tests. |
| libraries/microsoft-agents-a365-observability-core/microsoft_agents_a365/observability/core/opentelemetry_scope.py | Uses get_span_context() for logging span IDs and guards span name access in _end(). |
| libraries/microsoft-agents-a365-observability-core/microsoft_agents_a365/observability/core/exporters/enriched_span.py | Makes EnrichedReadableSpan.context delegate via get_span_context(). |
| libraries/microsoft-agents-a365-observability-core/microsoft_agents_a365/observability/core/exporters/agent365_exporter.py | Uses get_span_context() in _map_span() to extract trace/span IDs. |
fpfp100
approved these changes
May 27, 2026
juliomenendez
approved these changes
May 27, 2026
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.
Summary
When the token resolver returns None on the first turn, the TracerProvider produces a NonRecordingSpan. The
.contextattribute only exists on concrete ReadableSpan implementations, causingAttributeErrorcrashes on the first agent message.Changes
Replace all
span.contextaccesses with the publicget_span_context()interface method, which is available on all Span types including NonRecordingSpan. Also guard the.nameaccess in_end()withgetattr()since NonRecordingSpan lacks that attribute as well.Files modified:
get_span_context()in span start/end loggingget_span_context()get_span_context()in_map_span()get_span_context()return value on all mock spanstest_nonrecording_span.pywith 6 targeted testsSource
Ported from: microsoft/opentelemetry-distro-python#168
Testing
All 227 observability core tests pass.