From 71be1f54e1d33aa5846abac47e6b45678ac34fdc Mon Sep 17 00:00:00 2001 From: Rachel Yang Date: Tue, 11 Feb 2025 13:02:32 -0500 Subject: [PATCH 1/2] chore(tracer): add more detail in encoding error message --- ddtrace/internal/_encoding.pyx | 13 +++++++++++-- tests/tracer/test_encoders.py | 1 + 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/ddtrace/internal/_encoding.pyx b/ddtrace/internal/_encoding.pyx index f85fe7c6776..01f1eced7a8 100644 --- a/ddtrace/internal/_encoding.pyx +++ b/ddtrace/internal/_encoding.pyx @@ -8,7 +8,7 @@ import threading from json import dumps as json_dumps from ._utils cimport PyBytesLike_Check - +import traceback # Do not use an absolute import here Cython<3.0.0 will # import `ddtrace.internal.constants` instead when this @@ -500,7 +500,16 @@ cdef class MsgpackEncoderBase(BufferedEncoder): try: ret = self.pack_span(span, dd_origin) except Exception as e: - raise RuntimeError("failed to pack span: {!r}. Exception: {}".format(span, e)) + # Capture full traceback information. + tb_info = traceback.format_exc() + # Raise an error that includes the span details, exception, and traceback. + # Rachel - change what information is included + raise RuntimeError( + "Failed to pack span: {!r}.\n" + "Exception: {}.\n" + "Traceback:\n{}" + .format(span, e, tb_info) + ) from e # No exception was raised, but we got an error code from msgpack if ret != 0: diff --git a/tests/tracer/test_encoders.py b/tests/tracer/test_encoders.py index f96e063502e..e8a7566a320 100644 --- a/tests/tracer/test_encoders.py +++ b/tests/tracer/test_encoders.py @@ -799,6 +799,7 @@ def test_encoding_invalid_data(data): with pytest.raises(RuntimeError) as e: encoder.put(trace) + # Rachel - these tests will fail assert e.match(r"failed to pack span: Date: Tue, 11 Feb 2025 14:56:54 -0500 Subject: [PATCH 2/2] adding span metric --- ddtrace/internal/_encoding.pyx | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/ddtrace/internal/_encoding.pyx b/ddtrace/internal/_encoding.pyx index 01f1eced7a8..600e5a7d751 100644 --- a/ddtrace/internal/_encoding.pyx +++ b/ddtrace/internal/_encoding.pyx @@ -502,13 +502,14 @@ cdef class MsgpackEncoderBase(BufferedEncoder): except Exception as e: # Capture full traceback information. tb_info = traceback.format_exc() - # Raise an error that includes the span details, exception, and traceback. - # Rachel - change what information is included + # Safely get the _metrics attribute from span, if available. + metrics = getattr(span, "_metrics", None) raise RuntimeError( "Failed to pack span: {!r}.\n" + "Span metrics: {!r}.\n" "Exception: {}.\n" "Traceback:\n{}" - .format(span, e, tb_info) + .format(span, metrics, e, tb_info) ) from e # No exception was raised, but we got an error code from msgpack