// Copyright (c) Microsoft. All rights reserved.
using System;
using System.Diagnostics;
using Microsoft.Agents.AI.Workflows.Observability;
using Microsoft.Shared.Diagnostics;
namespace Microsoft.Agents.AI.Workflows;
///
/// Provides extension methods for adding OpenTelemetry instrumentation to instances.
///
public static class OpenTelemetryWorkflowBuilderExtensions
{
///
/// Enables OpenTelemetry instrumentation for the workflow, providing comprehensive observability for workflow operations.
///
/// The to which OpenTelemetry support will be added.
///
/// An optional callback that provides additional configuration of the instance.
/// This allows for fine-tuning telemetry behavior such as enabling sensitive data collection.
///
///
/// An optional to use for telemetry. If provided, this activity source will be used
/// directly and the caller retains ownership (responsible for disposal). If , a shared
/// default activity source named "Microsoft.Agents.AI.Workflows" will be used.
///
/// The with OpenTelemetry instrumentation enabled, enabling method chaining.
/// is .
///
///
/// This extension adds comprehensive telemetry capabilities to workflows, including:
///
/// - Distributed tracing of workflow execution
/// - Executor invocation and processing spans
/// - Edge routing and message delivery spans
/// - Workflow build and validation spans
/// - Error tracking and exception details
///
///
///
/// By default, workflow telemetry is disabled. Call this method to enable telemetry collection.
///
///
///
///
/// var workflow = new WorkflowBuilder(startExecutor)
/// .AddEdge(executor1, executor2)
/// .WithOpenTelemetry(cfg => cfg.EnableSensitiveData = true)
/// .Build();
///
///
public static WorkflowBuilder WithOpenTelemetry(
this WorkflowBuilder builder,
Action? configure = null,
ActivitySource? activitySource = null)
{
Throw.IfNull(builder);
WorkflowTelemetryOptions options = new();
configure?.Invoke(options);
WorkflowTelemetryContext context = new(options, activitySource);
builder.SetTelemetryContext(context);
return builder;
}
}