From 2c768b6b58c0a7a17dbdc30e09c3d2c4059e286c Mon Sep 17 00:00:00 2001 From: Philipp Hofmann Date: Mon, 3 Jul 2023 16:06:07 +0200 Subject: [PATCH] feat(sdk-crash-detection): Set in app for SDK frames (#52083) Set in_app to true only for SDK frames and to false for all others. Closes #51024 --- .../utils/sdk_crashes/event_stripper.py | 15 +++++--- .../utils/sdk_crashes/test_event_stripper.py | 36 ++++++++++++++++++- 2 files changed, 46 insertions(+), 5 deletions(-) diff --git a/src/sentry/utils/sdk_crashes/event_stripper.py b/src/sentry/utils/sdk_crashes/event_stripper.py index 6bd664d9e0779a..601e75adbc4f80 100644 --- a/src/sentry/utils/sdk_crashes/event_stripper.py +++ b/src/sentry/utils/sdk_crashes/event_stripper.py @@ -1,5 +1,5 @@ from enum import Enum, auto -from typing import Any, Dict, Mapping, Optional, Sequence +from typing import Any, Dict, Mapping, MutableMapping, Optional, Sequence from sentry.db.models import NodeData from sentry.utils.safe import get_path @@ -98,11 +98,11 @@ def strip_event_data( if (new_event_data is None) or (new_event_data == {}): return {} - stripped_frames: Sequence[Mapping[str, Any]] = [] frames = get_path(new_event_data, "exception", "values", -1, "stacktrace", "frames") if frames is not None: stripped_frames = _strip_frames(frames, sdk_crash_detector) + new_event_data["exception"]["values"][0]["stacktrace"]["frames"] = stripped_frames return new_event_data @@ -144,7 +144,7 @@ def _strip_event_data_with_allowlist( def _strip_frames( - frames: Sequence[Mapping[str, Any]], sdk_crash_detector: SDKCrashDetector + frames: Sequence[MutableMapping[str, Any]], sdk_crash_detector: SDKCrashDetector ) -> Sequence[Mapping[str, Any]]: """ Only keep SDK frames or Apple system libraries. @@ -162,8 +162,15 @@ def is_system_library(frame: Mapping[str, Any]) -> bool: return False + def strip_frame(frame: MutableMapping[str, Any]) -> MutableMapping[str, Any]: + if sdk_crash_detector.is_sdk_frame(frame): + frame["in_app"] = True + else: + frame["in_app"] = False + return frame + return [ - frame + strip_frame(frame) for frame in frames if sdk_crash_detector.is_sdk_frame(frame) or is_system_library(frame) ] diff --git a/tests/sentry/utils/sdk_crashes/test_event_stripper.py b/tests/sentry/utils/sdk_crashes/test_event_stripper.py index c8fefa51da5778..6bff4fbe4ea0c8 100644 --- a/tests/sentry/utils/sdk_crashes/test_event_stripper.py +++ b/tests/sentry/utils/sdk_crashes/test_event_stripper.py @@ -161,6 +161,40 @@ def test_strip_event_data_keeps_exception_mechanism(self): }, } + def test_set_in_app_only_for_sdk_frames(self): + frames = get_frames("SentryCrashMonitor_CPPException.cpp", sentry_frame_in_app=False) + + system_frame_in_app = [ + { + "abs_path": "/System/Library/PrivateFrameworks/UIKitCore.framework/UIKitCore", + "in_app": True, + } + ] + + event_data = get_crash_event_with_frames(system_frame_in_app + list(frames)) + + event = self.create_event( + data=event_data, + project_id=self.project.id, + ) + + stripped_event_data = strip_event_data(event.data, CocoaSDKCrashDetector()) + + stripped_frames = get_path( + stripped_event_data, "exception", "values", -1, "stacktrace", "frames" + ) + + for stripped_frame in stripped_frames[0::-1]: + assert stripped_frame["in_app"] is False + + cocoa_sdk_frame = stripped_frames[-1] + assert cocoa_sdk_frame == { + "function": "SentryCrashMonitor_CPPException.cpp", + "package": "/private/var/containers/Bundle/Application/59E988EF-46DB-4C75-8E08-10C27DC3E90E/iOS-Swift.app/Frameworks/Sentry.framework/Sentry", + "in_app": True, + "image_addr": "0x100304000", + } + def test_strip_event_data_keeps_exception_stacktrace(self): event = self.create_event( data=get_crash_event(), @@ -229,7 +263,7 @@ def test_strip_frames(self): assert cocoa_sdk_frame == { "function": "SentryCrashMonitor_CPPException.cpp", "package": "/private/var/containers/Bundle/Application/59E988EF-46DB-4C75-8E08-10C27DC3E90E/iOS-Swift.app/Frameworks/Sentry.framework/Sentry", - "in_app": False, + "in_app": True, "image_addr": "0x100304000", }