Skip to content

Commit a0b7f39

Browse files
emmettbutlerrachelyangdogquinna-hbrettlangdon
authored
fix(tracer): error when encoding bytes and adding a string [backport #13419 to 2.21] (#14084)
fixes error when we try to concat the string 'truncated' to bytes - [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 2e13f18) --------- Co-authored-by: Rachel Yang <[email protected]> Co-authored-by: Quinna Halim <[email protected]> Co-authored-by: Brett Langdon <[email protected]>
1 parent 5a51e2d commit a0b7f39

5 files changed

+84
-4
lines changed

ddtrace/internal/_encoding.pyx

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

9797
cdef inline object truncate_string(object string):
9898
if string and len(string) > MAX_SPAN_META_VALUE_LEN:
99-
return string[:TRUNCATED_SPAN_ATTRIBUTE_LEN - 14] + "<truncated>..."
99+
if PyBytesLike_Check(string):
100+
return string[:TRUNCATED_SPAN_ATTRIBUTE_LEN - 14] + b"<truncated>..."
101+
elif PyUnicode_Check(string):
102+
return string[:TRUNCATED_SPAN_ATTRIBUTE_LEN - 14] + "<truncated>..."
100103
return string
101104

102105
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: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -278,9 +278,7 @@ def test_snapshot_skip():
278278
@parametrize_with_all_encodings
279279
@mark_snapshot
280280
def test_setting_span_tags_and_metrics_generates_no_error_logs():
281-
import ddtrace
282-
283-
s = ddtrace.tracer.trace("operation", service="my-svc")
281+
s = tracer.trace("operation", service="my-svc")
284282
s.set_tag("env", "my-env")
285283
s.set_metric("number1", 123)
286284
s.set_metric("number2", 12.0)
@@ -296,3 +294,28 @@ def test_encode_span_with_large_string_attributes(encoding):
296294
with override_global_config(dict(_trace_api=encoding)):
297295
with tracer.trace(name="a" * 25000, resource="b" * 25001) as span:
298296
span.set_tag(key="c" * 25001, value="d" * 2000)
297+
298+
299+
@pytest.mark.parametrize("encoding", ["v0.4", "v0.5"])
300+
@pytest.mark.snapshot()
301+
def test_encode_span_with_large_bytes_attributes(encoding):
302+
from ddtrace import tracer
303+
304+
with override_global_config(dict(_trace_api=encoding)):
305+
name = b"a" * 25000
306+
resource = b"b" * 25001
307+
key = b"c" * 25001
308+
value = b"d" * 2000
309+
310+
with tracer.trace(name=name, resource=resource) as span:
311+
span.set_tag(key=key, value=value)
312+
313+
314+
@pytest.mark.parametrize("encoding", ["v0.4", "v0.5"])
315+
@pytest.mark.snapshot()
316+
def test_encode_span_with_large_unicode_string_attributes(encoding):
317+
from ddtrace import tracer
318+
319+
with override_global_config(dict(_trace_api=encoding)):
320+
with tracer.trace(name="á" * 25000, resource="â" * 25001) as span:
321+
span.set_tag(key="å" * 25001, value="ä" * 2000)

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)