Skip to content

Commit 3645218

Browse files
committed
Have full frames of the full stack trace
1 parent 5abd334 commit 3645218

File tree

1 file changed

+43
-21
lines changed

1 file changed

+43
-21
lines changed

sentry_sdk/utils.py

Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -819,10 +819,10 @@ def single_exception_from_error_tuple(
819819

820820
for stackframe in full_stack:
821821
stackframe_id = {
822-
"filename": stackframe.filename,
823-
"line": stackframe.line,
824-
"lineno": stackframe.lineno,
825-
"name": stackframe.name,
822+
"filename": stackframe["abs_path"],
823+
"line": stackframe["context_line"],
824+
"lineno": stackframe["lineno"],
825+
"name": stackframe["function"],
826826
}
827827

828828
found = False
@@ -839,19 +839,7 @@ def single_exception_from_error_tuple(
839839
break
840840

841841
if not found:
842-
frames.append(
843-
{
844-
"filename": os.path.basename(stackframe.filename),
845-
"abs_path": stackframe.filename,
846-
"function": stackframe.name,
847-
"module": None,
848-
"lineno": stackframe.lineno,
849-
"pre_context": [],
850-
"context_line": stackframe.line,
851-
"post_context": [],
852-
"vars": {},
853-
}
854-
)
842+
frames.append(stackframe)
855843

856844
frames.reverse()
857845
from pprint import pprint
@@ -1153,6 +1141,42 @@ def exc_info_from_error(error):
11531141
return exc_info
11541142

11551143

1144+
def get_full_stack():
1145+
# type: () -> List[Dict[str, Any]]
1146+
"""
1147+
Returns a serialized representation of the full stack from the first frame that is not in sentry_sdk.
1148+
"""
1149+
try:
1150+
# Raise an exception to capture the current stack
1151+
raise Exception
1152+
except:
1153+
# Get the current stack frame
1154+
_, _, tb = sys.exc_info()
1155+
1156+
# Get the frame one level up (skipping this function's frame)
1157+
frame = tb.tb_frame.f_back
1158+
1159+
stack_info = []
1160+
1161+
# Walk up the stack
1162+
while frame:
1163+
in_sdk = False
1164+
try:
1165+
if "sentry_sdk" in frame.f_code.co_filename:
1166+
in_sdk = True
1167+
except Exception:
1168+
pass
1169+
1170+
if not in_sdk:
1171+
stack_info.append(serialize_frame(frame))
1172+
1173+
frame = frame.f_back
1174+
1175+
stack_info.reverse()
1176+
1177+
return stack_info
1178+
1179+
11561180
def event_from_exception(
11571181
exc_info, # type: Union[BaseException, ExcInfo]
11581182
client_options=None, # type: Optional[Dict[str, Any]]
@@ -1161,12 +1185,10 @@ def event_from_exception(
11611185
# type: (...) -> Tuple[Event, Dict[str, Any]]
11621186
exc_info = exc_info_from_error(exc_info)
11631187
hint = event_hint_with_exc_info(exc_info)
1164-
# TODO: do not use extract_stack() but get the real stack frames (instead of summaries with extract_stack)
1165-
# to get all the information about the frames
1166-
full_stack = traceback.extract_stack()
1188+
full_stack = get_full_stack()
11671189

11681190
# TODO: add an option "add_full_stack" to the client options to add the full stack to the event (defaults to True)
1169-
1191+
11701192
# TODO: add an option "max_stack_frames" to the client options to limit the number of stack frames (defaults to 50?)
11711193

11721194
return (

0 commit comments

Comments
 (0)