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 @@ -364,20 +364,13 @@ private static string SerializeChatMessages(IEnumerable<ChatMessage> messages, C
}
}

if (_providerName is not null)
// Log all additional request options as raw values on the span.
// Since AdditionalProperties has undefined meaning, we treat it as potentially sensitive data.
if (EnableSensitiveData && options.AdditionalProperties is { } props)
{
// Since AdditionalProperties has undefined meaning, we treat it as potentially sensitive data
if (EnableSensitiveData && options.AdditionalProperties is { } props)
foreach (KeyValuePair<string, object?> prop in props)
{
// Log all additional request options as per-provider tags. This is non-normative, but it covers cases where
// there's a per-provider specification in a best-effort manner (e.g. gen_ai.openai.request.service_tier),
// and more generally cases where there's additional useful information to be logged.
foreach (KeyValuePair<string, object?> prop in props)
{
_ = activity.AddTag(
OpenTelemetryConsts.GenAI.Request.PerProvider(_providerName, JsonNamingPolicy.SnakeCaseLower.ConvertName(prop.Key)),
prop.Value);
}
_ = activity.AddTag(prop.Key, prop.Value);
}
}
}
Expand Down Expand Up @@ -467,20 +460,13 @@ private void TraceResponse(
_ = activity.AddTag(OpenTelemetryConsts.GenAI.Usage.OutputTokens, (int)outputTokens);
}

if (_providerName is not null)
// Log all additional response properties as raw values on the span.
// Since AdditionalProperties has undefined meaning, we treat it as potentially sensitive data.
if (EnableSensitiveData && response.AdditionalProperties is { } props)
{
// Since AdditionalProperties has undefined meaning, we treat it as potentially sensitive data
if (EnableSensitiveData && response.AdditionalProperties is { } props)
foreach (KeyValuePair<string, object?> prop in props)
{
// Log all additional response properties as per-provider tags. This is non-normative, but it covers cases where
// there's a per-provider specification in a best-effort manner (e.g. gen_ai.openai.response.system_fingerprint),
// and more generally cases where there's additional useful information to be logged.
foreach (KeyValuePair<string, object?> prop in props)
{
_ = activity.AddTag(
OpenTelemetryConsts.GenAI.Response.PerProvider(_providerName, JsonNamingPolicy.SnakeCaseLower.ConvertName(prop.Key)),
prop.Value);
}
_ = activity.AddTag(prop.Key, prop.Value);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using System.Diagnostics;
using System.Diagnostics.Metrics;
using System.Linq;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -175,19 +174,13 @@ protected override void Dispose(bool disposing)
_ = activity.AddTag(OpenTelemetryConsts.GenAI.Request.EmbeddingDimensions, dimensionsValue);
}

// Log all additional request options as per-provider tags. This is non-normative, but it covers cases where
// there's a per-provider specification in a best-effort manner (e.g. gen_ai.openai.request.service_tier),
// and more generally cases where there's additional useful information to be logged.
// Log all additional request options as raw values on the span.
// Since AdditionalProperties has undefined meaning, we treat it as potentially sensitive data.
if (EnableSensitiveData &&
_providerName is not null &&
options?.AdditionalProperties is { } props)
if (EnableSensitiveData && options?.AdditionalProperties is { } props)
{
foreach (KeyValuePair<string, object?> prop in props)
{
_ = activity.AddTag(
OpenTelemetryConsts.GenAI.Request.PerProvider(_providerName, JsonNamingPolicy.SnakeCaseLower.ConvertName(prop.Key)),
prop.Value);
_ = activity.AddTag(prop.Key, prop.Value);
}
}
}
Expand Down Expand Up @@ -255,18 +248,13 @@ private void TraceResponse(
_ = activity.AddTag(OpenTelemetryConsts.GenAI.Response.Model, responseModelId);
}

