Skip to content
Draft
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 @@ -5,6 +5,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.Metrics;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.ExceptionServices;
Expand Down Expand Up @@ -79,6 +80,10 @@ public class FunctionInvokingChatClient : DelegatingChatClient
/// <remarks>This component does not own the instance and should not dispose it.</remarks>
private readonly ActivitySource? _activitySource;

/// <summary>The <see cref="Meter"/> to use for metrics.</summary>
/// <remarks>This component does not own the instance and should not dispose it.</remarks>
private readonly Meter? _meter;

/// <summary>
/// Initializes a new instance of the <see cref="FunctionInvokingChatClient"/> class.
/// </summary>
Expand All @@ -90,6 +95,7 @@ public FunctionInvokingChatClient(IChatClient innerClient, ILoggerFactory? logge
{
_logger = (ILogger?)loggerFactory?.CreateLogger<FunctionInvokingChatClient>() ?? NullLogger.Instance;
_activitySource = innerClient.GetService<ActivitySource>();
_meter = innerClient.GetService<Meter>();
FunctionInvocationServices = functionInvocationServices;
}

Expand All @@ -101,7 +107,8 @@ public FunctionInvokingChatClient(IChatClient innerClient, ILoggerFactory? logge
invokeAgentActivity =>
invokeAgentActivity is not null
? invokeAgentActivity.GetCustomProperty(OpenTelemetryChatClient.SensitiveDataEnabledCustomKey) as string is OpenTelemetryChatClient.SensitiveDataEnabledTrueValue
: InnerClient.GetService<OpenTelemetryChatClient>()?.EnableSensitiveData is true);
: InnerClient.GetService<OpenTelemetryChatClient>()?.EnableSensitiveData is true,
_meter is not null ? OtelMetricHelpers.CreateGenAIExecuteToolDurationHistogram(_meter) : null);

/// <summary>
/// Gets or sets the <see cref="FunctionInvocationContext"/> for the current function invocation.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace Microsoft.Extensions.AI;

/// <summary>Represents a delegating chat client that implements the OpenTelemetry Semantic Conventions for Generative AI systems.</summary>
/// <remarks>
/// This class provides an implementation of the Semantic Conventions for Generative AI systems v1.41, defined at <see href="https://opentelemetry.io/docs/specs/semconv/gen-ai/" />.
/// This class provides an implementation of the GenAI Semantic Conventions v1.41, defined at <see href="https://opentelemetry.io/docs/specs/semconv/gen-ai/" />.
/// The specification is still experimental and subject to change; as such, the telemetry output by this client is also subject to change.
/// </remarks>
public sealed partial class OpenTelemetryChatClient : DelegatingChatClient
Expand Down Expand Up @@ -129,6 +129,7 @@ protected override void Dispose(bool disposing)
/// <inheritdoc/>
public override object? GetService(Type serviceType, object? serviceKey = null) =>
serviceType == typeof(ActivitySource) ? _activitySource :
serviceType == typeof(Meter) ? _meter :
base.GetService(serviceType, serviceKey);

/// <inheritdoc/>
Expand Down Expand Up @@ -353,6 +354,24 @@ public override async IAsyncEnumerable<ChatResponseUpdate> GetStreamingResponseA
_ = activity.AddTag(OpenTelemetryConsts.GenAI.Request.TopP, top_p);
}

if (options.Reasoning?.Effort is ReasoningEffort reasoningEffort)
{
string? reasoningLevel = reasoningEffort switch
{
ReasoningEffort.None => "none",
ReasoningEffort.Low => "low",
ReasoningEffort.Medium => "medium",
ReasoningEffort.High => "high",
ReasoningEffort.ExtraHigh => "extrahigh",
_ => null,
};

if (reasoningLevel is not null)
{
_ = activity.AddTag(OpenTelemetryConsts.GenAI.Request.ReasoningLevel, reasoningLevel);
}
}

