diff --git a/ddtrace/internal/_encoding.pyx b/ddtrace/internal/_encoding.pyx index f85fe7c6776..600e5a7d751 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,17 @@ 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() + # 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, metrics, 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: