Skip to content

Commit 1784e67

Browse files
fix(tracing): fix msgpack import error (#7271)
## Description - This change fixes an import error raised when `DD_TRACE_API` is set to `v0.5`. This occurs because msgpack is not installed by ddtrace. - This feature was reverted on 2.x due to performance reasons (#7263). It only exists on 1.20 ## Motivation [Import msgpack](https://github.com/DataDog/dd-trace-py/blob/v2.1.1/ddtrace/internal/_encoding.pyx#L425) is problematic and breaks v0.5 encoding. To fix this issue we can ensure the ddtrace library installs msgpack (or we can vendor it) but this comes with it's own drawbacks. Instead of fixing the importerror this PR only imports msgpack if `_DD_TRACE_WRITER_LOG_ERROR_PAYLOADS=True`. - [x] checkbox --------- Co-authored-by: Munir Abdinur <[email protected]> Co-authored-by: Munir Abdinur <[email protected]>
1 parent 676b522 commit 1784e67

File tree

4 files changed

+19
-11
lines changed

4 files changed

+19
-11
lines changed

ddtrace/internal/_encoding.pyi

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ class BufferItemTooLarge(Exception):
1717
pass
1818

1919
class EncodingValidationError(Exception):
20-
pass
20+
def __init__(self, msg="", debug_message=""):
21+
Exception.__init__(self, msg)
22+
self.msg = msg
23+
self.debug_message = debug_message
2124

2225
class BufferedEncoder(object):
2326
max_size: int

ddtrace/internal/_encoding.pyx

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,10 @@ class BufferItemTooLarge(Exception):
6868

6969

7070
class EncodingValidationError(Exception):
71-
pass
71+
def __init__(self, msg="", debug_message=""):
72+
Exception.__init__(self, msg)
73+
self.msg = msg
74+
self.debug_message = debug_message
7275

7376

7477
cdef inline const char * string_to_buff(str s):
@@ -853,13 +856,16 @@ cdef class MsgpackEncoderV05(MsgpackEncoderBase):
853856
return 0
854857

855858
cpdef _verify_encoding(self, encoded_bytes):
856-
import msgpack
857-
858859
from ddtrace import config
859860

860861
if not config._trace_writer_log_err_payload:
861862
return
862863

864+
# msgpack is not installed by ddtrace. To decode ddtrace payloads ensure
865+
# msgpack is installed
866+
# TODO: vendor msgpack.unpackb()
867+
import msgpack
868+
863869
unpacked = msgpack.unpackb(encoded_bytes, raw=True, strict_map_key=False)
864870
if not unpacked or not unpacked[0]:
865871
return unpacked
@@ -916,11 +922,7 @@ cdef class MsgpackEncoderV05(MsgpackEncoderBase):
916922
)
917923
except Exception as e:
918924
eve = EncodingValidationError(
919-
str(e) + "\nDecoded Span does not match encoded span: {}".format(og_span._pprint())
920-
)
921-
setattr(
922-
eve,
923-
"_debug_message",
925+
str(e) + "\nDecoded Span does not match encoded span: {}".format(og_span._pprint()),
924926
"Malformed String table values\ntable:{}\ntraces:{}\nencoded_bytes:{}\nspans:{}".format(
925927
table, packed_traces, encoded_bytes, self._encoded_spans
926928
),

ddtrace/internal/writer/writer.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -401,8 +401,7 @@ def _flush_queue_with_client(self, client, raise_exc=False):
401401
return
402402
except EncodingValidationError as e:
403403
log.error("Encoding Error (or span was modified after finish): %s", str(e))
404-
if hasattr(e, "_debug_message"):
405-
log.debug(e._debug_message)
404+
log.debug(e.debug_message)
406405
return
407406
except Exception:
408407
log.error("failed to encode trace with encoder %r", client.encoder, exc_info=True)
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 a msgpack import error when ``DD_TRACE_API`` is set to ``v0.5``

0 commit comments

Comments
 (0)