// Log all additional response properties as per-provider tags. This is non-normative, but it covers cases where
// there's a per-provider specification in a best-effort manner (e.g. gen_ai.openai.response.system_fingerprint),
// and more generally cases where there's additional useful information to be logged.
if (EnableSensitiveData &&
_providerName is not null &&
embeddings?.AdditionalProperties is { } props)
// Log all additional response properties as raw values on the span.
// Since AdditionalProperties has undefined meaning, we treat it as potentially sensitive data.
if (EnableSensitiveData && embeddings?.AdditionalProperties is { } props)
{
foreach (KeyValuePair<string, object?> prop in props)
{
_ = activity.AddTag(
OpenTelemetryConsts.GenAI.Response.PerProvider(_providerName, JsonNamingPolicy.SnakeCaseLower.ConvertName(prop.Key)),
prop.Value);
_ = activity.AddTag(prop.Key, prop.Value);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,17 +90,13 @@ public static class Request
public const string Temperature = "gen_ai.request.temperature";
public const string TopK = "gen_ai.request.top_k";
public const string TopP = "gen_ai.request.top_p";

public static string PerProvider(string providerName, string parameterName) => $"{providerName}.request.{parameterName}";
}

public static class Response
{
public const string FinishReasons = "gen_ai.response.finish_reasons";
public const string Id = "gen_ai.response.id";
public const string Model = "gen_ai.response.model";

public static string PerProvider(string providerName, string parameterName) => $"{providerName}.response.{parameterName}";
}

public static class Token
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,16 +163,16 @@ async static IAsyncEnumerable<ChatResponseUpdate> CallbackAsync(
Assert.Equal(7, activity.GetTagItem("gen_ai.request.top_k"));
Assert.Equal(123, activity.GetTagItem("gen_ai.request.max_tokens"));
Assert.Equal("""["hello", "world"]""", activity.GetTagItem("gen_ai.request.stop_sequences"));
Assert.Equal(enableSensitiveData ? "value1" : null, activity.GetTagItem("testservice.request.service_tier"));
Assert.Equal(enableSensitiveData ? "value2" : null, activity.GetTagItem("testservice.request.something_else"));
Assert.Equal(enableSensitiveData ? "value1" : null, activity.GetTagItem("service_tier"));
Assert.Equal(enableSensitiveData ? "value2" : null, activity.GetTagItem("SomethingElse"));
Assert.Equal(42L, activity.GetTagItem("gen_ai.request.seed"));

Assert.Equal("id123", activity.GetTagItem("gen_ai.response.id"));
Assert.Equal("""["stop"]""", activity.GetTagItem("gen_ai.response.finish_reasons"));
Assert.Equal(10, activity.GetTagItem("gen_ai.usage.input_tokens"));
Assert.Equal(20, activity.GetTagItem("gen_ai.usage.output_tokens"));
Assert.Equal(enableSensitiveData ? "abcdefgh" : null, activity.GetTagItem("testservice.response.system_fingerprint"));
Assert.Equal(enableSensitiveData ? "value2" : null, activity.GetTagItem("testservice.response.and_something_else"));
Assert.Equal(enableSensitiveData ? "abcdefgh" : null, activity.GetTagItem("system_fingerprint"));
Assert.Equal(enableSensitiveData ? "value2" : null, activity.GetTagItem("AndSomethingElse"));

Assert.True(activity.Duration.TotalMilliseconds > 0);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,12 @@ public async Task ExpectedInformationLogged_Async(string? perRequestModelId, boo

Assert.Equal(expectedModelName, activity.GetTagItem("gen_ai.request.model"));
Assert.Equal(1234, activity.GetTagItem("gen_ai.request.embedding.dimensions"));
Assert.Equal(enableSensitiveData ? "value1" : null, activity.GetTagItem("testservice.request.service_tier"));
Assert.Equal(enableSensitiveData ? "value2" : null, activity.GetTagItem("testservice.request.something_else"));
Assert.Equal(enableSensitiveData ? "value1" : null, activity.GetTagItem("service_tier"));
Assert.Equal(enableSensitiveData ? "value2" : null, activity.GetTagItem("SomethingElse"));

Assert.Equal(10, activity.GetTagItem("gen_ai.usage.input_tokens"));
Assert.Equal(enableSensitiveData ? "abcdefgh" : null, activity.GetTagItem("testservice.response.system_fingerprint"));
Assert.Equal(enableSensitiveData ? "value3" : null, activity.GetTagItem("testservice.response.and_something_else"));
Assert.Equal(enableSensitiveData ? "abcdefgh" : null, activity.GetTagItem("system_fingerprint"));
Assert.Equal(enableSensitiveData ? "value3" : null, activity.GetTagItem("AndSomethingElse"));

Assert.True(activity.Duration.TotalMilliseconds > 0);
}
Expand Down
Loading