Skip to content

Commit 4dbf661

Browse files
committed
Improvements
1 parent 5e4c6a7 commit 4dbf661

File tree

2 files changed

+15
-18
lines changed

2 files changed

+15
-18
lines changed

sentry_sdk/consts.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,8 @@ def __init__(
550550
cert_file=None, # type: Optional[str]
551551
key_file=None, # type: Optional[str]
552552
custom_repr=None, # type: Optional[Callable[..., Optional[str]]]
553+
add_full_stack=False, # type: bool
554+
max_stack_frames=50, # type: Optional[int]
553555
):
554556
# type: (...) -> None
555557
pass

sentry_sdk/utils.py

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -806,7 +806,7 @@ def single_exception_from_error_tuple(
806806
custom_repr=custom_repr,
807807
)
808808
for tb in iter_stacks(tb)
809-
]
809+
] # type: List[Dict[str, Any]]
810810

811811
if frames:
812812
if not full_stack:
@@ -836,6 +836,13 @@ def single_exception_from_error_tuple(
836836
]
837837
new_frames.extend(frames)
838838

839+
# Limit the number of frames
840+
max_stack_frames = (
841+
client_options.get("max_stack_frames") if client_options else None
842+
)
843+
if max_stack_frames is not None:
844+
new_frames = new_frames[:max_stack_frames]
845+
839846
exception_value["stacktrace"] = {"frames": new_frames}
840847

841848
return exception_value
@@ -1142,22 +1149,10 @@ def get_full_stack():
11421149
"""
11431150
Returns a serialized representation of the full stack from the first frame that is not in sentry_sdk.
11441151
"""
1145-
try:
1146-
# Raise an exception to capture the current stack
1147-
raise Exception
1148-
except:
1149-
# Get the current stack frame
1150-
_, _, tb = sys.exc_info()
1151-
1152-
# Get the frame one level up (skipping this function's frame)
1153-
if tb is None:
1154-
return []
1155-
1156-
frame = tb.tb_frame.f_back
1157-
11581152
stack_info = []
11591153

11601154
# Walk up the stack
1155+
frame = sys._getframe(1) # type: Optional[FrameType]
11611156
while frame:
11621157
in_sdk = False
11631158
try:
@@ -1184,11 +1179,11 @@ def event_from_exception(
11841179
# type: (...) -> Tuple[Event, Dict[str, Any]]
11851180
exc_info = exc_info_from_error(exc_info)
11861181
hint = event_hint_with_exc_info(exc_info)
1187-
full_stack = get_full_stack()
11881182

1189-
# TODO: add an option "add_full_stack" to the client options to add the full stack to the event (defaults to True)
1190-
1191-
# TODO: add an option "max_stack_frames" to the client options to limit the number of stack frames (defaults to 50?)
1183+
if client_options and client_options["add_full_stack"]:
1184+
full_stack = get_full_stack()
1185+
else:
1186+
full_stack = None
11921187

11931188
return (
11941189
{

0 commit comments

Comments
 (0)