Skip to content

Python: Show FoundryAgent client spans in Foundry traces - #7981

Open
Jose Alvarez (jpalvarezl) wants to merge 9 commits into
mainfrom
jpalvarezl/fix/7492_python_telemetry
Open

Python: Show FoundryAgent client spans in Foundry traces#7981
Jose Alvarez (jpalvarezl) wants to merge 9 commits into
mainfrom
jpalvarezl/fix/7492_python_telemetry

Conversation

@jpalvarezl

@jpalvarezl Jose Alvarez (jpalvarezl) commented Sep 1, 2026

Copy link
Copy Markdown
Member

Motivation & Context

FoundryAgent client-side spans are exported to Application Insights but do not appear in the agent's Foundry trace view. Foundry requires the client root span to include the Foundry project ARM ID and the response ID used for trace discovery.

This change makes client-side invoke_agent and child chat spans discoverable in Foundry for both streaming and non-streaming runs without changing the agent's public identity or adding agent-version metadata.

Description & Review Guide

  • What are the major changes?
    • Resolve the Foundry project ARM ID from the project's Application Insights connection when Azure Monitor is configured.
    • Add a provider extension point for attributes required on the root agent span.
    • Preserve the inner chat response ID on the Foundry client root span, including streaming runs.
  • What is the impact of these changes?
    • Foundry client spans now appear in the Foundry trace view while existing agent ID and response telemetry behavior remains unchanged for other providers.
  • What do you want reviewers to focus on?
    • The project-ID derivation and the context-variable lifecycle used to retain response IDs across streaming and non-streaming execution.

Related Issue

Fixes #7492

Contribution Checklist

  • The code builds clean without any errors or warnings
  • All unit tests pass, and I have added new tests where possible
  • The PR follows the Contribution Guidelines
  • This PR is linked to an issue and there is no other open PR for this issue (see Related Issue above).
  • This is not a breaking change. If it is a breaking change, add the breaking change label (or add "[BREAKING]" to the title prefix, before or after any language prefix) — a workflow keeps the label and title prefix in sync automatically

Copilot AI balanced review requested due to automatic review settings September 1, 2026 08:38
Comment thread .gitignore Outdated
@agent-framework-automation agent-framework-automation Bot added the python Usage: [Issues, PRs], Target: Python label Sep 1, 2026
@github-actions github-actions Bot changed the title Python telemetry fix Python: Python telemetry fix Sep 1, 2026

Copilot AI left a comment

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.

Pull request overview

Fixes Foundry client-span attribution by exposing provider-specific OpenTelemetry agent identity hooks.

Changes:

  • Adds overridable agent ID/name telemetry hooks.
  • Maps Foundry’s deployed agent name to trace identity.
  • Adds identity tests and ignores JetBrains metadata.

Reviewed changes

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

File Description
observability.py Adds telemetry identity hooks.
_agent.py Implements Foundry-specific identity mapping.
test_foundry_agent.py Tests resolved and explicit identities.
.gitignore Ignores .idea/.

💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

@github-code-quality

github-code-quality Bot commented Sep 1, 2026

Copy link
Copy Markdown

Code Coverage Overview

Languages: Python

Python / code-coverage/python

The overall line coverage in commit 4f3b8b1 in the jpalvarezl/fix/7492_... branch is 91%. Line coverage data for the main branch is not yet available.

Show a line coverage summary of the most covered files.
File main jpalvarezl/fix/7492_... 4f3b8b1 +/-
packages/core/a...work/_skills.py 95%
packages/core/a...ework/_tools.py 94%
packages/core/a...rk/_sessions.py 94%
packages/core/a...ework/_types.py 93%
packages/core/a...bservability.py 93%
packages/core/a.../_compaction.py 93%
packages/openai..._chat_client.py 92%
packages/core/a...amework/_mcp.py 92%
packages/ag-ui/...i/_agent_run.py 90%
packages/core/a...ork/security.py 78%

Updated September 02, 2026 11:37 UTC

@moonbox3

Copy link
Copy Markdown
Contributor

Please use the required pull request template. When ready, re-open the PR.

@jpalvarezl Jose Alvarez (jpalvarezl) changed the title Python: Python telemetry fix Python: Show FoundryAgent client spans in Foundry traces Sep 1, 2026
@jpalvarezl

Copy link
Copy Markdown
Member Author

Re-opening PR to request feedback. Made the PR compliant to guidelines to the best of my ability.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 5641780c-318a-4def-8692-710b2b061c60

@github-actions github-actions Bot left a comment

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.

MAF Automated Review — Iteration 1

Result: Findings reported
Scope: full PR (8 commit(s)): c133e29bf466, 2ac21147e0b5, c46ff52fccb4, 7180c5893298, 79db80942428, 7695b7ffd6d9, a3eeaee0c3b8, 807a12a7d750
Model: gpt-5.6-sol

Overview

The PR adds provider hooks for Foundry-specific root-span attributes and response-ID retention, with safe defaults for other providers and symmetric context-variable cleanup in streaming and non-streaming paths. The new tests cover both response-ID modes and successful project-ID derivation. One setup-ordering regression remains: failure of the additional ARM-ID lookup prevents Azure Monitor from being configured even after a valid Application Insights connection string was obtained.

Reviewed the supplied pull-request change set across correctness, security/reliability, architecture, and failure behavior.
1 verified finding remained after source verification (1 high) across 1 file. Details are attached to the affected lines below.

Affected areas: python/packages/foundry/agent_framework_foundry/_agent.py

"Install it with: pip install azure-monitor-opentelemetry"
) from exc

self._foundry_project_arm_id = await self._get_foundry_project_arm_id()

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.

Could this lookup degrade gracefully instead of aborting setup? The connection string has already been retrieved successfully, but the separate connections API can return no APPLICATION_INSIGHTS entry, an unexpected ID shape, or its own service error. Any of those now prevents configure_azure_monitor() and enable_instrumentation() from running, so deployments that previously exported telemetry lose it entirely just because the optional Foundry project attribute cannot be derived. Please warn and continue configuring Azure Monitor without the project attribute when this lookup fails.

"Install it with: pip install azure-monitor-opentelemetry"
) from exc

self._foundry_project_arm_id = await self._get_foundry_project_arm_id()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

so does calling this here, mean that only if you setup using this helper method this get's applied and things work? that's either something we need to document extra well, including the in the current samples, or something we need to rethink... (to be clear, I'm not opposed perse)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

python Usage: [Issues, PRs], Target: Python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python: [Bug]: Foundry Prompt Agent client spans are not showing up in Foundry tracing UI

4 participants