if (options.ResponseFormat is not null)
{
switch (options.ResponseFormat)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace Microsoft.Extensions.AI;

/// <summary>Represents a delegating image generator that implements the OpenTelemetry Semantic Conventions for Generative AI systems.</summary>
/// <remarks>
/// This class provides an implementation of the Semantic Conventions for Generative AI systems v1.41, defined at <see href="https://opentelemetry.io/docs/specs/semconv/gen-ai/" />.
/// This class provides an implementation of the GenAI Semantic Conventions v1.41, defined at <see href="https://opentelemetry.io/docs/specs/semconv/gen-ai/" />.
/// The specification is still experimental and subject to change; as such, the telemetry output by this client is also subject to change.
/// </remarks>
[Experimental(DiagnosticIds.Experiments.AIImageGeneration, UrlFormat = DiagnosticIds.UrlFormat)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.Metrics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -22,6 +23,7 @@ internal sealed class FunctionInvocationProcessor
{
private readonly ILogger _logger;
private readonly ActivitySource? _activitySource;
private readonly Histogram<double>? _executeToolDurationHistogram;
private readonly Func<FunctionInvocationContext, CancellationToken, ValueTask<object?>> _invokeFunction;
private readonly Func<Activity?, bool> _isSensitiveDataEnabled;

Expand All @@ -36,16 +38,19 @@ internal sealed class FunctionInvocationProcessor
/// Receives the invoke agent activity (or null if not in agent context).
/// Returns true if sensitive data should be logged/tagged, false otherwise.
/// </param>
/// <param name="executeToolDurationHistogram">An optional histogram for recording <c>gen_ai.execute_tool.duration</c> metric values.</param>
public FunctionInvocationProcessor(
ILogger logger,
ActivitySource? activitySource,
Func<FunctionInvocationContext, CancellationToken, ValueTask<object?>> invokeFunction,
Func<Activity?, bool>? isSensitiveDataEnabled = null)
Func<Activity?, bool>? isSensitiveDataEnabled = null,
Histogram<double>? executeToolDurationHistogram = null)
{
_logger = logger;
_activitySource = activitySource;
_invokeFunction = invokeFunction;
_isSensitiveDataEnabled = isSensitiveDataEnabled ?? (_ => false);
_executeToolDurationHistogram = executeToolDurationHistogram;
}

/// <summary>
Expand Down Expand Up @@ -199,15 +204,17 @@ private async Task<FunctionInvocationResult> ProcessSingleFunctionCallAsync(
}

object? result = null;
string? errorType = null;
try
{
result = await _invokeFunction(context, cancellationToken).ConfigureAwait(false);
}
catch (Exception e)
{
errorType = e.GetType().FullName;
if (activity is not null)
{
_ = activity.SetTag(OpenTelemetryConsts.Error.Type, e.GetType().FullName)
_ = activity.SetTag(OpenTelemetryConsts.Error.Type, errorType)
.SetStatus(ActivityStatusCode.Error, e.Message);
}

Expand All @@ -224,6 +231,19 @@ private async Task<FunctionInvocationResult> ProcessSingleFunctionCallAsync(
}
finally
{
if (_executeToolDurationHistogram?.Enabled is true)
{
TagList tags = default;
tags.Add(OpenTelemetryConsts.GenAI.Tool.Name, context.Function.Name);
tags.Add(OpenTelemetryConsts.GenAI.Tool.Type, OpenTelemetryConsts.ToolTypeFunction);
if (errorType is not null)
{
tags.Add(OpenTelemetryConsts.Error.Type, errorType);
}

_executeToolDurationHistogram.Record(FunctionInvocationHelpers.GetElapsedTime(startingTimestamp).TotalSeconds, tags);
}

bool loggedResult = false;
if (enableSensitiveData || traceLoggingEnabled)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ internal static string SerializeChatMessages(
topLevel.Equals("image", StringComparison.OrdinalIgnoreCase) ? "image" :
topLevel.Equals("audio", StringComparison.OrdinalIgnoreCase) ? "audio" :
topLevel.Equals("video", StringComparison.OrdinalIgnoreCase) ? "video" :
topLevel.Equals("application", StringComparison.OrdinalIgnoreCase) ? "document" :
null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,12 @@ public static Histogram<double> CreateGenAIOperationDurationHistogram(Meter mete
OpenTelemetryConsts.SecondsUnit,
OpenTelemetryConsts.GenAI.Client.OperationDuration.Description,
advice: new() { HistogramBucketBoundaries = OpenTelemetryConsts.GenAI.Client.OperationDuration.ExplicitBucketBoundaries });

/// <summary>Creates the standard <c>gen_ai.execute_tool.duration</c> histogram on <paramref name="meter"/>.</summary>
public static Histogram<double> CreateGenAIExecuteToolDurationHistogram(Meter meter) =>
meter.CreateHistogram<double>(
OpenTelemetryConsts.GenAI.ExecuteTool.Duration.Name,
OpenTelemetryConsts.SecondsUnit,
OpenTelemetryConsts.GenAI.ExecuteTool.Duration.Description,
advice: new() { HistogramBucketBoundaries = OpenTelemetryConsts.GenAI.ExecuteTool.Duration.ExplicitBucketBoundaries });
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace Microsoft.Extensions.AI;

/// <summary>Represents a delegating embedding generator that implements the OpenTelemetry Semantic Conventions for Generative AI systems.</summary>
/// <remarks>
/// This class provides an implementation of the Semantic Conventions for Generative AI systems v1.41, defined at <see href="https://opentelemetry.io/docs/specs/semconv/gen-ai/" />.
/// This class provides an implementation of the GenAI Semantic Conventions v1.41, defined at <see href="https://opentelemetry.io/docs/specs/semconv/gen-ai/" />.
/// The specification is still experimental and subject to change; as such, the telemetry output by this client is also subject to change.
/// </remarks>
/// <typeparam name="TInput">The type of input used to produce embeddings.</typeparam>
Expand Down
11 changes: 11 additions & 0 deletions src/Libraries/Microsoft.Extensions.AI/OpenTelemetryConsts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,16 @@ public static class TimePerOutputChunk
}
}

public static class ExecuteTool
{
public static class Duration
{
public const string Description = "Measures the duration of a single tool execution";
public const string Name = "gen_ai.execute_tool.duration";
public static readonly double[] ExplicitBucketBoundaries = Client.OperationDuration.ExplicitBucketBoundaries;
}
}

public static class Conversation
{
public const string Id = "gen_ai.conversation.id";
Expand Down Expand Up @@ -128,6 +138,7 @@ public static class Request
public const string Model = "gen_ai.request.model";
public const string MaxTokens = "gen_ai.request.max_tokens";
public const string PresencePenalty = "gen_ai.request.presence_penalty";
public const string ReasoningLevel = "gen_ai.request.reasoning.level";
public const string Seed = "gen_ai.request.seed";
public const string StopSequences = "gen_ai.request.stop_sequences";
public const string Stream = "gen_ai.request.stream";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.Metrics;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -67,6 +68,10 @@ internal sealed class FunctionInvokingRealtimeClientSession : IRealtimeClientSes
/// <remarks>This component does not own the instance and should not dispose it.</remarks>
private readonly ActivitySource? _activitySource;

/// <summary>The <see cref="Meter"/> to use for metrics.</summary>
/// <remarks>This component does not own the instance and should not dispose it.</remarks>
private readonly Meter? _meter;

/// <summary>The inner session to delegate to.</summary>
private readonly IRealtimeClientSession _innerSession;

Expand All @@ -86,14 +91,16 @@ public FunctionInvokingRealtimeClientSession(IRealtimeClientSession innerSession
_client = Throw.IfNull(client);
_logger = (ILogger?)loggerFactory?.CreateLogger<FunctionInvokingRealtimeClientSession>() ?? NullLogger.Instance;
_activitySource = innerSession.GetService<ActivitySource>();
_meter = innerSession.GetService<Meter>();
FunctionInvocationServices = functionInvocationServices;
}

/// <summary>Gets the function invocation processor, creating it lazily.</summary>
private FunctionInvocationProcessor Processor => field ??= new FunctionInvocationProcessor(
_logger,
_activitySource,
InvokeFunctionAsync);
InvokeFunctionAsync,
executeToolDurationHistogram: _meter is not null ? OtelMetricHelpers.CreateGenAIExecuteToolDurationHistogram(_meter) : null);

/// <summary>
/// Gets or sets the <see cref="FunctionInvocationContext"/> for the current function invocation.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace Microsoft.Extensions.AI;
/// <summary>Represents a delegating realtime session that follows the OpenTelemetry Semantic Conventions for Generative AI systems where applicable.</summary>
/// <remarks>
/// <para>
/// This class follows the patterns of the Semantic Conventions for Generative AI systems v1.41 where applicable, as defined at
/// This class follows the patterns of the GenAI Semantic Conventions v1.41 where applicable, as defined at
/// <see href="https://opentelemetry.io/docs/specs/semconv/gen-ai/" />, with custom extensions for realtime-specific behavior.
/// The specification does not currently define a realtime operation; a custom operation name is used.
/// </para>
Expand Down Expand Up @@ -168,6 +168,7 @@ public async ValueTask DisposeAsync()

return
serviceType == typeof(ActivitySource) ? _activitySource :
serviceType == typeof(Meter) ? _meter :
serviceKey is null && serviceType.IsInstanceOfType(this) ? this :
_innerSession.GetService(serviceType, serviceKey);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace Microsoft.Extensions.AI;

/// <summary>Represents a delegating speech-to-text client that implements the OpenTelemetry Semantic Conventions for Generative AI systems.</summary>
/// <remarks>
/// This class provides an implementation of the Semantic Conventions for Generative AI systems v1.41, defined at <see href="https://opentelemetry.io/docs/specs/semconv/gen-ai/" />.
/// This class provides an implementation of the GenAI Semantic Conventions v1.41, defined at <see href="https://opentelemetry.io/docs/specs/semconv/gen-ai/" />.
/// The specification is still experimental and subject to change; as such, the telemetry output by this client is also subject to change.
/// </remarks>
[Experimental(DiagnosticIds.Experiments.AISpeechToText, UrlFormat = DiagnosticIds.UrlFormat)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace Microsoft.Extensions.AI;

/// <summary>Represents a delegating text-to-speech client that implements the OpenTelemetry Semantic Conventions for Generative AI systems.</summary>
/// <remarks>
/// This class provides an implementation of the Semantic Conventions for Generative AI systems v1.41, defined at <see href="https://opentelemetry.io/docs/specs/semconv/gen-ai/" />.
/// This class provides an implementation of the GenAI Semantic Conventions v1.41, defined at <see href="https://opentelemetry.io/docs/specs/semconv/gen-ai/" />.
/// The specification is still experimental and subject to change; as such, the telemetry output by this client is also subject to change.
/// </remarks>
[Experimental(DiagnosticIds.Experiments.AITextToSpeech, UrlFormat = DiagnosticIds.UrlFormat)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3639,5 +3639,50 @@ public async Task ServerHandledFunctionCalls_NoMatchingFRC_StillInvoked(bool str
Assert.Contains(response.Messages, m =>
m.Contents.Any(c => c is FunctionResultContent frc2 && frc2.CallId == "callId1"));
}
}

[Theory]
[InlineData(false)]
[InlineData(true)]
public async Task ExecuteToolDuration_MetricRecorded(bool streaming)
{
string sourceName = Guid.NewGuid().ToString();

ChatOptions options = new()
{
Tools = [AIFunctionFactory.Create(() => "Result 1", "Func1")]
};

List<ChatMessage> plan =
[
new ChatMessage(ChatRole.User, "hello"),
new ChatMessage(ChatRole.Assistant, [new FunctionCallContent("callId1", "Func1")]),
new ChatMessage(ChatRole.Tool, [new FunctionResultContent("callId1", result: "Result 1")]),
new ChatMessage(ChatRole.Assistant, "world"),
];

using var metricCollector = new Microsoft.Extensions.Diagnostics.Metrics.Testing.MetricCollector<double>(null, sourceName, "gen_ai.execute_tool.duration");

Func<ChatClientBuilder, ChatClientBuilder> configurePipeline = b =>
{
b.UseFunctionInvocation();
b.UseOpenTelemetry(sourceName: sourceName);
return b;
};

if (streaming)
{
await InvokeAndAssertStreamingAsync(options, plan, configurePipeline: configurePipeline);
}
else
{
await InvokeAndAssertAsync(options, plan, configurePipeline: configurePipeline);
}

var measurements = metricCollector.GetMeasurementSnapshot();
var measurement = Assert.Single(measurements);
Assert.True(measurement.Value >= 0);
Assert.True(measurement.ContainsTags(
new KeyValuePair<string, object?>("gen_ai.tool.name", "Func1"),
new KeyValuePair<string, object?>("gen_ai.tool.type", "function")));
}
}
Loading
Loading