Skip to content

Commit 593d5d5

Browse files
github-actions[bot]rachelyangdogquinna-hbrettlangdon
authored
fix(tracer): error when encoding bytes and adding a string [backport 3.9] (#13718)
Backport 2e13f18 from #13419 to 3.9. fixes error when we try to concat the string 'truncated' to bytes ## 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: Rachel Yang <[email protected]> Co-authored-by: Quinna Halim <[email protected]> Co-authored-by: Brett Langdon <[email protected]>
1 parent 03c93b7 commit 593d5d5

5 files changed

+73
-1
lines changed

ddtrace/internal/_encoding.pyx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,10 @@ cdef inline int array_prefix_size(stdint.uint32_t l):
9898

9999
cdef inline object truncate_string(object string):
100100
if string and len(string) > MAX_SPAN_META_VALUE_LEN:
101-
return string[:TRUNCATED_SPAN_ATTRIBUTE_LEN - 14] + "<truncated>..."
101+
if PyBytesLike_Check(string):
102+
return string[:TRUNCATED_SPAN_ATTRIBUTE_LEN - 14] + b"<truncated>..."
103+
elif PyUnicode_Check(string):
104+
return string[:TRUNCATED_SPAN_ATTRIBUTE_LEN - 14] + "<truncated>..."
102105
return string
103106

104107
cdef inline int pack_bytes(msgpack_packer *pk, char *bs, Py_ssize_t 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: This resolves a ``TypeError`` in encoding when truncating a large bytes object.

tests/integration/test_integration_snapshots.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,21 @@ def test_encode_span_with_large_string_attributes(encoding):
291291
span.set_tag(key="c" * 25001, value="d" * 2000)
292292

293293

294+
@pytest.mark.parametrize("encoding", ["v0.4", "v0.5"])
295+
@pytest.mark.snapshot()
296+
def test_encode_span_with_large_bytes_attributes(encoding):
297+
from ddtrace import tracer
298+
299+
with override_global_config(dict(_trace_api=encoding)):
300+
name = b"a" * 25000
301+
resource = b"b" * 25001
302+
key = b"c" * 25001
303+
value = b"d" * 2000
304+
305+
with tracer.trace(name=name, resource=resource) as span:
306+
span.set_tag(key=key, value=value)
307+
308+
294309
@pytest.mark.parametrize("encoding", ["v0.4", "v0.5"])
295310
@pytest.mark.snapshot()
296311
def test_encode_span_with_large_unicode_string_attributes(encoding):

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

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

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

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

0 commit comments

Comments
 (0)