Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(tracing): fix traceback max size for exceptions [backport 2.7] #9389

Merged
merged 2 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion ddtrace/_trace/span.py
Original file line number Diff line number Diff line change
Expand Up @@ -505,9 +505,13 @@ def set_exc_info(self, exc_type, exc_val, exc_tb):
self._set_exc_tags(exc_type, exc_val, exc_tb)

def _set_exc_tags(self, exc_type, exc_val, exc_tb):
limit = config._span_traceback_max_size
if limit is None:
limit = 30

# get the traceback
buff = StringIO()
traceback.print_exception(exc_type, exc_val, exc_tb, file=buff, limit=30)
traceback.print_exception(exc_type, exc_val, exc_tb, file=buff, limit=limit)
tb = buff.getvalue()

# readable version of type (e.g. exceptions.ZeroDivisionError)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
fixes:
- |
tracing: Fixes an issue where ``DD_TRACE_SPAN_TRACEBACK_MAX_SIZE`` was not applied to exception tracebacks.
26 changes: 26 additions & 0 deletions tests/tracer/test_span.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,32 @@ def test_custom_traceback_size(self):
stack = s.get_tag(ERROR_STACK)
assert len(stack.splitlines()) == tb_length_limit * 2, "stacktrace should contain two lines per entry"

def test_custom_traceback_size_with_error(self):
tb_length_limit = 2
with override_global_config(dict(_span_traceback_max_size=tb_length_limit)):
s = Span("test.span")

def divide_by_zero():
1 / 0

# Wrapper function to generate a larger traceback
def wrapper():
divide_by_zero()

try:
wrapper()
except ZeroDivisionError:
s.set_traceback()
else:
assert 0, "should have failed"

stack = s.get_tag(ERROR_STACK)
# one header "Traceback (most recent call last):" and one footer "ZeroDivisionError: division by zero"
header_and_footer_lines = 2
assert (
len(stack.splitlines()) == tb_length_limit * 2 + header_and_footer_lines
), "stacktrace should contain two lines per entry"

def test_ctx_mgr(self):
s = Span("bar")
assert not s.duration
Expand Down
Loading