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 @@ -49,9 +49,10 @@ internal static class LogsHelper
{
if (!properties.ContainsKey(scopeItem.Key))
{
var stringValue = SchemaConstants.TruncationExemptProperties.Contains(scopeItem.Key)
? Convert.ToString(scopeItem.Value, CultureInfo.InvariantCulture)!
: Convert.ToString(scopeItem.Value, CultureInfo.InvariantCulture)?.Truncate(SchemaConstants.MessageData_Properties_MaxValueLength)!;
var maxValueLength = SchemaConstants.GenAiProperties.Contains(scopeItem.Key)
? SchemaConstants.GenAi_Properties_MaxValueLength
: SchemaConstants.MessageData_Properties_MaxValueLength;
var stringValue = Convert.ToString(scopeItem.Value, CultureInfo.InvariantCulture)?.Truncate(maxValueLength)!;
properties.Add(scopeItem.Key, stringValue);
}
}
Expand Down Expand Up @@ -190,9 +191,10 @@ internal static void ProcessLogRecordProperties(LogRecord logRecord, IDictionary
{
if (!properties.ContainsKey(item.Key))
{
var stringValue = SchemaConstants.TruncationExemptProperties.Contains(item.Key)
? item.Value.ToString()!
: item.Value.ToString().Truncate(SchemaConstants.MessageData_Properties_MaxValueLength)!;
var maxValueLength = SchemaConstants.GenAiProperties.Contains(item.Key)
? SchemaConstants.GenAi_Properties_MaxValueLength
: SchemaConstants.MessageData_Properties_MaxValueLength;
var stringValue = item.Value.ToString().Truncate(maxValueLength)!;
properties.Add(item.Key, stringValue);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,16 @@ internal static class SchemaConstants
public const int Tags_AiInternalSdkVersion_MaxLength = 64;

/// <summary>
/// GenAI semantic convention property keys that are exempt from value truncation.
/// Maximum value length for GenAI semantic convention properties (256 KB).
/// These properties may carry large payloads (e.g. full prompt/completion content)
/// and must not be truncated.
/// and are truncated to a higher limit than standard properties.
/// </summary>
public static readonly HashSet<string> TruncationExemptProperties = new HashSet<string>(StringComparer.Ordinal)
public const int GenAi_Properties_MaxValueLength = 256 * 1024;
Comment thread
harsimar marked this conversation as resolved.

/// <summary>
/// GenAI semantic convention property keys that receive a higher truncation limit.
/// </summary>
public static readonly HashSet<string> GenAiProperties = new HashSet<string>(StringComparer.Ordinal)
{
"gen_ai.input.messages",
"gen_ai.output.messages",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,10 @@ internal static void AddKvpToDictionary(IDictionary<string, string> destination,
{
// Note: if Key exceeds MaxLength or if Value is null, the entire KVP will be dropped.
// In case of duplicate keys, only the first occurence will be exported.
var stringValue = SchemaConstants.TruncationExemptProperties.Contains(keyValuePair.Key)
? Convert.ToString(keyValuePair.Value, CultureInfo.InvariantCulture) ?? "null"
: Convert.ToString(keyValuePair.Value, CultureInfo.InvariantCulture).Truncate(SchemaConstants.KVP_MaxValueLength) ?? "null";
var maxValueLength = SchemaConstants.GenAiProperties.Contains(keyValuePair.Key)
? SchemaConstants.GenAi_Properties_MaxValueLength
: SchemaConstants.KVP_MaxValueLength;
var stringValue = Convert.ToString(keyValuePair.Value, CultureInfo.InvariantCulture).Truncate(maxValueLength) ?? "null";
#if NET
destination.TryAdd(keyValuePair.Key, stringValue);
#else
Expand All @@ -130,9 +131,10 @@ internal static void AddKvpToDictionary(IDictionary<string, string> destination,
{
// Note: if Key exceeds MaxLength or if Value is null, the entire KVP will be dropped.
// In case of duplicate keys, only the first occurence will be exported.
var sanitizedValue = SchemaConstants.TruncationExemptProperties.Contains(key)
? value
: value.Truncate(SchemaConstants.KVP_MaxValueLength) ?? "null";
var maxValueLength = SchemaConstants.GenAiProperties.Contains(key)
? SchemaConstants.GenAi_Properties_MaxValueLength
: SchemaConstants.KVP_MaxValueLength;
var sanitizedValue = value.Truncate(maxValueLength) ?? "null";
#if NET
destination.TryAdd(key, sanitizedValue);
#else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@

namespace Azure.Monitor.OpenTelemetry.Exporter.Tests
{
public class GenAiTruncationExemptionTests
public class GenAiTruncationTests
{
private const int LargePayloadLength = 64_000;
/// <summary>
/// A payload larger than the 256 KB GenAI truncation limit to verify truncation occurs.
/// </summary>
private const int LargePayloadLength = 300_000;

private static readonly string s_largePayload = new string('x', LargePayloadLength);

static GenAiTruncationExemptionTests()
static GenAiTruncationTests()
{
Activity.DefaultIdFormat = ActivityIdFormat.W3C;
Activity.ForceDefaultIdFormat = true;
Expand All @@ -45,7 +48,7 @@ static GenAiTruncationExemptionTests()
[InlineData("gen_ai.tool.call.arguments")]
[InlineData("gen_ai.tool.call.result")]
[InlineData("gen_ai.evaluation.explanation")]
public void GenAiProperties_AreNotTruncated_InAddPropertiesToTelemetry(string propertyKey)
public void GenAiProperties_AreTruncatedTo256KB_InAddPropertiesToTelemetry(string propertyKey)
{
// Arrange
IDictionary<string, string> destination = new Dictionary<string, string>();
Expand All @@ -57,8 +60,7 @@ public void GenAiProperties_AreNotTruncated_InAddPropertiesToTelemetry(string pr

// Assert
Assert.True(destination.TryGetValue(propertyKey, out var value));
Assert.Equal(LargePayloadLength, value!.Length);
Assert.Equal(s_largePayload, value);
Assert.Equal(SchemaConstants.GenAi_Properties_MaxValueLength, value!.Length);
}

[Theory]
Expand All @@ -69,7 +71,7 @@ public void GenAiProperties_AreNotTruncated_InAddPropertiesToTelemetry(string pr
[InlineData("gen_ai.tool.call.arguments")]
[InlineData("gen_ai.tool.call.result")]
[InlineData("gen_ai.evaluation.explanation")]
public void GenAiProperties_AreNotTruncated_InAddKvpToDictionary_WithStringOverload(string propertyKey)
public void GenAiProperties_AreTruncatedTo256KB_InAddKvpToDictionary_WithStringOverload(string propertyKey)
{
// Arrange
IDictionary<string, string> destination = new Dictionary<string, string>();
Expand All @@ -79,8 +81,7 @@ public void GenAiProperties_AreNotTruncated_InAddKvpToDictionary_WithStringOverl

// Assert
Assert.True(destination.TryGetValue(propertyKey, out var value));
Assert.Equal(LargePayloadLength, value!.Length);
Assert.Equal(s_largePayload, value);
Assert.Equal(SchemaConstants.GenAi_Properties_MaxValueLength, value!.Length);
}

[Fact]
Expand Down Expand Up @@ -121,7 +122,7 @@ public void NonGenAiProperties_AreTruncated_InAddKvpToDictionary_WithStringOverl
[InlineData("gen_ai.tool.call.arguments")]
[InlineData("gen_ai.tool.call.result")]
[InlineData("gen_ai.evaluation.explanation")]
public void GenAiProperties_AreNotTruncated_InLogRecordAttributes(string propertyKey)
public void GenAiProperties_AreTruncatedTo256KB_InLogRecordAttributes(string propertyKey)
{
// Arrange
var logRecords = new List<LogRecord>();
Expand All @@ -132,10 +133,10 @@ public void GenAiProperties_AreNotTruncated_InLogRecordAttributes(string propert
options.IncludeFormattedMessage = true;
options.AddInMemoryExporter(logRecords);
});
builder.AddFilter(typeof(GenAiTruncationExemptionTests).FullName, LogLevel.Trace);
builder.AddFilter(typeof(GenAiTruncationTests).FullName, LogLevel.Trace);
});

var logger = loggerFactory.CreateLogger<GenAiTruncationExemptionTests>();
var logger = loggerFactory.CreateLogger<GenAiTruncationTests>();

// Use LoggerMessage.Define pattern to add structured attributes
var state = new List<KeyValuePair<string, object?>>
Expand All @@ -151,8 +152,7 @@ public void GenAiProperties_AreNotTruncated_InLogRecordAttributes(string propert

// Assert
Assert.True(properties.TryGetValue(propertyKey, out var value), $"Property '{propertyKey}' should exist in the properties dictionary.");
Assert.Equal(LargePayloadLength, value!.Length);
Assert.Equal(s_largePayload, value);
Assert.Equal(SchemaConstants.GenAi_Properties_MaxValueLength, value!.Length);
}

[Fact]
Expand All @@ -167,10 +167,10 @@ public void NonGenAiProperties_AreTruncated_InLogRecordAttributes()
options.IncludeFormattedMessage = true;
options.AddInMemoryExporter(logRecords);
});
builder.AddFilter(typeof(GenAiTruncationExemptionTests).FullName, LogLevel.Trace);
builder.AddFilter(typeof(GenAiTruncationTests).FullName, LogLevel.Trace);
});

var logger = loggerFactory.CreateLogger<GenAiTruncationExemptionTests>();
var logger = loggerFactory.CreateLogger<GenAiTruncationTests>();

var state = new List<KeyValuePair<string, object?>>
{
Expand All @@ -196,10 +196,10 @@ public void NonGenAiProperties_AreTruncated_InLogRecordAttributes()
[InlineData("gen_ai.tool.call.arguments")]
[InlineData("gen_ai.tool.call.result")]
[InlineData("gen_ai.evaluation.explanation")]
public void GenAiProperties_AreNotTruncated_InActivityCustomDimensions(string propertyKey)
public void GenAiProperties_AreTruncatedTo256KB_InActivityCustomDimensions(string propertyKey)
{
// Arrange
using var activitySource = new ActivitySource(nameof(GenAiTruncationExemptionTests));
using var activitySource = new ActivitySource(nameof(GenAiTruncationTests));
using var activity = activitySource.StartActivity(
"TestActivity",
ActivityKind.Client,
Expand All @@ -214,8 +214,7 @@ public void GenAiProperties_AreNotTruncated_InActivityCustomDimensions(string pr

// Assert
Assert.True(remoteDependencyData.Properties.TryGetValue(propertyKey, out var value), $"Property '{propertyKey}' should exist in dependency custom dimensions.");
Assert.Equal(LargePayloadLength, value!.Length);
Assert.Equal(s_largePayload, value);
Assert.Equal(SchemaConstants.GenAi_Properties_MaxValueLength, value!.Length);
}

[Theory]
Expand All @@ -226,10 +225,10 @@ public void GenAiProperties_AreNotTruncated_InActivityCustomDimensions(string pr
[InlineData("gen_ai.tool.call.arguments")]
[InlineData("gen_ai.tool.call.result")]
[InlineData("gen_ai.evaluation.explanation")]
public void GenAiProperties_AreNotTruncated_InRequestDataCustomDimensions(string propertyKey)
public void GenAiProperties_AreTruncatedTo256KB_InRequestDataCustomDimensions(string propertyKey)
{
// Arrange
using var activitySource = new ActivitySource(nameof(GenAiTruncationExemptionTests));
using var activitySource = new ActivitySource(nameof(GenAiTruncationTests));
using var activity = activitySource.StartActivity(
"TestActivity",
ActivityKind.Server,
Expand All @@ -244,8 +243,7 @@ public void GenAiProperties_AreNotTruncated_InRequestDataCustomDimensions(string

// Assert
Assert.True(requestData.Properties.TryGetValue(propertyKey, out var value), $"Property '{propertyKey}' should exist in request custom dimensions.");
Assert.Equal(LargePayloadLength, value!.Length);
Assert.Equal(s_largePayload, value);
Assert.Equal(SchemaConstants.GenAi_Properties_MaxValueLength, value!.Length);
}
}
}
Loading