Skip to content

Commit 0d3f679

Browse files
Copilotharsimar
andauthored
Truncate gen_ai attributes to 256KB instead of exempting from truncation (#48454)
* Initial plan * Truncate genai attributes to 256KB instead of exempting from truncation Co-authored-by: harsimar <19897860+harsimar@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: harsimar <19897860+harsimar@users.noreply.github.com> Co-authored-by: Harsimar Kaur <harskaur@microsoft.com>
1 parent 3acaa86 commit 0d3f679

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

sdk/monitor/azure-monitor-opentelemetry-autoconfigure/src/main/java/com/azure/monitor/opentelemetry/autoconfigure/implementation/builders/AbstractTelemetryBuilder.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@ public abstract class AbstractTelemetryBuilder {
2323

2424
private static final int MAX_PROPERTY_KEY_LENGTH = 150;
2525
private static final int MAX_PROPERTY_VALUE_LENGTH = 8192;
26+
private static final int MAX_GENAI_PROPERTY_VALUE_LENGTH = 256 * 1024; // 256 KB
2627

2728
// gen_ai properties can contain large payloads (e.g. full conversation messages,
28-
// tool definitions) that should not be truncated
29-
private static final Set<String> TRUNCATION_EXEMPT_PROPERTY_KEYS = new HashSet<>(asList("gen_ai.input.messages",
29+
// tool definitions) that are truncated at a higher limit (256 KB)
30+
private static final Set<String> GENAI_PROPERTY_KEYS = new HashSet<>(asList("gen_ai.input.messages",
3031
"gen_ai.output.messages", "gen_ai.system_instructions", "gen_ai.tool.definitions", "gen_ai.tool.call.arguments",
3132
"gen_ai.tool.call.result", "gen_ai.evaluation.explanation"));
3233

@@ -89,8 +90,9 @@ public void addProperty(@Nullable String key, @Nullable String value) {
8990
// TODO (trask) log
9091
return;
9192
}
92-
if (TRUNCATION_EXEMPT_PROPERTY_KEYS.contains(key)) {
93-
getProperties().put(key, value);
93+
if (GENAI_PROPERTY_KEYS.contains(key)) {
94+
getProperties().put(key,
95+
TelemetryTruncation.truncatePropertyValue(value, MAX_GENAI_PROPERTY_VALUE_LENGTH, key));
9496
} else {
9597
getProperties().put(key, TelemetryTruncation.truncatePropertyValue(value, MAX_PROPERTY_VALUE_LENGTH, key));
9698
}

sdk/monitor/azure-monitor-opentelemetry-autoconfigure/src/test/java/com/azure/monitor/opentelemetry/autoconfigure/implementation/builders/AbstractTelemetryBuilderTest.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,28 @@ public void regularPropertyIsTruncatedAt8192() {
3535
"gen_ai.tool.call.arguments",
3636
"gen_ai.tool.call.result",
3737
"gen_ai.evaluation.explanation" })
38-
public void genAiPropertyIsNotTruncated(String key) {
38+
public void genAiPropertyIsTruncatedAt256KB(String key) {
39+
MessageTelemetryBuilder builder = MessageTelemetryBuilder.create();
40+
int size256KB = 256 * 1024;
41+
String longValue = repeat("a", size256KB + 10000);
42+
builder.addProperty(key, longValue);
43+
44+
TelemetryItem item = builder.build();
45+
Map<String, String> properties = getProperties(item);
46+
assertEquals(size256KB, properties.get(key).length());
47+
}
48+
49+
@ParameterizedTest
50+
@ValueSource(
51+
strings = {
52+
"gen_ai.input.messages",
53+
"gen_ai.output.messages",
54+
"gen_ai.system_instructions",
55+
"gen_ai.tool.definitions",
56+
"gen_ai.tool.call.arguments",
57+
"gen_ai.tool.call.result",
58+
"gen_ai.evaluation.explanation" })
59+
public void genAiPropertyUnder256KBIsNotTruncated(String key) {
3960
MessageTelemetryBuilder builder = MessageTelemetryBuilder.create();
4061
String longValue = repeat("a", 50000);
4162
builder.addProperty(key, longValue);

0 commit comments

Comments
 (0)