// Copyright (c) Microsoft. All rights reserved.
using System;
using Microsoft.Shared.Diagnostics;
namespace Microsoft.Agents.AI;
///
/// Provides extension methods for adding OpenTelemetry instrumentation to instances.
///
public static class OpenTelemetryAgentBuilderExtensions
{
///
/// Adds OpenTelemetry instrumentation to the agent pipeline, enabling comprehensive observability for agent operations.
///
/// The to which OpenTelemetry support will be added.
///
/// An optional source name that will be used to identify telemetry data from this agent.
/// If not specified, a default source name will be used.
///
///
/// An optional callback that provides additional configuration of the instance.
/// This allows for fine-tuning telemetry behavior such as enabling sensitive data collection.
///
/// The with OpenTelemetry instrumentation added, enabling method chaining.
/// is .
///
///
/// This extension adds comprehensive telemetry capabilities to AI agents, including:
///
/// - Distributed tracing of agent invocations
/// - Performance metrics and timing information
/// - Request and response payload logging (when enabled)
/// - Error tracking and exception details
/// - Usage statistics and token consumption metrics
///
///
///
/// The implementation follows the OpenTelemetry Semantic Conventions for Generative AI systems as defined at
/// .
///
///
/// Note: The OpenTelemetry specification for Generative AI is still experimental and subject to change.
/// As the specification evolves, the telemetry output from this agent may also change to maintain compliance.
///
///
public static AIAgentBuilder UseOpenTelemetry(
this AIAgentBuilder builder,
string? sourceName = null,
Action? configure = null) =>
Throw.IfNull(builder).Use((innerAgent, services) =>
{
var agent = new OpenTelemetryAgent(innerAgent, sourceName);
configure?.Invoke(agent);
return agent;
});
}