Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions sentry_sdk/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ def __init__(
project_root=None, # type: Optional[str]
enable_tracing=None, # type: Optional[bool]
include_local_variables=True, # type: Optional[bool]
include_source_context=True, # type: Optional[bool]
trace_propagation_targets=[ # noqa: B006
MATCH_ALL
], # type: Optional[Sequence[str]]
Expand Down
13 changes: 10 additions & 3 deletions sentry_sdk/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -632,16 +632,20 @@ def serialize_frame(
return rv


def current_stacktrace(include_local_variables=True):
# type: (bool) -> Any
def current_stacktrace(include_local_variables=True, include_source_context=True):
# type: (bool, bool) -> Any
__tracebackhide__ = True
frames = []

f = sys._getframe() # type: Optional[FrameType]
while f is not None:
if not should_hide_frame(f):
frames.append(
serialize_frame(f, include_local_variables=include_local_variables)
serialize_frame(
f,
include_local_variables=include_local_variables,
include_source_context=include_source_context,
)
)
f = f.f_back

Expand Down Expand Up @@ -677,14 +681,17 @@ def single_exception_from_error_tuple(

if client_options is None:
include_local_variables = True
include_source_context = True
else:
include_local_variables = client_options["include_local_variables"]
include_source_context = client_options["include_source_context"]

frames = [
serialize_frame(
tb.tb_frame,
tb_lineno=tb.tb_lineno,
include_local_variables=include_local_variables,
include_source_context=include_source_context,
)
for tb in iter_stacks(tb)
]
Expand Down
32 changes: 32 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,38 @@ def test_include_local_variables_disabled(sentry_init, capture_events):
)


def test_include_source_context_enabled(sentry_init, capture_events):
sentry_init(include_source_context=True)
events = capture_events()
try:
1 / 0
except Exception:
capture_exception()

(event,) = events

frame = event["exception"]["values"][0]["stacktrace"]["frames"][0]
assert "post_context" in frame
assert "pre_context" in frame
assert "context_line" in frame


def test_include_source_context_disabled(sentry_init, capture_events):
sentry_init(include_source_context=False)
events = capture_events()
try:
1 / 0
except Exception:
capture_exception()

(event,) = events

frame = event["exception"]["values"][0]["stacktrace"]["frames"][0]
assert "post_context" not in frame
assert "pre_context" not in frame
assert "context_line" not in frame


@pytest.mark.parametrize("integrations", [[], [ExecutingIntegration()]])
def test_function_names(sentry_init, capture_events, integrations):
sentry_init(integrations=integrations)
Expand Down