Fix attriubte value truncation #5997
Open
+216
−50
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fix #5996
Correctness
From the OTel specification:
Our current implementation truncates on number of bytes not characters.
Unit tests are added/updated to validate this fix and prevent regressions.
Performance
Values shorter than limit
This is the default code path. Most attribute values will be shorter than the default 128 limit that users will not modify.
The current code,
safeTruncate
requires a full iteration of the value to determine it is valid and under the limit.The replacement,
truncate
, first checks if the number of bytes in the value are less than or equal to the limit (which guarantees the number of characters are less than or equal to the limit) and returns immediately. This will mean that invalid encoding less than the limit is not changed, which meets the specification requirements.Values longer than the limit
For values who's number of bytes exceeds the limit, they are iterated only once with the replacement,
truncate
.In comparison, the current code,
safeTruncate
, can iterate the string up to three separate times when the string contains invalid characters.