Skip to content

Commit 394f0e7

Browse files
jpalvarezlCopilot
andcommitted
Fix Foundry attribution setup and agent response ownership
Separate per-agent project identity from exporter configuration, preserve export on optional discovery failures, and capture response identity from the owned chat result. Consolidate invocation telemetry state and document both setup paths. Co-authored-by: Copilot <[email protected]> Copilot-Session: bfc650ea-638e-4c20-aae6-f63cca98462e
1 parent 51f07df commit 394f0e7

11 files changed

Lines changed: 854 additions & 106 deletions

File tree

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
---
2+
status: proposed
3+
contact: jpalvarezl
4+
date: 2026-09-09
5+
deciders: [eavanvalkenburg, moonbox3]
6+
---
7+
8+
# Separate Foundry trace attribution from exporter configuration
9+
10+
## Context and Problem Statement
11+
12+
[Issue #7492](https://github.com/microsoft/agent-framework/issues/7492) reports
13+
client spans reaching Application Insights but missing from the Foundry agent
14+
trace view. Exporting telemetry and identifying the project/agent represented
15+
by an operation are separate concerns.
16+
17+
The initial fix in [PR #7981](https://github.com/microsoft/agent-framework/pull/7981)
18+
resolved project identity only through `FoundryAgent.configure_azure_monitor()`.
19+
That left applications configuring their own exporters without an attribution
20+
path. It also let optional identity-discovery failures abort usable export.
21+
Separately, recording the last child chat's response ID could assign an
22+
after-run provider's ID to the agent invocation.
23+
24+
## Decision Drivers
25+
26+
- Support application-managed exporters without reconfiguring global providers.
27+
- Keep identity scoped to the agent/project rather than a process-wide setting.
28+
- Preserve export when optional metadata discovery fails, with a visible warning.
29+
- Use the completed agent-owned operation for response identity.
30+
- Preserve public response-ID suppression, continuation, and usage semantics.
31+
- Do not generalize one successful root-span arrangement into a universal
32+
requirement that every application root carry project attributes.
33+
34+
## Considered Options
35+
36+
- **Helper-only discovery.** Simple, but couples identity to one exporter helper
37+
and leaves application-managed configurations unsupported.
38+
- **Automatic discovery on each run.** Convenient, but introduces implicit
39+
network work, latency, and failure handling into the invocation path.
40+
- **Explicit per-agent identity plus cached helper discovery.** Supports both
41+
setup styles without run-time discovery. Applications using their own
42+
exporters must supply the project ARM ID.
43+
44+
## Decision Outcome
45+
46+
Choose **explicit per-agent identity plus cached helper discovery**:
47+
48+
- Add keyword-only `project_arm_id` to `RawFoundryAgent` and `FoundryAgent`.
49+
The full project ARM ID is validated at construction; it is not inferred from
50+
the data-plane endpoint or read implicitly from an environment variable.
51+
- `FoundryAgent` emits the project attribute alongside agent identity on its
52+
`invoke_agent` span, which may be nested beneath an application span.
53+
- The Azure Monitor helper discovers and caches identity only when no ID was
54+
supplied. Expected discovery/metadata errors log a warning and preserve
55+
export; unexpected programming errors and cancellation still propagate.
56+
- Capture response identity from the agent-owned chat result before conversion
57+
and after-run callbacks. In streaming, use the underlying final response,
58+
including finalizer-only metadata. Keep this private telemetry identity
59+
separate from the public response/continuation fields.
60+
- Consolidate invocation bookkeeping into one owner-scoped context state.
61+
Child chats can still contribute usage without choosing the agent's identity.
62+
63+
The public SDK does not yet expose project identity directly
64+
([Azure/azure-sdk-for-python#48825](https://github.com/Azure/azure-sdk-for-python/issues/48825)).
65+
Connection-ID parsing remains a bounded discovery workaround, not a requirement
66+
for applications that already know their project ARM ID.
67+
68+
Acceptance distinguishes local emission, Application Insights ingestion, and
69+
personal inspection of client spans in Foundry. Supported examples use full
70+
project ARM IDs; alternate formats and service-side legacy-key behavior are not
71+
new guarantees of this API.

‎python/packages/core/agent_framework/_agents.py‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,10 @@
6464
normalize_messages,
6565
)
6666
from .exceptions import AgentInvalidRequestException, AgentInvalidResponseException, UserInputRequiredException
67-
from .observability import AgentTelemetryLayer
67+
from .observability import (
68+
AgentTelemetryLayer,
69+
_capture_agent_response_id, # pyright: ignore[reportPrivateUsage]
70+
)
6871

6972
if sys.version_info >= (3, 13):
7073
from typing import TypeVar # pragma: no cover
@@ -1219,6 +1222,7 @@ async def _parse_non_streaming_response(
12191222
if not response:
12201223
raise AgentInvalidResponseException("Chat client did not return a response.")
12211224

1225+
_capture_agent_response_id(self, response)
12221226
for message in response.messages:
12231227
if message.author_name is None:
12241228
message.author_name = context["agent_name"]
@@ -1290,7 +1294,9 @@ def _finalizer(updates: Sequence[AgentResponseUpdate]) -> AgentResponse[Any]:
12901294
response_format=context["chat_options"].get("response_format"),
12911295
)
12921296

1293-
stream = stream_response.map(
1297+
# Capture the completed chat operation, including finalizer-only metadata,
1298+
# before mapping to the public response and invoking after-run providers.
1299+
stream = stream_response.with_result_hook(partial(_capture_agent_response_id, self)).map(
12941300
transform=partial(
12951301
map_chat_to_agent_update,
12961302
agent_name=self.name,

0 commit comments

Comments
 (0)