Skip to content

Commit

Permalink
fix(tracing): fix traceback max size for exceptions [backport 2.9] (#…
Browse files Browse the repository at this point in the history
…9391)

Backport 1b954de from #9112 to 2.9.

#7558 made the traceback size
configurable via `DD_TRACE_SPAN_TRACEBACK_MAX_SIZE`, but tracebacks for
exceptions were still hardcoded with a limit of 30.

The fix is modeled after `Span#set_traceback`'s existing logic, and
tests were modeled after both `test_traceback_with_error` and
`test_custom_traceback_size`. Not much new logic, just an edge case.

## Checklist

- [x] Change(s) are motivated and described in the PR description
- [x] Testing strategy is described if automated tests are not included
in the PR
- [x] Risks are described (performance impact, potential for breakage,
maintainability)
- [x] Change is maintainable (easy to change, telemetry, documentation)
- [x] [Library release note
guidelines](https://ddtrace.readthedocs.io/en/stable/releasenotes.html)
are followed or label `changelog/no-changelog` is set
- [x] Documentation is included (in-code, generated user docs, [public
corp docs](https://github.com/DataDog/documentation/))
- [x] Backport labels are set (if
[applicable](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting))
- [x] If this PR changes the public interface, I've notified
`@DataDog/apm-tees`.

## Reviewer Checklist

- [x] Title is accurate
- [x] All changes are related to the pull request's stated goal
- [x] Description motivates each change
- [x] Avoids breaking
[API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces)
changes
- [x] Testing strategy adequately addresses listed risks
- [x] Change is maintainable (easy to change, telemetry, documentation)
- [x] Release note makes sense to a user of the library
- [x] Author has acknowledged and discussed the performance implications
of this PR as reported in the benchmarks PR comment
- [x] 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)

Co-authored-by: Matt Alexander <1710128+quiqueg@users.noreply.github.com>
  • Loading branch information
github-actions[bot] and quiqueg committed May 28, 2024
1 parent 3fce1b0 commit 4382c8d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
6 changes: 5 additions & 1 deletion ddtrace/_trace/span.py
Original file line number Diff line number Diff line change
Expand Up @@ -552,9 +552,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

0 comments on commit 4382c8d

Please sign in to comment.