Skip to content

fix tracing for operation id to flow in exception table.#47387

Open
harsheet-shah wants to merge 16 commits into
Azure:mainfrom
harsheet-shah:user/harsheetshah/fix-telemetry-traceid
Open

fix tracing for operation id to flow in exception table.#47387
harsheet-shah wants to merge 16 commits into
Azure:mainfrom
harsheet-shah:user/harsheetshah/fix-telemetry-traceid

Conversation

@harsheet-shah
Copy link
Copy Markdown

Description

Please add an informative description that covers that changes made by the pull request and link all relevant issues.

If an SDK is being regenerated based on a new API spec, a link to the pull request containing these API spec changes should be included above.

All SDK Contribution checklist:

  • The pull request does not introduce [breaking changes]
  • CHANGELOG is updated for new features, bug fixes or other significant changes.
  • I have read the contribution guidelines.

General Guidelines and Best Practices

  • Title of the pull request is clear and informative.
  • There are a small number of commits, each of which have an informative message. This means that previously merged commits do not appear in the history of the PR. For more information on cleaning up the commits in your PR, see this page.

Testing Guidelines

  • Pull request includes test coverage for the included changes.

Copilot AI review requested due to automatic review settings June 8, 2026 04:48
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Jun 8, 2026

Thank you for your contribution @harsheet-shah! We will review the pull request and get back to you soon.

@github-actions github-actions Bot added Community Contribution Community members are working on the issue customer-reported Issues that are reported by GitHub users external to the Azure organization. Hosted Agents sdk/agentserver/* labels Jun 8, 2026
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

Note

Copilot was unable to run its full agentic suite in this review.

Updates ASGI tracing middleware to ensure logs emitted after request handling (notably Hypercorn access logs) still carry the correct OpenTelemetry trace context, preventing zeroed operation_Id in Application Insights.

Changes:

  • Creates a NonRecordingSpan from extracted trace context so get_current_span() exposes the correct trace_id.
  • Attaches the extracted context for the duration of the task and removes the previous detach() in finally to keep context available for post-response logging.

Comment thread sdk/agentserver/azure-ai-agentserver-core/azure/ai/agentserver/core/_tracing.py Outdated
span = trace.get_current_span(ctx)
span_ctx = span.get_span_context()
if span_ctx and span_ctx.trace_id:
non_recording = trace.NonRecordingSpan(span_ctx)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why do we need to create a non-recording span ?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We should validate consequent request and how traces show up.

Copy link
Copy Markdown
Author

Defer context detach by one event-loop turn so post-response access logging can still observe request trace context, while still restoring prior context to prevent leakage.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@harsheet-shah harsheet-shah requested a review from Copilot June 8, 2026 08:06
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

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

Comment thread sdk/agentserver/azure-ai-agentserver-core/azure/ai/agentserver/core/_tracing.py Outdated
await asyncio.sleep(0)
detach_mock.assert_called_once_with("token")

asyncio.run(run_test())
await asyncio.sleep(0)
detach_mock.assert_called_once_with("token")

asyncio.run(run_test())
@harsheet-shah harsheet-shah requested a review from Copilot June 8, 2026 08:09
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

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

Comment thread sdk/agentserver/azure-ai-agentserver-core/azure/ai/agentserver/core/_tracing.py Outdated
Comment on lines +454 to +462
with mock.patch("azure.ai.agentserver.core._tracing._otel_context.attach", return_value="token") as attach_mock, \
mock.patch("azure.ai.agentserver.core._tracing._otel_context.detach") as detach_mock:
await middleware(scope, receive, send)
assert events == ["app-called"]
attach_mock.assert_called_once()
detach_mock.assert_not_called()

await asyncio.sleep(0)
detach_mock.assert_called_once_with("token")
Comment thread sdk/agentserver/azure-ai-agentserver-core/tests/test_tracing.py Outdated
Comment thread sdk/agentserver/azure-ai-agentserver-core/tests/test_tracing.py Outdated
Harsheet Shah and others added 6 commits June 8, 2026 13:46
Validate detach_context swallows ValueError so call_soon(detach_context, token) cannot surface non-current-token detach errors as unhandled callback failures.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Use a dedicated safe callback for call_soon detaches and keep ValueError suppression behavior for invalid/non-current tokens without regressing deferred-detach tracing fix.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Use call_soon(detach_context, token) directly and keep ValueError handling in detach_context to avoid callback noise while preserving deferred trace context behavior.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Use an explicit deferred callback helper that detaches the attached token and swallows ValueError to avoid unhandled callback noise while preserving delayed context cleanup.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@harsheet-shah harsheet-shah requested a review from Copilot June 8, 2026 08:30
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

Copilot reviewed 1 out of 1 changed files in this pull request and generated 2 comments.

Comment thread sdk/agentserver/azure-ai-agentserver-core/azure/ai/agentserver/core/_tracing.py Outdated
Comment thread sdk/agentserver/azure-ai-agentserver-core/azure/ai/agentserver/core/_tracing.py Outdated
Harsheet Shah and others added 2 commits June 8, 2026 14:03
Wrap deferred detach scheduling in try/except and fall back to immediate best-effort detach so token cleanup still happens when the event loop is closing/closed.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Route both call_soon callback and scheduling-failure fallback through detach_context(token) to keep detach semantics centralized and consistent.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@harsheet-shah harsheet-shah requested a review from Copilot June 8, 2026 08:36
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

Copilot reviewed 1 out of 1 changed files in this pull request and generated 2 comments.

Comment thread sdk/agentserver/azure-ai-agentserver-core/azure/ai/agentserver/core/_tracing.py Outdated
Harsheet Shah and others added 2 commits June 8, 2026 14:29
Add agent name/version and sessionid dimensions to log records via OTel log processor while retaining baggage propagation and span enrichment.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Resolve agent name/version from AGENT_* env pairs when FOUNDRY_AGENT_* is absent, and remove gen_ai.agent.sessionid to rely on existing azure.ai.agentserver.session_id baggage field.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Harsheet Shah and others added 4 commits June 8, 2026 15:47
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Community Contribution Community members are working on the issue customer-reported Issues that are reported by GitHub users external to the Azure organization. Hosted Agents sdk/agentserver/*

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants