Skip to content

Commit 706371c

Browse files
authored
Added a 256kb custom properties truncation limit on specific GenAI attributes (#45749)
* Added a 256kb custom properties truncation limit on specific GenAI attributes * Update CHANGELOG * Move max_length declaration to the top * Retrigger CI/CD pipeline * Fix cspell error * Retrigger CI/CD pipeline * Retrigger CI/CD pipeline * Retrigger CI/CD pipeline * Retrigger CI/CD pipeline
1 parent abef375 commit 706371c

File tree

3 files changed

+25
-11
lines changed

3 files changed

+25
-11
lines changed

sdk/monitor/azure-monitor-opentelemetry-exporter/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
## 1.0.0b49 (Unreleased)
44

55
### Features Added
6-
6+
- Added a 256kb custom properties truncation limit on specific GenAI attributes
7+
([#45749](https://github.com/Azure/azure-sdk-for-python/pull/45749))
78
- Add Browser SDK loader SDK Stats feature bit ([#42904](https://github.com/Azure/azure-sdk-for-python/pull/44162))
89

910
### Breaking Changes

sdk/monitor/azure-monitor-opentelemetry-exporter/azure/monitor/opentelemetry/exporter/_utils.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,7 @@ def _is_any_synthetic_source(properties: Optional[Any]) -> bool:
352352
# pylint: disable=W0622
353353
def _filter_custom_properties(properties: Attributes, filter=None) -> Dict[str, str]:
354354
max_length = 64 * 1024
355+
max_length_for_gen_ai_attributes = 256 * 1024
355356
processed_properties: Dict[str, str] = {}
356357
if not properties:
357358
return processed_properties
@@ -365,7 +366,7 @@ def _filter_custom_properties(properties: Attributes, filter=None) -> Dict[str,
365366
if not key or len(key) > 150 or val is None:
366367
continue
367368
if key in _GEN_AI_ATTRIBUTES:
368-
processed_properties[key] = str(val)
369+
processed_properties[key] = str(val)[:max_length_for_gen_ai_attributes]
369370
else:
370371
processed_properties[key] = str(val)[:max_length]
371372
return processed_properties

sdk/monitor/azure-monitor-opentelemetry-exporter/tests/test_utils.py

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ def test_filter_custom_properties_drops_invalid_entries(self):
5454
self.assertEqual(filtered["short"], "ok")
5555
self.assertNotIn("k" * 151, filtered)
5656

57-
def test_custom_properties_gen_ai_attributes_not_truncated(self):
58-
# All values in _GEN_AI_ATTRIBUTES should not be truncated even when > 64KiB
57+
def test_custom_properties_gen_ai_attributes_not_truncated_at_64kb(self):
58+
# All values in _GEN_AI_ATTRIBUTES should not be truncated at > 64kb but at > 256kb
5959
large_value = "x" * (64 * 1024 + 1000)
6060
properties = {key: large_value for key in _GEN_AI_ATTRIBUTES}
6161
filtered = _utils._filter_custom_properties(properties)
@@ -65,7 +65,7 @@ def test_custom_properties_gen_ai_attributes_not_truncated(self):
6565
self.assertEqual(len(filtered[key]), 64 * 1024 + 1000)
6666

6767
def test_filter_custom_properties_non_gen_ai_truncated_at_64kb(self):
68-
# Regular properties exceeding 64KiB should be truncated
68+
# Regular properties exceeding 64kb should be truncated
6969
max_length = 64 * 1024
7070
large_value = "y" * (max_length + 2000)
7171
properties = {
@@ -81,9 +81,10 @@ def test_filter_custom_properties_non_gen_ai_truncated_at_64kb(self):
8181
self.assertEqual(len(filtered[key]), max_length)
8282

8383
def test_filter_custom_properties_mixed_gen_ai_and_regular(self):
84-
# Gen AI attributes keep full value, regular ones are truncated
84+
# Gen AI attributes truncated at 256kb, regular ones are truncated at 64kb
8585
max_length = 64 * 1024
86-
large_value = "z" * (max_length + 3000)
86+
max_length_for_gen_ai_attributes = 256 * 1024
87+
large_value = "z" * (1024 * 1024 + 3000)
8788
properties = {
8889
"gen_ai.input.messages": large_value,
8990
"gen_ai.output.messages": large_value,
@@ -92,14 +93,25 @@ def test_filter_custom_properties_mixed_gen_ai_and_regular(self):
9293
"db.statement": large_value,
9394
}
9495
filtered = _utils._filter_custom_properties(properties)
95-
# Gen AI attributes — not truncated
96-
self.assertEqual(len(filtered["gen_ai.input.messages"]), max_length + 3000)
97-
self.assertEqual(len(filtered["gen_ai.output.messages"]), max_length + 3000)
98-
# Regular attributes — truncated
96+
97+
self.assertEqual(len(filtered["gen_ai.input.messages"]), max_length_for_gen_ai_attributes)
98+
self.assertEqual(len(filtered["gen_ai.output.messages"]), max_length_for_gen_ai_attributes)
99+
99100
self.assertEqual(len(filtered["gen_ai.agent.version"]), max_length)
100101
self.assertEqual(len(filtered["span_kind"]), max_length)
101102
self.assertEqual(len(filtered["db.statement"]), max_length)
102103

104+
def test_custom_properties_gen_ai_attributes_truncated_at_256kb(self):
105+
# All values in _GEN_AI_ATTRIBUTES should be truncated when > 256kb
106+
max_length_for_gen_ai_attributes = 256 * 1024
107+
large_value = "x" * (256 * 1024 + 1000)
108+
properties = {key: large_value for key in _GEN_AI_ATTRIBUTES}
109+
filtered = _utils._filter_custom_properties(properties)
110+
for key in _GEN_AI_ATTRIBUTES:
111+
with self.subTest(key=key):
112+
self.assertIn(key, filtered)
113+
self.assertEqual(len(filtered[key]), max_length_for_gen_ai_attributes)
114+
103115
def test_nanoseconds_to_duration(self):
104116
ns_to_duration = _utils.ns_to_duration
105117
self.assertEqual(ns_to_duration(0), "0.00:00:00.000")

0 commit comments

Comments
 (0)