Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,7 @@ private static async Task Main()
// Build the workflow by connecting executors sequentially
var workflow = new WorkflowBuilder(uppercase)
.AddEdge(uppercase, reverse)
.WithOpenTelemetry(
// Set `EnableSensitiveData` to true to include message content in traces
configure: cfg => cfg.EnableSensitiveData = true,
activitySource: s_activitySource)
.WithOpenTelemetry(activitySource: s_activitySource)
.Build();

// Execute the workflow with input data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,7 @@ private static async Task Main()
// Build the workflow by connecting executors sequentially
var workflow = new WorkflowBuilder(uppercase)
.AddEdge(uppercase, reverse)
.WithOpenTelemetry(
// Set `EnableSensitiveData` to true to include message content in traces
configure: cfg => cfg.EnableSensitiveData = true,
activitySource: s_activitySource)
.WithOpenTelemetry(activitySource: s_activitySource)
.Build();

// Execute the workflow with input data
Expand Down
7 changes: 1 addition & 6 deletions dotnet/src/Microsoft.Agents.AI.Workflows/Executor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ protected internal virtual ValueTask InitializeAsync(IWorkflowContext context, C

internal async ValueTask<object?> ExecuteCoreAsync(object message, TypeId messageType, IWorkflowContext context, WorkflowTelemetryContext telemetryContext, CancellationToken cancellationToken = default)
{
using var activity = telemetryContext.StartExecutorProcessActivity(this.Id, this.GetType().FullName, messageType.TypeName, message);
using var activity = telemetryContext.StartExecutorProcessActivity(this.Id, this.GetType().FullName, messageType.TypeName);
activity?.CreateSourceLinks(context.TraceContext);

await context.AddEventAsync(new ExecutorInvokedEvent(this.Id, message), cancellationToken).ConfigureAwait(false);
Expand Down Expand Up @@ -298,11 +298,6 @@ protected internal virtual ValueTask InitializeAsync(IWorkflowContext context, C
return null; // Void result.
}

// Output is not available if executor does not return anything, in which case
// messages sent in the handlers of this executor will be set in the message
// send activities.
telemetryContext.SetExecutorOutput(activity, result.Result);

// If we had a real return type, raise it as a SendMessage; TODO: Should we have a way to disable this behaviour?
if (result.Result is not null && this.Options.AutoSendMessageHandlerResultObject)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ public ValueTask AddEventAsync(WorkflowEvent workflowEvent, CancellationToken ca

public async ValueTask SendMessageAsync(string sourceId, object message, string? targetId = null, CancellationToken cancellationToken = default)
{
using Activity? activity = this._workflow.TelemetryContext.StartMessageSendActivity(sourceId, targetId, message);
using Activity? activity = this._workflow.TelemetryContext.StartMessageSendActivity(sourceId, targetId);

// Create a carrier for trace context propagation
var traceContext = activity is null ? null : new Dictionary<string, string>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ internal static class Tags
public const string SessionId = "session.id";
public const string ExecutorId = "executor.id";
public const string ExecutorType = "executor.type";
public const string ExecutorInput = "executor.input";
public const string ExecutorOutput = "executor.output";
public const string MessageType = "message.type";
public const string MessageContent = "message.content";
public const string EdgeGroupType = "edge_group.type";
public const string MessageSourceId = "message.source_id";
public const string MessageTargetId = "message.target_id";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
// Copyright (c) Microsoft. All rights reserved.

using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Text.Json;
using OpenTelemetry;

namespace Microsoft.Agents.AI.Workflows.Observability;
Expand Down Expand Up @@ -146,9 +143,8 @@ public WorkflowTelemetryContext(WorkflowTelemetryOptions options, ActivitySource
/// <param name="executorId">The executor identifier.</param>
/// <param name="executorType">The executor type name.</param>
/// <param name="messageType">The message type name.</param>
/// <param name="message">The input message. Logged only when <see cref="WorkflowTelemetryOptions.EnableSensitiveData"/> is true.</param>
/// <returns>An activity if executor process telemetry is enabled, otherwise null.</returns>
public Activity? StartExecutorProcessActivity(string executorId, string? executorType, string messageType, object? message)
public Activity? StartExecutorProcessActivity(string executorId, string? executorType, string messageType)
{
if (!this.IsEnabled || this.Options.DisableExecutorProcess)
{
Expand All @@ -165,27 +161,9 @@ public WorkflowTelemetryContext(WorkflowTelemetryOptions options, ActivitySource
.SetTag(Tags.ExecutorType, executorType)
.SetTag(Tags.MessageType, messageType);

if (this.Options.EnableSensitiveData)
{
activity.SetTag(Tags.ExecutorInput, SerializeForTelemetry(message));
}

return activity;
}

/// <summary>
/// Sets the executor output tag on an activity when sensitive data logging is enabled.
/// </summary>
/// <param name="activity">The activity to set the output on.</param>
/// <param name="output">The output value to log.</param>
public void SetExecutorOutput(Activity? activity, object? output)
{
if (activity is not null && this.Options.EnableSensitiveData)
{
activity.SetTag(Tags.ExecutorOutput, SerializeForTelemetry(output));
}
}

/// <summary>
/// Starts an edge group process activity if enabled.
/// </summary>
Expand All @@ -205,9 +183,8 @@ public void SetExecutorOutput(Activity? activity, object? output)
/// </summary>
/// <param name="sourceId">The source executor identifier.</param>
/// <param name="targetId">The target executor identifier, if any.</param>
/// <param name="message">The message being sent. Logged only when <see cref="WorkflowTelemetryOptions.EnableSensitiveData"/> is true.</param>
/// <returns>An activity if message send telemetry is enabled, otherwise null.</returns>
public Activity? StartMessageSendActivity(string sourceId, string? targetId, object? message)
public Activity? StartMessageSendActivity(string sourceId, string? targetId)
{
if (!this.IsEnabled || this.Options.DisableMessageSend)
{
Expand All @@ -226,33 +203,6 @@ public void SetExecutorOutput(Activity? activity, object? output)
activity.SetTag(Tags.MessageTargetId, targetId);
}

if (this.Options.EnableSensitiveData)
{
activity.SetTag(Tags.MessageContent, SerializeForTelemetry(message));
}

return activity;
}

[UnconditionalSuppressMessage("ReflectionAnalysis", "IL3050:RequiresDynamicCode", Justification = "Telemetry serialization is optional and only used when explicitly enabled.")]
[UnconditionalSuppressMessage("Trimming", "IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access", Justification = "Telemetry serialization is optional and only used when explicitly enabled.")]
private static string? SerializeForTelemetry(object? value)
{
if (value is null)
{
return null;
}

try
{
return JsonSerializer.Serialize(value, value.GetType());
}
// Telemetry must never fail workflow execution. Serialization can throw arbitrary exceptions
// (unregistered polymorphic types, reflection-disabled AOT, throwing property getters, custom
// converters), so fall back for every serialization failure.
catch (Exception)
{
return $"[Unserializable: {value.GetType().FullName}]";
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Copyright (c) Microsoft. All rights reserved.

using System;

namespace Microsoft.Agents.AI.Workflows.Observability;

/// <summary>
Expand All @@ -8,17 +10,16 @@ namespace Microsoft.Agents.AI.Workflows.Observability;
public sealed class WorkflowTelemetryOptions
{
/// <summary>
/// Gets or sets a value indicating whether potentially sensitive information should be included in telemetry.
/// Gets or sets a legacy option that no longer affects workflow telemetry.
/// </summary>
/// <value>
/// <see langword="true"/> if potentially sensitive information should be included in telemetry;
/// <see langword="false"/> if telemetry shouldn't include raw inputs and outputs.
/// The default value is <see langword="false"/>.
/// The value is ignored. The default value is <see langword="false"/>.
/// </value>
/// <remarks>
/// By default, telemetry includes metadata but not raw inputs and outputs,
/// such as message content and executor data.
/// Workflow payload attributes are no longer emitted. This option is deprecated
/// and will be removed in version 1.30.0. Agent and chat-client payload capture settings are unaffected.
/// </remarks>
[Obsolete("Workflow payload attributes are no longer emitted. This option has no effect and will be removed in version 1.30.0.")]
public bool EnableSensitiveData { get; set; }

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static class OpenTelemetryWorkflowBuilderExtensions
/// <param name="builder">The <see cref="WorkflowBuilder"/> to which OpenTelemetry support will be added.</param>
/// <param name="configure">
/// An optional callback that provides additional configuration of the <see cref="WorkflowTelemetryOptions"/> instance.
/// This allows for fine-tuning telemetry behavior such as enabling sensitive data collection.
/// This allows for fine-tuning telemetry behavior such as disabling individual activity types.
/// </param>
/// <param name="activitySource">
/// An optional <see cref="ActivitySource"/> to use for telemetry. If provided, this activity source will be used
Expand Down Expand Up @@ -46,7 +46,7 @@ public static class OpenTelemetryWorkflowBuilderExtensions
/// <code>
/// var workflow = new WorkflowBuilder(startExecutor)
/// .AddEdge(executor1, executor2)
/// .WithOpenTelemetry(cfg => cfg.EnableSensitiveData = true)
/// .WithOpenTelemetry()
/// .Build();
/// </code>
/// </example>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public void ConfigureTelemetry_CanBeSet()
ConfigureTelemetry = opt =>
{
callbackInvoked = true;
opt.EnableSensitiveData = true;
opt.DisableEdgeGroupProcess = true;
}
};

Expand All @@ -102,7 +102,7 @@ public void ConfigureTelemetry_CanBeSet()
WorkflowTelemetryOptions telemetryOptions = new();
options.ConfigureTelemetry(telemetryOptions);
Assert.True(callbackInvoked);
Assert.True(telemetryOptions.EnableSensitiveData);
Assert.True(telemetryOptions.DisableEdgeGroupProcess);
}

[Fact]
Expand Down Expand Up @@ -201,7 +201,7 @@ public async Task BuildWorkflow_WithConfigureTelemetry_AppliesConfigurationAsync
ConfigureTelemetry = opt =>
{
configureInvoked = true;
opt.EnableSensitiveData = true;
opt.DisableEdgeGroupProcess = true;
},
LoggerFactory = NullLoggerFactory.Instance
};
Expand All @@ -222,6 +222,7 @@ public async Task BuildWorkflow_WithConfigureTelemetry_AppliesConfigurationAsync
Assert.NotEmpty(capturedActivities);
Assert.Contains(capturedActivities, a => a.OperationName.StartsWith(WorkflowBuildActivityName, StringComparison.Ordinal));
Assert.Contains(capturedActivities, a => a.OperationName.StartsWith(WorkflowRunActivityName, StringComparison.Ordinal));
Assert.DoesNotContain(capturedActivities, a => a.OperationName.StartsWith("edge_group.process", StringComparison.Ordinal));
}

[Fact]
Expand Down
Loading
Loading