-
Notifications
You must be signed in to change notification settings - Fork 510
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add missing stack frames #3673
Open
antonpirker
wants to merge
14
commits into
master
Choose a base branch
from
antonpirker/missing-stack-trames
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add missing stack frames #3673
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
7198a75
extract full stack frame and give it to where it is needed
antonpirker 8ae7d34
Adding missing frames
antonpirker e69ca9a
Added some todos
antonpirker 5abd334
Merge branch 'master' into antonpirker/missing-stack-trames
antonpirker 3645218
Have full frames of the full stack trace
antonpirker 31d059c
Added full frames to full_stack
antonpirker 8d2490b
Merge branch 'master' into antonpirker/missing-stack-trames
antonpirker 7189be1
Merge branch 'master' into antonpirker/missing-stack-trames
antonpirker 5e4c6a7
Merge branch 'master' into antonpirker/missing-stack-trames
antonpirker 4dbf661
Improvements
antonpirker 04f79ed
linting
antonpirker e4f3397
Make it more resilient againt misuse
antonpirker 39db548
Cleanup
antonpirker 10d7a49
Added tests
antonpirker 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
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,7 +26,12 @@ | |
|
||
import sentry_sdk | ||
from sentry_sdk._compat import PY37 | ||
from sentry_sdk.consts import DEFAULT_MAX_VALUE_LENGTH, EndpointType | ||
from sentry_sdk.consts import ( | ||
DEFAULT_ADD_FULL_STACK, | ||
DEFAULT_MAX_STACK_FRAMES, | ||
DEFAULT_MAX_VALUE_LENGTH, | ||
EndpointType, | ||
) | ||
|
||
from typing import TYPE_CHECKING | ||
|
||
|
@@ -737,6 +742,7 @@ def single_exception_from_error_tuple( | |
exception_id=None, # type: Optional[int] | ||
parent_id=None, # type: Optional[int] | ||
source=None, # type: Optional[str] | ||
full_stack=None, # type: Optional[list[dict[str, Any]]] | ||
): | ||
# type: (...) -> Dict[str, Any] | ||
""" | ||
|
@@ -804,10 +810,15 @@ def single_exception_from_error_tuple( | |
custom_repr=custom_repr, | ||
) | ||
for tb in iter_stacks(tb) | ||
] | ||
] # type: List[Dict[str, Any]] | ||
|
||
if frames: | ||
exception_value["stacktrace"] = {"frames": frames} | ||
if not full_stack: | ||
new_frames = frames | ||
else: | ||
new_frames = merge_stack_frames(frames, full_stack, client_options) | ||
|
||
exception_value["stacktrace"] = {"frames": new_frames} | ||
|
||
return exception_value | ||
|
||
|
@@ -862,6 +873,7 @@ def exceptions_from_error( | |
exception_id=0, # type: int | ||
parent_id=0, # type: int | ||
source=None, # type: Optional[str] | ||
full_stack=None, # type: Optional[list[dict[str, Any]]] | ||
): | ||
# type: (...) -> Tuple[int, List[Dict[str, Any]]] | ||
""" | ||
|
@@ -881,6 +893,7 @@ def exceptions_from_error( | |
exception_id=exception_id, | ||
parent_id=parent_id, | ||
source=source, | ||
full_stack=full_stack, | ||
) | ||
exceptions = [parent] | ||
|
||
|
@@ -906,6 +919,7 @@ def exceptions_from_error( | |
mechanism=mechanism, | ||
exception_id=exception_id, | ||
source="__cause__", | ||
full_stack=full_stack, | ||
) | ||
exceptions.extend(child_exceptions) | ||
|
||
|
@@ -927,6 +941,7 @@ def exceptions_from_error( | |
mechanism=mechanism, | ||
exception_id=exception_id, | ||
source="__context__", | ||
full_stack=full_stack, | ||
) | ||
exceptions.extend(child_exceptions) | ||
|
||
|
@@ -943,6 +958,7 @@ def exceptions_from_error( | |
exception_id=exception_id, | ||
parent_id=parent_id, | ||
source="exceptions[%s]" % idx, | ||
full_stack=full_stack, | ||
) | ||
exceptions.extend(child_exceptions) | ||
|
||
|
@@ -953,6 +969,7 @@ def exceptions_from_error_tuple( | |
exc_info, # type: ExcInfo | ||
client_options=None, # type: Optional[Dict[str, Any]] | ||
mechanism=None, # type: Optional[Dict[str, Any]] | ||
full_stack=None, # type: Optional[list[dict[str, Any]]] | ||
): | ||
# type: (...) -> List[Dict[str, Any]] | ||
exc_type, exc_value, tb = exc_info | ||
|
@@ -970,14 +987,20 @@ def exceptions_from_error_tuple( | |
mechanism=mechanism, | ||
exception_id=0, | ||
parent_id=0, | ||
full_stack=full_stack, | ||
) | ||
|
||
else: | ||
exceptions = [] | ||
for exc_type, exc_value, tb in walk_exception_chain(exc_info): | ||
exceptions.append( | ||
single_exception_from_error_tuple( | ||
exc_type, exc_value, tb, client_options, mechanism | ||
exc_type=exc_type, | ||
exc_value=exc_value, | ||
tb=tb, | ||
client_options=client_options, | ||
mechanism=mechanism, | ||
full_stack=full_stack, | ||
) | ||
) | ||
|
||
|
@@ -1096,6 +1119,73 @@ def exc_info_from_error(error): | |
return exc_info | ||
|
||
|
||
def get_full_stack(): | ||
# type: () -> List[Dict[str, Any]] | ||
""" | ||
Returns a serialized representation of the full stack from the first frame that is not in sentry_sdk. | ||
""" | ||
stack_info = [] | ||
|
||
# Walk up the stack | ||
frame = sys._getframe(1) # type: Optional[FrameType] | ||
while frame: | ||
in_sdk = False | ||
try: | ||
if "sentry_sdk" in frame.f_code.co_filename: | ||
in_sdk = True | ||
Comment on lines
+1134
to
+1135
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. might be better to use |
||
except Exception: | ||
pass | ||
|
||
if not in_sdk: | ||
stack_info.append(serialize_frame(frame)) | ||
|
||
frame = frame.f_back | ||
|
||
stack_info.reverse() | ||
|
||
return stack_info | ||
|
||
|
||
def merge_stack_frames(frames, full_stack, client_options): | ||
# type: (List[Dict[str, Any]], List[Dict[str, Any]], Optional[Dict[str, Any]]) -> List[Dict[str, Any]] | ||
""" | ||
Add the missing frames from full_stack to frames and return the merged list. | ||
""" | ||
frame_ids = { | ||
( | ||
frame["abs_path"], | ||
frame["context_line"], | ||
frame["lineno"], | ||
frame["function"], | ||
) | ||
for frame in frames | ||
} | ||
|
||
new_frames = [ | ||
stackframe | ||
for stackframe in full_stack | ||
if ( | ||
stackframe["abs_path"], | ||
stackframe["context_line"], | ||
stackframe["lineno"], | ||
stackframe["function"], | ||
) | ||
not in frame_ids | ||
] | ||
new_frames.extend(frames) | ||
|
||
# Limit the number of frames | ||
max_stack_frames = ( | ||
client_options.get("max_stack_frames", DEFAULT_MAX_STACK_FRAMES) | ||
if client_options | ||
else None | ||
) | ||
if max_stack_frames is not None: | ||
new_frames = new_frames[len(new_frames) - max_stack_frames :] | ||
|
||
return new_frames | ||
|
||
|
||
def event_from_exception( | ||
exc_info, # type: Union[BaseException, ExcInfo] | ||
client_options=None, # type: Optional[Dict[str, Any]] | ||
|
@@ -1104,12 +1194,18 @@ def event_from_exception( | |
# type: (...) -> Tuple[Event, Dict[str, Any]] | ||
exc_info = exc_info_from_error(exc_info) | ||
hint = event_hint_with_exc_info(exc_info) | ||
|
||
if client_options and client_options.get("add_full_stack", DEFAULT_ADD_FULL_STACK): | ||
full_stack = get_full_stack() | ||
else: | ||
full_stack = None | ||
|
||
return ( | ||
{ | ||
"level": "error", | ||
"exception": { | ||
"values": exceptions_from_error_tuple( | ||
exc_info, client_options, mechanism | ||
exc_info, client_options, mechanism, full_stack | ||
) | ||
}, | ||
}, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import sentry_sdk | ||
|
||
|
||
def test_full_stack_frames_default(sentry_init, capture_events): | ||
sentry_init() | ||
events = capture_events() | ||
|
||
def foo(): | ||
try: | ||
bar() | ||
except Exception as e: | ||
sentry_sdk.capture_exception(e) | ||
|
||
def bar(): | ||
raise Exception("This is a test exception") | ||
|
||
foo() | ||
|
||
(event,) = events | ||
frames = event["exception"]["values"][0]["stacktrace"]["frames"] | ||
|
||
assert len(frames) == 2 | ||
assert frames[-1]["function"] == "bar" | ||
assert frames[-2]["function"] == "foo" | ||
|
||
|
||
def test_full_stack_frames_enabled(sentry_init, capture_events): | ||
sentry_init( | ||
add_full_stack=True, | ||
) | ||
events = capture_events() | ||
|
||
def foo(): | ||
try: | ||
bar() | ||
except Exception as e: | ||
sentry_sdk.capture_exception(e) | ||
|
||
def bar(): | ||
raise Exception("This is a test exception") | ||
|
||
foo() | ||
|
||
(event,) = events | ||
frames = event["exception"]["values"][0]["stacktrace"]["frames"] | ||
|
||
assert len(frames) > 2 | ||
assert frames[-1]["function"] == "bar" | ||
assert frames[-2]["function"] == "foo" | ||
assert frames[-3]["function"] == "foo" | ||
assert frames[-4]["function"] == "test_full_stack_frames_enabled" | ||
|
||
|
||
def test_full_stack_frames_enabled_truncated(sentry_init, capture_events): | ||
sentry_init( | ||
add_full_stack=True, | ||
max_stack_frames=3, | ||
) | ||
events = capture_events() | ||
|
||
def foo(): | ||
try: | ||
bar() | ||
except Exception as e: | ||
sentry_sdk.capture_exception(e) | ||
|
||
def bar(): | ||
raise Exception("This is a test exception") | ||
|
||
foo() | ||
|
||
(event,) = events | ||
frames = event["exception"]["values"][0]["stacktrace"]["frames"] | ||
|
||
assert len(frames) == 3 | ||
assert frames[-1]["function"] == "bar" | ||
assert frames[-2]["function"] == "foo" | ||
assert frames[-3]["function"] == "foo" | ||
|
||
|
||
def test_full_stack_frames_default_no_truncation_happening(sentry_init, capture_events): | ||
sentry_init( | ||
max_stack_frames=1, # this is ignored if add_full_stack=False (which is the default) | ||
) | ||
events = capture_events() | ||
|
||
def foo(): | ||
try: | ||
bar() | ||
except Exception as e: | ||
sentry_sdk.capture_exception(e) | ||
|
||
def bar(): | ||
raise Exception("This is a test exception") | ||
|
||
foo() | ||
|
||
(event,) = events | ||
frames = event["exception"]["values"][0]["stacktrace"]["frames"] | ||
|
||
assert len(frames) == 2 | ||
assert frames[-1]["function"] == "bar" | ||
assert frames[-2]["function"] == "foo" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should this replace the code in the logging plugin which does similar?