Skip to content

Commit 03f0f2b

Browse files
dd-octo-sts[bot]quinna-hbrettlangdon
authored
fix: remove throwing PyErr on large bytes object in span attribute [backport 3.13] (#14631)
Backport f6d8df9 from #14589 to 3.13. Removes throwing `PyErr_Format` when encoding bytes object, which may have led to potential error in customer logs in application with long byte string postgres query as resource name. ## Checklist - [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)) ## Reviewer Checklist - [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) Co-authored-by: Quinna Halim <[email protected]> Co-authored-by: Brett Langdon <[email protected]>
1 parent 78fc4f3 commit 03f0f2b

File tree

3 files changed

+67
-1
lines changed

3 files changed

+67
-1
lines changed

ddtrace/internal/_encoding.pyx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,6 @@ cdef inline int pack_text(msgpack_packer *pk, object text) except? -1:
149149
if PyBytesLike_Check(text):
150150
L = len(text)
151151
if L > MAX_SPAN_META_VALUE_LEN:
152-
PyErr_Format(ValueError, b"%.200s object is too large", Py_TYPE(text).tp_name)
153152
text = truncate_string(text)
154153
L = len(text)
155154
ret = msgpack_pack_raw(pk, L)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
fixes:
3+
- |
4+
tracing: Fixes encoding bytes objects as span attributes by truncating byte string, rather than throwing PyErr_Format.

tests/tracer/test_encoders.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,69 @@ def test_long_span_start(encoding):
404404
assert decode(encoder.encode()[0][0]) is not None
405405

406406

407+
@allencodings
408+
def test_span_encoding_large_resource(encoding):
409+
encoder = MSGPACK_ENCODERS[encoding](1 << 20, 1 << 20)
410+
postgres_query = (
411+
"INSERT INTO table VALUES "
412+
'(\'x\' * 25001, \'{"top_left": {"x": 0.29411766, "y": 0.123786405}, '
413+
'"top_right": {"x": 0.38886696, "y": 0.123786405}, '
414+
'"bottom_right": {"x": 0.38886696, "y": 0.13288835}, '
415+
'"bottom_left": {"x": 0.29411766, "y": 0.13288835}}\'), '
416+
'(\'y\' * 25001, \'{"top_left": {"x": 0.12345678, "y": 0.23456789}, '
417+
'"top_right": {"x": 0.34567890, "y": 0.23456789}, '
418+
'"bottom_right": {"x": 0.34567890, "y": 0.45678901}, '
419+
'"bottom_left": {"x": 0.12345678, "y": 0.45678901}}\'), '
420+
'(\'z\' * 25001, \'{"top_left": {"x": 0.56789012, "y": 0.67890123}, '
421+
'"top_right": {"x": 0.78901234, "y": 0.67890123}, '
422+
'"bottom_right": {"x": 0.78901234, "y": 0.89012345}, '
423+
'"bottom_left": {"x": 0.56789012, "y": 0.89012345}}\'), '
424+
'(\'a\' * 25001, \'{"top_left": {"x": 0.11111111, "y": 0.22222222}, '
425+
'"top_right": {"x": 0.33333333, "y": 0.22222222}, '
426+
'"bottom_right": {"x": 0.33333333, "y": 0.44444444}, '
427+
'"bottom_left": {"x": 0.11111111, "y": 0.44444444}}\'), '
428+
'(\'b\' * 25001, \'{"top_left": {"x": 0.55555555, "y": 0.66666666}, '
429+
'"top_right": {"x": 0.77777777, "y": 0.66666666}, '
430+
'"bottom_right": {"x": 0.77777777, "y": 0.88888888}, '
431+
'"bottom_left": {"x": 0.55555555, "y": 0.88888888}}\'), '
432+
'(\'c\' * 25001, \'{"top_left": {"x": 0.99999999, "y": 0.10101010}, '
433+
'"top_right": {"x": 0.20202020, "y": 0.10101010}, '
434+
'"bottom_right": {"x": 0.20202020, "y": 0.30303030}, '
435+
'"bottom_left": {"x": 0.99999999, "y": 0.30303030}}\'), '
436+
'(\'d\' * 25001, \'{"top_left": {"x": 0.40404040, "y": 0.50505050}, '
437+
'"top_right": {"x": 0.60606060, "y": 0.50505050}, '
438+
'"bottom_right": {"x": 0.60606060, "y": 0.70707070}, '
439+
'"bottom_left": {"x": 0.40404040, "y": 0.70707070}}\'), '
440+
'(\'e\' * 25001, \'{"top_left": {"x": 0.80808080, "y": 0.90909090}, '
441+
'"top_right": {"x": 0.12121212, "y": 0.90909090}, '
442+
'"bottom_right": {"x": 0.12121212, "y": 0.13131313}, '
443+
'"bottom_left": {"x": 0.80808080, "y": 0.13131313}}\'), '
444+
'(\'f\' * 25001, \'{"top_left": {"x": 0.14141414, "y": 0.15151515}, '
445+
'"top_right": {"x": 0.16161616, "y": 0.15151515}, '
446+
'"bottom_right": {"x": 0.16161616, "y": 0.17171717}, '
447+
'"bottom_left": {"x": 0.14141414, "y": 0.17171717}}\'), '
448+
'(\'g\' * 25001, \'{"top_left": {"x": 0.18181818, "y": 0.19191919}, '
449+
'"top_right": {"x": 0.20202020, "y": 0.19191919}, '
450+
'"bottom_right": {"x": 0.20202020, "y": 0.21212121}, '
451+
'"bottom_left": {"x": 0.18181818, "y": 0.21212121}}\'), '
452+
'(\'h\' * 25001, \'{"top_left": {"x": 0.22222222, "y": 0.23232323}, '
453+
'"top_right": {"x": 0.24242424, "y": 0.23232323}, '
454+
'"bottom_right": {"x": 0.24242424, "y": 0.25252525}, '
455+
'"bottom_left": {"x": 0.22222222, "y": 0.25252525}}\'), '
456+
'(\'i\' * 25001, \'{"top_left": {"x": 0.26262626, "y": 0.27272727}, '
457+
'"top_right": {"x": 0.28282828, "y": 0.27272727}, '
458+
'"bottom_right": {"x": 0.28282828, "y": 0.29292929}, '
459+
'"bottom_left": {"x": 0.26262626, "y": 0.29292929}}\'), '
460+
'(\'j\' * 25001, \'{"top_left": {"x": 0.30303030, "y": 0.31313131}, '
461+
'"top_right": {"x": 0.32323232, "y": 0.31313131}, '
462+
'"bottom_right": {"x": 0.32323232, "y": 0.33333333}, '
463+
'"bottom_left": {"x": 0.30303030, "y": 0.33333333}}\')' * 10
464+
)
465+
span = Span(name="test-span", resource=postgres_query.encode("utf-8"))
466+
trace = [span]
467+
encoder.put(trace)
468+
469+
407470
class SubString(str):
408471
pass
409472

0 commit comments

Comments
 (0)