Skip to content

Commit eeb528b

Browse files
fix(tracing): handle unicode when truncating long span attributes [backport #13475 to 2.21] (#14081)
PR #13270 introduced truncation of long span attributes. However, the truncation code works at the character level (e.g., uses `len(text)` to count the string length), but `msgpack_pack_unicode()` expects a size in bytes as an argument. For a string with non-ASCII characters, the character length can be less than the byte length, and so in some cases the string would not be truncated (because the number of characters would be below the limit), but `msgpack_pack_unicode()` would fail (because the number of bytes would be above the limit). This PR changes the call to `msgpack_pack_unicode()` to use the old limit of `ITEM_LIMIT` (2**32 - 1). - [x] PR author has checked that all the criteria below are met - The PR description includes an overview of the change - The PR description articulates the motivation for the change - The change includes tests OR the PR description describes a testing strategy - The PR description notes risks associated with the change, if any - Newly-added code is easy to change - The change follows the [library release note guidelines](https://ddtrace.readthedocs.io/en/stable/releasenotes.html) - The change includes or references documentation updates if necessary - Backport labels are set (if [applicable](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting)) - [x] Reviewer has checked that all the criteria below are met - Title is accurate - All changes are related to the pull request's stated goal - Avoids breaking [API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces) changes - Testing strategy adequately addresses listed risks - Newly-added code is easy to change - Release note makes sense to a user of the library - If necessary, author has acknowledged and discussed the performance implications of this PR as reported in the benchmarks PR comment - Backport labels are set in a manner that is consistent with the [release branch maintenance policy](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting) (cherry picked from commit c0fe465) --------- Co-authored-by: Vítor De Araújo <[email protected]>
1 parent a0b7f39 commit eeb528b

4 files changed

+58
-1
lines changed

ddtrace/internal/_encoding.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ cdef inline int pack_text(msgpack_packer *pk, object text) except? -1:
151151
if len(text) > MAX_SPAN_META_VALUE_LEN:
152152
text = truncate_string(text)
153153
IF PY_MAJOR_VERSION >= 3:
154-
ret = msgpack_pack_unicode(pk, text, MAX_SPAN_META_VALUE_LEN)
154+
ret = msgpack_pack_unicode(pk, text, ITEM_LIMIT)
155155
if ret == -2:
156156
raise ValueError("unicode string is too large")
157157
ELSE:
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
fixes:
3+
- |
4+
tracing: Fixes an issue where truncation of span attributes longer than 25000 characters would not consistently
5+
count the size of UTF-8 multibyte characters, leading to a ``unicode string is too large`` error.

tests/snapshots/tests.integration.test_integration_snapshots.test_encode_span_with_large_unicode_string_attributes[v0.4].json

Lines changed: 26 additions & 0 deletions
Large diffs are not rendered by default.

tests/snapshots/tests.integration.test_integration_snapshots.test_encode_span_with_large_unicode_string_attributes[v0.5].json

Lines changed: 26 additions & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)