-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Add GenAI main agent attribution processors for spans and logs #59368
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b4fc25b
Add GenAI main agent attribution processors for spans and logs
rajkumar-rangaraj de9b869
Address review: dedup log attrs, guard span overwrites, per-attribute…
rajkumar-rangaraj c7626d9
Address Copilot review: fix test snapshot, fix misleading comment
rajkumar-rangaraj 3344dd5
Fix netstandard2.0 build: use parameterless HashSet ctor
rajkumar-rangaraj 6a72237
Address Harsimar review: restore all-or-nothing OnEnd per spec
rajkumar-rangaraj e83a9c7
Align OnStart with spec: unconditionally copy from parent
rajkumar-rangaraj f7c8530
Remove spec file from PR
rajkumar-rangaraj File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
78 changes: 78 additions & 0 deletions
78
sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/genai_main_agent_attribution.md
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| # GenAI Main Agent Attribution | ||
|
|
||
| ## Owner | ||
|
|
||
| <!-- TODO: Add owner --> | ||
|
|
||
| ## Approvers | ||
|
|
||
| * Rajkumar Rangaraj | ||
| * Jackson Weber | ||
| * Radhika Gupta | ||
|
|
||
| ## Status | ||
|
|
||
| Experimental | ||
|
|
||
| ## Purpose | ||
|
|
||
| Specifies how Azure Monitor Distros (Python, .NET, Java, Node.js) detect the top-level ("main") agent in a GenAI multi-agent system and propagate main-agent context so that all emitted telemetry (traces, metrics, logs) is attributed to the user-facing agent rather than to internal implementation agents. | ||
|
|
||
| ## Specification | ||
|
|
||
| ### Span Attribution | ||
|
|
||
| The distro MUST register a SpanProcessor in the TracerProvider pipeline before any export processor (e.g., BatchSpanProcessor). | ||
|
|
||
| In [OnStart(span, parentContext)][1], the processor MUST: | ||
|
|
||
| 1. Extract the Span from `parentContext` to obtain the parent span. If there is no parent span, return. | ||
|
|
||
| 2. For each row in the table below, read the **primary source** attribute from the parent span. If it exists, copy it onto `span` as the target attribute. Otherwise, read the **fallback source** attribute from the parent span and, if it exists, copy it onto `span` as the target attribute. | ||
|
|
||
| | Target attribute (on `span`) | Primary source (on parent span) | Fallback source (on parent span) | | ||
| | :--- | :--- | :--- | | ||
| | `microsoft.gen_ai.main_agent.name` | `microsoft.gen_ai.main_agent.name` | `gen_ai.agent.name` | | ||
| | `microsoft.gen_ai.main_agent.id` | `microsoft.gen_ai.main_agent.id` | `gen_ai.agent.id` | | ||
| | `microsoft.gen_ai.main_agent.version` | `microsoft.gen_ai.main_agent.version` | `gen_ai.agent.version` | | ||
| | `microsoft.gen_ai.main_agent.conversation_id` | `microsoft.gen_ai.main_agent.conversation_id` | `gen_ai.conversation.id` | | ||
|
|
||
| In [OnEnd(span)][7], the processor MUST: | ||
|
|
||
| 1. If `span` does not have `gen_ai.operation.name` = `invoke_agent`, return. | ||
|
|
||
| 2. If `span` already has any `microsoft.gen_ai.main_agent.*` attribute, return. | ||
|
|
||
| 3. For each row in the table below, read the source attribute from `span` and, if it exists, copy it onto `span` as the target attribute. | ||
|
|
||
| | Target attribute | Source attribute | | ||
| | :--- | :--- | | ||
| | `microsoft.gen_ai.main_agent.name` | `gen_ai.agent.name` | | ||
| | `microsoft.gen_ai.main_agent.id` | `gen_ai.agent.id` | | ||
| | `microsoft.gen_ai.main_agent.version` | `gen_ai.agent.version` | | ||
| | `microsoft.gen_ai.main_agent.conversation_id` | `gen_ai.conversation.id` | | ||
|
|
||
| ### Log Attribution | ||
|
|
||
| The distro MUST register a LogRecordProcessor in the LoggerProvider pipeline before any export processor (e.g., BatchLogRecordProcessor). | ||
|
|
||
| In [OnEmit(logRecord, context)][2], the processor MUST: | ||
|
|
||
| 1. Extract the Span from `context` to obtain the current span. If there is no current span, return. | ||
|
|
||
| 2. If the current span has any `microsoft.gen_ai.main_agent.*` attributes, copy all of them onto the `logRecord`. | ||
|
|
||
| ### Metric Attribution | ||
|
|
||
| The OTel Metrics SDK does not define a per-measurement processor hook ([issue][3], [PR][4]). | ||
|
|
||
| **Java**: The Java SDK includes an internal [`View.AttributesProcessor`][5] that accepts `(Attributes, Context)` and can modify measurement attributes. The distro could use this to extract the active span from `Context` and copy `microsoft.gen_ai.main_agent.*` attributes onto measurements. | ||
|
|
||
| **Python, .NET, Node.js**: I don't believe an equivalent internal hook exists. | ||
|
|
||
| [1]: https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/sdk.md#onstart "SpanProcessor OnStart" | ||
| [2]: https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/logs/sdk.md#onemit "LogRecordProcessor OnEmit" | ||
| [3]: https://github.com/open-telemetry/opentelemetry-specification/issues/4298 "Support measurement processors in Metrics SDK" | ||
| [4]: https://github.com/open-telemetry/opentelemetry-specification/pull/4318 "Add MeasurementProcessor to Metrics SDK (closed)" | ||
| [5]: https://github.com/open-telemetry/opentelemetry-java/blob/main/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/internal/view/AttributesProcessor.java "Java AttributesProcessor (internal API)" | ||
| [7]: https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/sdk.md#onend "SpanProcessor OnEnd" |
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
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
24 changes: 24 additions & 0 deletions
24
...r/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/GenAI/MainAgentAttributeConstants.cs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| namespace Azure.Monitor.OpenTelemetry.Exporter.Internals.GenAI | ||
| { | ||
| internal static class MainAgentAttributeConstants | ||
| { | ||
| // Target attributes (microsoft.gen_ai.main_agent.*) | ||
| internal const string MainAgentName = "microsoft.gen_ai.main_agent.name"; | ||
| internal const string MainAgentId = "microsoft.gen_ai.main_agent.id"; | ||
| internal const string MainAgentVersion = "microsoft.gen_ai.main_agent.version"; | ||
| internal const string MainAgentConversationId = "microsoft.gen_ai.main_agent.conversation_id"; | ||
|
|
||
| // Source / fallback attributes (gen_ai.agent.* / gen_ai.conversation.*) | ||
| internal const string GenAiAgentName = "gen_ai.agent.name"; | ||
| internal const string GenAiAgentId = "gen_ai.agent.id"; | ||
| internal const string GenAiAgentVersion = "gen_ai.agent.version"; | ||
| internal const string GenAiConversationId = "gen_ai.conversation.id"; | ||
|
|
||
| // Operation name | ||
| internal const string GenAiOperationName = "gen_ai.operation.name"; | ||
| internal const string InvokeAgentOperationName = "invoke_agent"; | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.