Skip to content

Commit f805239

Browse files
authored
Surface include_source_context as an option (#2100)
1 parent e8f4792 commit f805239

File tree

3 files changed

+43
-3
lines changed

3 files changed

+43
-3
lines changed

Diff for: sentry_sdk/consts.py

+1
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ def __init__(
185185
project_root=None, # type: Optional[str]
186186
enable_tracing=None, # type: Optional[bool]
187187
include_local_variables=True, # type: Optional[bool]
188+
include_source_context=True, # type: Optional[bool]
188189
trace_propagation_targets=[ # noqa: B006
189190
MATCH_ALL
190191
], # type: Optional[Sequence[str]]

Diff for: sentry_sdk/utils.py

+10-3
Original file line numberDiff line numberDiff line change
@@ -632,16 +632,20 @@ def serialize_frame(
632632
return rv
633633

634634

635-
def current_stacktrace(include_local_variables=True):
636-
# type: (bool) -> Any
635+
def current_stacktrace(include_local_variables=True, include_source_context=True):
636+
# type: (bool, bool) -> Any
637637
__tracebackhide__ = True
638638
frames = []
639639

640640
f = sys._getframe() # type: Optional[FrameType]
641641
while f is not None:
642642
if not should_hide_frame(f):
643643
frames.append(
644-
serialize_frame(f, include_local_variables=include_local_variables)
644+
serialize_frame(
645+
f,
646+
include_local_variables=include_local_variables,
647+
include_source_context=include_source_context,
648+
)
645649
)
646650
f = f.f_back
647651

@@ -677,14 +681,17 @@ def single_exception_from_error_tuple(
677681

678682
if client_options is None:
679683
include_local_variables = True
684+
include_source_context = True
680685
else:
681686
include_local_variables = client_options["include_local_variables"]
687+
include_source_context = client_options["include_source_context"]
682688

683689
frames = [
684690
serialize_frame(
685691
tb.tb_frame,
686692
tb_lineno=tb.tb_lineno,
687693
include_local_variables=include_local_variables,
694+
include_source_context=include_source_context,
688695
)
689696
for tb in iter_stacks(tb)
690697
]

Diff for: tests/test_client.py

+32
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,38 @@ def test_include_local_variables_disabled(sentry_init, capture_events):
365365
)
366366

367367

368+
def test_include_source_context_enabled(sentry_init, capture_events):
369+
sentry_init(include_source_context=True)
370+
events = capture_events()
371+
try:
372+
1 / 0
373+
except Exception:
374+
capture_exception()
375+
376+
(event,) = events
377+
378+
frame = event["exception"]["values"][0]["stacktrace"]["frames"][0]
379+
assert "post_context" in frame
380+
assert "pre_context" in frame
381+
assert "context_line" in frame
382+
383+
384+
def test_include_source_context_disabled(sentry_init, capture_events):
385+
sentry_init(include_source_context=False)
386+
events = capture_events()
387+
try:
388+
1 / 0
389+
except Exception:
390+
capture_exception()
391+
392+
(event,) = events
393+
394+
frame = event["exception"]["values"][0]["stacktrace"]["frames"][0]
395+
assert "post_context" not in frame
396+
assert "pre_context" not in frame
397+
assert "context_line" not in frame
398+
399+
368400
@pytest.mark.parametrize("integrations", [[], [ExecutingIntegration()]])
369401
def test_function_names(sentry_init, capture_events, integrations):
370402
sentry_init(integrations=integrations)

0 commit comments

Comments
 (0)