-
Notifications
You must be signed in to change notification settings - Fork 8
[Observability] Output scope #201
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
5 commits
Select commit
Hold shift + click to select a range
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
72 changes: 72 additions & 0 deletions
72
src/Observability/Runtime/DTOs/Builders/OutputDataBuilder.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,72 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using Microsoft.Agents.A365.Observability.Runtime.Tracing.Contracts; | ||
| using Microsoft.Agents.A365.Observability.Runtime.Tracing.Scopes; | ||
| using System; | ||
| using System.Collections.Generic; | ||
|
|
||
| namespace Microsoft.Agents.A365.Observability.Runtime.DTOs.Builders | ||
| { | ||
| /// <summary> | ||
| /// Builds an OutputData instance. | ||
| /// </summary> | ||
| public class OutputDataBuilder : BaseDataBuilder<OutputData> | ||
| { | ||
| private const string OutputMessagesOperationName = "output_messages"; | ||
|
|
||
| /// <summary> | ||
| /// Builds complete data for an output_messages operation. | ||
| /// </summary> | ||
| /// <param name="agentDetails">The details of the agent.</param> | ||
| /// <param name="tenantDetails">The details of the tenant.</param> | ||
| /// <param name="response">The response containing output messages.</param> | ||
| /// <param name="startTime">Optional custom start time for the operation.</param> | ||
| /// <param name="endTime">Optional custom end time for the operation.</param> | ||
| /// <param name="spanId">Optional span ID for the operation.</param> | ||
| /// <param name="parentSpanId">Optional parent span ID for distributed tracing.</param> | ||
| /// <param name="extraAttributes">Optional dictionary of extra attributes.</param> | ||
| /// <returns>An OutputData object containing all telemetry data.</returns> | ||
| public static OutputData Build( | ||
| AgentDetails agentDetails, | ||
| TenantDetails tenantDetails, | ||
| Response response, | ||
| DateTimeOffset? startTime = null, | ||
| DateTimeOffset? endTime = null, | ||
| string? spanId = null, | ||
| string? parentSpanId = null, | ||
| IDictionary<string, object?>? extraAttributes = null) | ||
| { | ||
| var attributes = BuildAttributes(agentDetails, tenantDetails, response, extraAttributes); | ||
|
|
||
| return new OutputData(attributes, startTime, endTime, spanId, parentSpanId); | ||
| } | ||
|
|
||
| private static Dictionary<string, object?> BuildAttributes( | ||
| AgentDetails agentDetails, | ||
| TenantDetails tenantDetails, | ||
| Response response, | ||
| IDictionary<string, object?>? extraAttributes = null) | ||
| { | ||
| var attributes = new Dictionary<string, object?>(); | ||
|
|
||
| // Operation name | ||
| AddIfNotNull(attributes, OpenTelemetryConstants.GenAiOperationNameKey, OutputMessagesOperationName); | ||
threddy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // Agent & tenant | ||
| AddAgentDetails(attributes, agentDetails); | ||
| AddTenantDetails(attributes, tenantDetails); | ||
|
|
||
| // Output messages from response | ||
| if (response.Messages.Count > 0) | ||
| { | ||
| AddIfNotNull(attributes, OpenTelemetryConstants.GenAiOutputMessagesKey, string.Join(",", response.Messages)); | ||
| } | ||
|
|
||
| // Add any extra attributes | ||
| AddExtraAttributes(attributes, extraAttributes); | ||
threddy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| return attributes; | ||
| } | ||
| } | ||
| } | ||
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,37 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using Microsoft.Agents.A365.Observability.Runtime.Tracing.Scopes; | ||
| using System; | ||
| using System.Collections.Generic; | ||
|
|
||
| namespace Microsoft.Agents.A365.Observability.Runtime.DTOs | ||
| { | ||
| /// <summary> | ||
| /// Encapsulates all telemetry data for an output_messages operation. | ||
| /// </summary> | ||
| public class OutputData : BaseData | ||
| { | ||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="OutputData"/> class. | ||
| /// </summary> | ||
| /// <param name="attributes">The telemetry attributes (tags).</param> | ||
| /// <param name="startTime">Optional custom start time for the operation.</param> | ||
| /// <param name="endTime">Optional custom end time for the operation.</param> | ||
| /// <param name="spanId">Optional span ID for the operation. If not provided one will be created.</param> | ||
| /// <param name="parentSpanId">Optional parent span ID for distributed tracing.</param> | ||
| public OutputData( | ||
| IDictionary<string, object?>? attributes = null, | ||
| DateTimeOffset? startTime = null, | ||
| DateTimeOffset? endTime = null, | ||
| string? spanId = null, | ||
| string? parentSpanId = null) | ||
| : base(attributes, startTime, endTime, spanId, parentSpanId) | ||
| { } | ||
|
|
||
| /// <summary> | ||
| /// Gets the name of the operation. | ||
| /// </summary> | ||
| public override string Name => OpenTelemetryConstants.OperationNames.OutputMessages.ToString(); | ||
| } | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
|
|
||
| namespace Microsoft.Agents.A365.Observability.Runtime.Tracing.Contracts | ||
| { | ||
| /// <summary> | ||
| /// Represents a response from an AI agent with output messages. | ||
| /// </summary> | ||
| public sealed class Response : IEquatable<Response> | ||
| { | ||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="Response"/> class. | ||
| /// </summary> | ||
| /// <param name="messages">The output messages from the agent.</param> | ||
| public Response(IReadOnlyList<string>? messages = null) | ||
| { | ||
| Messages = messages ?? Array.Empty<string>(); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets the output messages from the agent response. | ||
| /// </summary> | ||
| public IReadOnlyList<string> Messages { get; } | ||
|
|
||
| /// <inheritdoc/> | ||
| public bool Equals(Response? other) | ||
| { | ||
| if (other is null) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| if (Messages.Count != other.Messages.Count) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| for (int i = 0; i < Messages.Count; i++) | ||
| { | ||
| if (!string.Equals(Messages[i], other.Messages[i], StringComparison.Ordinal)) | ||
| { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| public override bool Equals(object? obj) | ||
| { | ||
| return Equals(obj as Response); | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| public override int GetHashCode() | ||
| { | ||
| unchecked | ||
| { | ||
| int hash = 17; | ||
| foreach (var message in Messages) | ||
| { | ||
| hash = (hash * 31) + (message != null ? StringComparer.Ordinal.GetHashCode(message) : 0); | ||
| } | ||
|
|
||
| return hash; | ||
| } | ||
| } | ||
| } | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System.Collections.Generic; | ||
| using System.Diagnostics; | ||
| using Microsoft.Agents.A365.Observability.Runtime.Tracing.Contracts; | ||
|
|
||
| namespace Microsoft.Agents.A365.Observability.Runtime.Tracing.Scopes | ||
| { | ||
| /// <summary> | ||
| /// Provides OpenTelemetry tracing scope for AI agent output operations. | ||
| /// </summary> | ||
| public sealed class OutputScope : OpenTelemetryScope | ||
| { | ||
| /// <summary> | ||
| /// The operation name for output tracing. | ||
| /// </summary> | ||
| public const string OperationName = "output_messages"; | ||
|
|
||
| private readonly List<string> outputMessages = new List<string>(); | ||
|
|
||
| /// <summary> | ||
| /// Creates and starts a new scope for output tracing. | ||
| /// </summary> | ||
| /// <param name="agentDetails">Information about the agent producing the output.</param> | ||
| /// <param name="tenantDetails">Tenant context used for telemetry enrichment and correlation.</param> | ||
| /// <param name="response">Response containing output messages.</param> | ||
| /// <param name="parentId">Optional parent Activity ID used to link this span to an upstream operation.</param> | ||
| /// <returns>A new OutputScope instance.</returns> | ||
| public static OutputScope Start(AgentDetails agentDetails, TenantDetails tenantDetails, Response response, string? parentId = null) | ||
| => new OutputScope(agentDetails, tenantDetails, response, parentId); | ||
|
|
||
| private OutputScope(AgentDetails agentDetails, TenantDetails tenantDetails, Response response, string? parentId) | ||
| : base( | ||
| kind: ActivityKind.Client, | ||
| agentDetails: agentDetails, | ||
| tenantDetails: tenantDetails, | ||
| operationName: OperationName, | ||
| activityName: $"{OperationName} {agentDetails?.AgentId}", | ||
| parentId: parentId) | ||
| { | ||
| if (response.Messages.Count > 0) | ||
| { | ||
| foreach (var message in response.Messages) | ||
| { | ||
| outputMessages.Add(message); | ||
| } | ||
|
|
||
| SetTagMaybe(OpenTelemetryConstants.GenAiOutputMessagesKey, string.Join(",", outputMessages)); | ||
| } | ||
threddy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| /// <summary> | ||
| /// Records additional output messages and appends them to the existing output messages attribute. | ||
| /// </summary> | ||
| /// <param name="messages">The messages to append to the output.</param> | ||
| public void RecordOutputMessages(IEnumerable<string> messages) | ||
| { | ||
threddy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (messages == null) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| foreach (var message in messages) | ||
| { | ||
| outputMessages.Add(message); | ||
| } | ||
|
|
||
| SetTagMaybe(OpenTelemetryConstants.GenAiOutputMessagesKey, string.Join(",", outputMessages)); | ||
| } | ||
threddy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
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.