-
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 all 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
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"; | ||
| } | ||
| } |
98 changes: 98 additions & 0 deletions
98
...re.Monitor.OpenTelemetry.Exporter/src/Internals/GenAI/MainAgentAttributionLogProcessor.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,98 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System.Collections.Generic; | ||
| using System.Diagnostics; | ||
| using OpenTelemetry; | ||
| using OpenTelemetry.Logs; | ||
|
|
||
| using static Azure.Monitor.OpenTelemetry.Exporter.Internals.GenAI.MainAgentAttributeConstants; | ||
|
|
||
| namespace Azure.Monitor.OpenTelemetry.Exporter.Internals.GenAI | ||
| { | ||
| /// <summary> | ||
| /// A log processor that copies main-agent attributes from the current span | ||
| /// onto emitted log records so that logs are attributed to the user-facing agent. | ||
| /// </summary> | ||
| internal sealed class MainAgentAttributionLogProcessor : BaseProcessor<LogRecord> | ||
| { | ||
| private static readonly string[] s_mainAgentAttributes = new[] | ||
| { | ||
| MainAgentName, | ||
| MainAgentId, | ||
| MainAgentVersion, | ||
| MainAgentConversationId, | ||
| }; | ||
|
|
||
| public override void OnEnd(LogRecord logRecord) | ||
| { | ||
| var activity = Activity.Current; | ||
| if (activity == null) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| // Quick check: skip if current span has no main agent attributes. | ||
| // Checking Name and Id suffices because any conforming GenAI | ||
| // span will always carry at least the agent name attribute. | ||
| if (activity.GetTagItem(MainAgentName) == null && | ||
| activity.GetTagItem(MainAgentId) == null) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| // Build a set of keys already present on the log record to avoid | ||
| // adding duplicate attributes. | ||
| var existingAttributes = logRecord.Attributes; | ||
| HashSet<string>? existingKeys = null; | ||
| if (existingAttributes != null && existingAttributes.Count > 0) | ||
| { | ||
| existingKeys = new HashSet<string>(); | ||
| foreach (var kvp in existingAttributes) | ||
| { | ||
| existingKeys.Add(kvp.Key); | ||
| } | ||
| } | ||
|
|
||
| // Collect values into a fixed-size buffer so we can check count | ||
| // before allocating the merged list. | ||
| var values = new KeyValuePair<string, object?>[s_mainAgentAttributes.Length]; | ||
| int count = 0; | ||
|
|
||
| foreach (var attributeKey in s_mainAgentAttributes) | ||
| { | ||
| // Skip if the log record already carries this attribute. | ||
| if (existingKeys != null && existingKeys.Contains(attributeKey)) | ||
| { | ||
| continue; | ||
| } | ||
|
|
||
| var value = activity.GetTagItem(attributeKey); | ||
| if (value != null) | ||
| { | ||
| values[count++] = new KeyValuePair<string, object?>(attributeKey, value); | ||
| } | ||
| } | ||
|
|
||
| if (count == 0) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| var merged = new List<KeyValuePair<string, object?>>( | ||
| (existingAttributes?.Count ?? 0) + count); | ||
|
|
||
| if (existingAttributes != null) | ||
| { | ||
| merged.AddRange(existingAttributes); | ||
| } | ||
|
|
||
| for (int i = 0; i < count; i++) | ||
| { | ||
| merged.Add(values[i]); | ||
| } | ||
|
|
||
| logRecord.Attributes = merged; | ||
| } | ||
| } | ||
| } |
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.