Skip to content

Replace parent_id string with OpenTelemetry Context for trace propagation#205

Merged
nikhilNava merged 7 commits intomainfrom
copilot/replace-parentid-with-context
Mar 18, 2026
Merged

Replace parent_id string with OpenTelemetry Context for trace propagation#205
nikhilNava merged 7 commits intomainfrom
copilot/replace-parentid-with-context

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 18, 2026

Scope classes used parent_id: str (W3C traceparent format) for linking spans. This should use OpenTelemetry's native Context object and expose methods for context propagation headers.

Changes

API Changes

  • All scope classes now accept parent_context: Context | None instead of parent_id: str | None
  • Added inject_trace_context() → returns W3C trace headers for downstream propagation
  • Added get_context() → returns scope's Context for passing to child scopes
  • Added extract_trace_context(headers) utility using OTel's standard extract()
  • InvokeAgentScope now supports parent_context (was previously missing)

Removed

  • Custom W3C trace context parsing (parse_parent_id_to_context) — delegated to OTel

Usage

from microsoft_agents_a365.observability.core import (
    InferenceScope, extract_trace_context
)

# Extract context from incoming HTTP headers
parent_context = extract_trace_context({"traceparent": "00-..."})

with InferenceScope.start(details, agent, tenant, parent_context=parent_context) as scope:
    # Get headers for outbound requests
    headers = scope.inject_trace_context()
    requests.post("https://downstream/api", headers=headers)

Copilot AI and others added 3 commits March 18, 2026 14:45
…opagation

- Changed all scope classes to accept `parent_context: Context` instead of `parent_id: str`
- Added `inject_trace_context()` method to OpenTelemetryScope for header propagation
- Added `get_context()` method to get the scope's Context for passing to child scopes
- Added `extract_trace_context()` utility function using OTel's standard `extract()`
- Removed custom W3C trace context parsing in favor of OTel's propagation API
- Updated OutputLoggingMiddleware to use extract_trace_context()
- Updated all tests to use extract_trace_context() and parent_context

Co-authored-by: nikhilNava <211831449+nikhilNava@users.noreply.github.com>
Co-authored-by: nikhilNava <211831449+nikhilNava@users.noreply.github.com>
@nikhilNava nikhilNava marked this pull request as ready for review March 18, 2026 15:58
@nikhilNava nikhilNava requested a review from a team as a code owner March 18, 2026 15:58
Copilot AI review requested due to automatic review settings March 18, 2026 15:58
@github-actions
Copy link
Copy Markdown

github-actions bot commented Mar 18, 2026

⚠️ Deprecation Warning: The deny-licenses option is deprecated for possible removal in the next major release. For more information, see issue 997.

Dependency Review

✅ No vulnerabilities or license issues or OpenSSF Scorecard issues found.

Scanned Files

None

Copy link
Copy Markdown
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

This PR updates the observability “scope” APIs to use OpenTelemetry’s native Context for parent/child span relationships (replacing the previous W3C traceparent string), and adds helpers for standard trace header propagation.

Changes:

  • Replace parent_id: str | None with parent_context: Context | None across scope classes (including InvokeAgentScope).
  • Add W3C propagation helpers: extract_trace_context(headers) and scope methods to inject/forward context (inject_trace_context(), get_context()).
  • Update/expand unit tests to validate inject/extract propagation and parent-child span linking.

Reviewed changes

Copilot reviewed 13 out of 13 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
tests/observability/hosting/middleware/test_output_logging_middleware.py Updates middleware test expectations from parent_id to parent_context.
tests/observability/core/test_trace_context_propagation.py New tests validating inject/extract propagation, span parenting, and InvokeAgentScope support.
tests/observability/core/test_output_scope.py Updates OutputScope test to build parent_context via extract_trace_context.
tests/observability/core/test_inference_scope.py Updates InferenceScope test to use parent_context extracted from traceparent.
tests/observability/core/test_execute_tool_scope.py Updates ExecuteToolScope test to use parent_context extracted from traceparent.
libraries/microsoft-agents-a365-observability-hosting/microsoft_agents_a365/observability/hosting/middleware/output_logging_middleware.py Extracts parent OTel context from stored traceparent value and passes it into OutputScope.
libraries/microsoft-agents-a365-observability-core/microsoft_agents_a365/observability/core/utils.py Removes custom W3C parsing and adds extract_trace_context() backed by OTel propagation.
libraries/microsoft-agents-a365-observability-core/microsoft_agents_a365/observability/core/spans_scopes/output_scope.py Updates OutputScope API to accept parent_context and forwards it to the base scope.
libraries/microsoft-agents-a365-observability-core/microsoft_agents_a365/observability/core/opentelemetry_scope.py Updates base scope constructor to accept parent_context; adds get_context() and inject_trace_context().
libraries/microsoft-agents-a365-observability-core/microsoft_agents_a365/observability/core/invoke_agent_scope.py Adds parent_context support to InvokeAgentScope.
libraries/microsoft-agents-a365-observability-core/microsoft_agents_a365/observability/core/inference_scope.py Updates InferenceScope API to accept and forward parent_context.
libraries/microsoft-agents-a365-observability-core/microsoft_agents_a365/observability/core/execute_tool_scope.py Updates ExecuteToolScope API to accept and forward parent_context.
libraries/microsoft-agents-a365-observability-core/microsoft_agents_a365/observability/core/init.py Exports extract_trace_context from the public core package API.

You can also share your feedback on Copilot code review. Take the survey.

nikhilNava and others added 4 commits March 18, 2026 22:57
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@nikhilNava nikhilNava enabled auto-merge (squash) March 18, 2026 18:41
@nikhilNava nikhilNava merged commit ba8566d into main Mar 18, 2026
9 checks passed
@nikhilNava nikhilNava deleted the copilot/replace-parentid-with-context branch March 18, 2026 19:19
biswapm pushed a commit to biswapm/Agent365-python that referenced this pull request Mar 20, 2026
…tion (microsoft#205)

* Initial plan

* feat: Replace parent_id string with Context object and add context propagation

- Changed all scope classes to accept `parent_context: Context` instead of `parent_id: str`
- Added `inject_trace_context()` method to OpenTelemetryScope for header propagation
- Added `get_context()` method to get the scope's Context for passing to child scopes
- Added `extract_trace_context()` utility function using OTel's standard `extract()`
- Removed custom W3C trace context parsing in favor of OTel's propagation API
- Updated OutputLoggingMiddleware to use extract_trace_context()
- Updated all tests to use extract_trace_context() and parent_context

Co-authored-by: nikhilNava <211831449+nikhilNava@users.noreply.github.com>

* refactor: Address code review feedback - move imports to module level

Co-authored-by: nikhilNava <211831449+nikhilNava@users.noreply.github.com>

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* clean up usage of preant context propogation

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: nikhilNava <211831449+nikhilNava@users.noreply.github.com>
Co-authored-by: Nikhil Navakiran <nikhil.navakiran@gmail.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Nikhil Chitlur Navakiran (from Dev Box) <nikhilc@microsoft.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants