Skip to content

Commit 5f3dfa7

Browse files
authored
Fix span attribute value truncation (#173)
1 parent 6c20782 commit 5f3dfa7

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

opentelemetry-exporter-gcp-trace/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
- Add optional resource attributes to trace spans with regex
66
([#145](https://github.com/GoogleCloudPlatform/opentelemetry-operations-python/pull/145))
77
- Upgrade `google-cloud-trace` dependency to version 1.1 or newer.
8+
([#170](https://github.com/GoogleCloudPlatform/opentelemetry-operations-python/pull/170))
9+
- Fix span attribute value truncation
10+
([#173](https://github.com/GoogleCloudPlatform/opentelemetry-operations-python/pull/173))
811

912
## Version 1.0.0
1013

opentelemetry-exporter-gcp-trace/src/opentelemetry/exporter/cloud_trace/__init__.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@
8989
MAX_EVENT_ATTRS = 4
9090
MAX_LINK_ATTRS = 32
9191
MAX_SPAN_ATTRS = 32
92+
MAX_ATTR_KEY_BYTES = 128
93+
MAX_ATTR_VAL_BYTES = 16 * 1024 # 16 kilobytes
9294

9395

9496
class CloudTraceSpanExporter(SpanExporter):
@@ -437,7 +439,7 @@ def _extract_attributes(
437439
) # type: BoundedDict[str, trace_types.AttributeValue]
438440
invalid_value_dropped_count = 0
439441
for ot_key, ot_value in attrs.items() if attrs else []:
440-
key = _truncate_str(ot_key, 128)[0]
442+
key = _truncate_str(ot_key, MAX_ATTR_KEY_BYTES)[0]
441443
if key in LABELS_MAPPING: # pylint: disable=consider-using-get
442444
key = LABELS_MAPPING[key]
443445
value = _format_attribute_value(ot_value)
@@ -485,14 +487,16 @@ def _format_attribute_value(
485487
value_type = "int_value"
486488
elif isinstance(value, str):
487489
value_type = "string_value"
488-
value = _get_truncatable_str_object(value, 256)
490+
value = _get_truncatable_str_object(value, MAX_ATTR_VAL_BYTES)
489491
elif isinstance(value, float):
490492
value_type = "string_value"
491-
value = _get_truncatable_str_object("{:0.4f}".format(value), 256)
493+
value = _get_truncatable_str_object(
494+
"{:0.4f}".format(value), MAX_ATTR_VAL_BYTES
495+
)
492496
elif isinstance(value, SequenceABC):
493497
value_type = "string_value"
494498
value = _get_truncatable_str_object(
495-
",".join(str(x) for x in value), 256
499+
",".join(str(x) for x in value), MAX_ATTR_VAL_BYTES
496500
)
497501
else:
498502
logger.warning(

opentelemetry-exporter-gcp-trace/tests/test_cloud_trace_exporter.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ def setUpClass(cls):
9696
cls.example_span_id = "95bb5edabd45950f"
9797
cls.example_time_in_ns = 1589919268850900051
9898
cls.example_time_stamp = _get_time_from_ns(cls.example_time_in_ns)
99+
cls.str_20kb = "a" * 20 * 1024
100+
cls.str_16kb = "a" * 16 * 1024
99101
cls.str_300 = "a" * 300
100102
cls.str_256 = "a" * 256
101103
cls.str_128 = "a" * 128
@@ -326,11 +328,24 @@ def test_agent_attribute_priority(self):
326328
)
327329

328330
def test_attribute_value_truncation(self):
331+
# shouldn't truncate
329332
self.assertEqual(
330333
_format_attribute_value(self.str_300),
331334
AttributeValue(
332335
string_value=TruncatableString(
333-
value=self.str_256, truncated_byte_count=300 - 256
336+
value=self.str_300,
337+
truncated_byte_count=0,
338+
)
339+
),
340+
)
341+
342+
# huge string should truncate
343+
self.assertEqual(
344+
_format_attribute_value(self.str_20kb),
345+
AttributeValue(
346+
string_value=TruncatableString(
347+
value=self.str_16kb,
348+
truncated_byte_count=(20 - 16) * 1024,
334349
)
335350
),
336351
)

0 commit comments

Comments
 (0)