Skip to content
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

Sentry SDK Crash Detection POC #49928

Closed
wants to merge 51 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
8f39673
test: Fix test discovery by adding missing init.py
philipphofmann Feb 3, 2023
48cd13a
backup
philipphofmann Feb 6, 2023
beb3c77
backup
philipphofmann Feb 7, 2023
9cdf838
Merge branch 'master' into feat/sdk-crash-monitoring
philipphofmann Feb 8, 2023
ef31472
Change frame order from newest to oldest
philipphofmann Feb 8, 2023
3b6bf5c
Rename to Cocoa
philipphofmann Feb 8, 2023
9cc03e6
Add events
philipphofmann Feb 8, 2023
fe89218
strip frames
philipphofmann Feb 9, 2023
db8f089
clarify test method names
philipphofmann Feb 9, 2023
0592d1c
add more fields to event
philipphofmann Feb 9, 2023
8eda480
use mock
philipphofmann Feb 13, 2023
27482bd
Use mocks
philipphofmann Feb 13, 2023
590718f
Merge branch 'master' into feat/sdk-crash-monitoring
philipphofmann Feb 14, 2023
ece3894
givenCrashDetector
philipphofmann Feb 14, 2023
53a4801
rename to event
philipphofmann Feb 14, 2023
7f859e3
Make _is_cocoa_sdk_crash private
philipphofmann Feb 14, 2023
83a578d
add event stripper
philipphofmann Feb 14, 2023
fc1d72e
Merge branch 'master' into feat/sdk-crash-monitoring
philipphofmann Mar 22, 2023
be86e37
strip frames before calling report
philipphofmann Mar 22, 2023
d842a57
Merge branch 'master' into feat/sdk-crash-monitoring
philipphofmann Mar 29, 2023
4216b94
add base class
philipphofmann Mar 29, 2023
b9e95eb
split up into files
philipphofmann Mar 29, 2023
a793793
fix tests
philipphofmann Mar 29, 2023
46dbf5d
move strip frames tests
philipphofmann Mar 29, 2023
0e29e2e
Fix compile error in event manager
philipphofmann Mar 29, 2023
8a36861
Merge branch 'master' into feat/sdk-crash-monitoring
philipphofmann Mar 30, 2023
b0a6ac3
Only keep in stack trace referenced debug images (#46494)
philipphofmann Mar 30, 2023
c3ddb38
Merge branch 'master' into feat/sdk-crash-monitoring
philipphofmann Apr 3, 2023
3b8c023
Avoid infinite processing loop (#46578)
philipphofmann Apr 3, 2023
e4d3300
add __init__.py file
philipphofmann Apr 4, 2023
eea92f2
Merge branch 'master' into feat/sdk-crash-monitoring
philipphofmann Apr 6, 2023
30d10c6
Merge branch 'master' into feat/sdk-crash-monitoring
philipphofmann May 8, 2023
ec5055f
Merge branch 'master' into feat/sdk-crash-monitoring
philipphofmann May 16, 2023
2958db6
Merge branch 'master' into feat/sdk-crash-monitoring
philipphofmann May 19, 2023
5744b75
Merge branch 'master' into feat/sdk-crash-monitoring
philipphofmann May 25, 2023
087e2c1
Merge branch 'master' into feat/sdk-crash-monitoring
philipphofmann May 30, 2023
ed415e4
feat(sdk-crash-detection): Call code in post processing (#49170)
philipphofmann May 30, 2023
95d93cc
Merge branch 'master' into feat/sdk-crash-monitoring
philipphofmann Jun 5, 2023
90b1967
feat(sdk-crash-detection): Add feature flag (#49528)
philipphofmann Jun 5, 2023
082ddda
feat(sdk-crash-detection): Store event to dedicated project (#49593)
philipphofmann Jun 5, 2023
cf3d1fc
fix(sdk-crash-detection): Reverse frame order (#49746)
philipphofmann Jun 5, 2023
9870f03
chore(sdk-crash-detection): Add to mypy.ini (#50284)
philipphofmann Jun 5, 2023
a2c982d
move fixture to fixtures namespace
philipphofmann Jun 12, 2023
af352dd
Remove empty inits
philipphofmann Jun 12, 2023
629e2eb
snake_case not CamelCase
philipphofmann Jun 12, 2023
35e2b0e
make allowed event keys a frozenset
philipphofmann Jun 12, 2023
045a9f7
feat(sdk-crash-detection): Event data allowlist (#50400)
philipphofmann Jun 12, 2023
bf72f45
Merge branch 'master' into feat/sdk-crash-monitoring
philipphofmann Jun 12, 2023
e1785ff
remove duplicate test fixtures
philipphofmann Jun 12, 2023
c710f36
fix post process tests
philipphofmann Jun 12, 2023
ea6b6dc
fix type issues
philipphofmann Jun 12, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
269 changes: 269 additions & 0 deletions fixtures/sdk_crash_detection/crash_event.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,269 @@
from typing import Any, Collection, Dict, Mapping, Sequence

IN_APP_FRAME = {
"function": "LoginViewController.viewDidAppear",
"raw_function": "LoginViewController.viewDidAppear(Bool)",
"symbol": "$s8Sentry9LoginViewControllerC13viewDidAppearyySbF",
"package": "SentryApp",
"filename": "LoginViewController.swift",
"abs_path": "/Users/sentry/git/iOS/Sentry/LoggedOut/LoginViewController.swift",
"lineno": 196,
"in_app": True,
"image_addr": "0x100260000",
"instruction_addr": "0x102b16630",
"symbol_addr": "0x100260000",
}


def get_sentry_frame(function: str, in_app: bool = False) -> Mapping[str, Any]:
return {
"function": function,
"package": "Sentry",
"in_app": in_app,
"image_addr": "0x100304000",
}


def get_frames(function: str, sentry_frame_in_app: bool = False) -> Sequence[Mapping[str, Any]]:
frames = [
get_sentry_frame(function, sentry_frame_in_app),
{
"function": "LoginViewController.viewDidAppear",
"symbol": "$s8Sentry9LoginViewControllerC13viewDidAppearyySbF",
"package": "SentryApp",
"in_app": True,
"filename": "LoginViewController.swift",
"image_addr": "0x100260000",
},
IN_APP_FRAME,
{
"function": "-[UIViewController _setViewAppearState:isAnimating:]",
"symbol": "-[UIViewController _setViewAppearState:isAnimating:]",
"package": "UIKitCore",
"in_app": False,
"image_addr": "0x1a4e8f000",
},
{
"function": "-[UIViewController __viewDidAppear:]",
"symbol": "-[UIViewController __viewDidAppear:]",
"package": "UIKitCore",
"in_app": False,
"image_addr": "0x1a4e8f000",
},
{
"function": "-[UIViewController _endAppearanceTransition:]",
"symbol": "-[UIViewController _endAppearanceTransition:]",
"package": "UIKitCore",
"in_app": False,
"image_addr": "0x1a4e8f000",
},
{
"function": "-[UINavigationController navigationTransitionView:didEndTransition:fromView:toView:]",
"symbol": "-[UINavigationController navigationTransitionView:didEndTransition:fromView:toView:]",
"package": "UIKitCore",
"in_app": False,
"image_addr": "0x1a4e8f000",
},
{
"function": "__49-[UINavigationController _startCustomTransition:]_block_invoke",
"symbol": "__49-[UINavigationController _startCustomTransition:]_block_invoke",
"package": "UIKitCore",
"in_app": False,
"image_addr": "0x1a4e8f000",
},
]

# The frames have to be ordered from caller to callee, or oldest to youngest.
# The last frame is the one creating the exception.
# As we usually look at stacktraces from youngest to oldest, we reverse the order.
return frames[::-1]


def get_crash_event(handled=False, function="-[Sentry]", **kwargs) -> Dict[str, Collection[str]]:
return get_crash_event_with_frames(get_frames(function), handled=handled, **kwargs)


def get_crash_event_with_frames(
frames: Sequence[Mapping[str, Any]], handled=False, **kwargs
) -> Dict[str, Collection[str]]:
result = {
"event_id": "80e3496eff734ab0ac993167aaa0d1cd",
"release": "5.222.5",
"type": "error",
"level": "fatal",
"platform": "cocoa",
"tags": {"level": "fatal"},
"exception": {
"values": [
{
"stacktrace": {
"frames": frames,
},
"type": "SIGABRT",
"mechanism": {"handled": handled},
}
]
},
"breadcrumbs": {
"values": [
{
"timestamp": 1675900265.0,
"type": "debug",
"category": "started",
"level": "info",
"message": "Breadcrumb Tracking",
},
]
},
"contexts": {
"app": {
"app_start_time": "2023-02-08T23:51:05Z",
"device_app_hash": "8854fe9e3d4e4a66493baee798bfae0228efabf1",
"build_type": "app store",
"app_identifier": "com.some.company.io",
"app_name": "SomeCompany",
"app_version": "5.222.5",
"app_build": "21036",
"app_id": "397D4F75-6C01-32D1-BF46-62098979E470",
"type": "app",
},
"device": {
"family": "iOS",
"model": "iPhone14,8",
"model_id": "D28AP",
"arch": "arm64e",
"memory_size": 5944508416,
"free_memory": 102154240,
"usable_memory": 4125687808,
"storage_size": 127854202880,
"boot_time": "2023-02-01T05:21:23Z",
"timezone": "PST",
"type": "device",
},
"os": {
"name": "iOS",
"version": "16.3",
"build": "20D47",
"kernel_version": "Darwin Kernel Version 22.3.0: Wed Jan 4 21:25:19 PST 2023; root:xnu-8792.82.2~1/RELEASE_ARM64_T8110",
"rooted": False,
"type": "os",
},
},
"debug_meta": {
"images": [
{
"code_file": "/private/var/containers/Bundle/Application/895DA2DE-5FE3-44A0-8C0F-900519EA5516/iOS-Swift.app/iOS-Swift",
"debug_id": "aa8a3697-c88a-36f9-a687-3d3596568c8d",
"arch": "arm64",
"image_addr": "0x100260000",
"image_size": 180224,
"image_vmaddr": "0x100000000",
"type": "macho",
},
{
"code_file": "/private/var/containers/Bundle/Application/9EB557CD-D653-4F51-BFCE-AECE691D4347/iOS-Swift.app/Frameworks/Sentry.framework/Sentry",
"debug_id": "e2623c4d-79c5-3cdf-90ab-2cf44e026bdd",
"arch": "arm64",
"image_addr": "0x100304000",
"image_size": 802816,
"type": "macho",
},
{
"code_file": "/System/Library/PrivateFrameworks/UIKitCore.framework/UIKitCore",
"debug_id": "b0858d8e-7220-37bf-873f-ecc2b0a358c3",
"arch": "arm64e",
"image_addr": "0x1a4e8f000",
"image_size": 25309184,
"image_vmaddr": "0x188ff7000",
"type": "macho",
},
{
"code_file": "/System/Library/Frameworks/CFNetwork.framework/CFNetwork",
"debug_id": "b2273be9-538a-3f56-b9c7-801f39550f58",
"arch": "arm64e",
"image_addr": "0x1a3e32000",
"image_size": 3977216,
"image_vmaddr": "0x187f9a000",
"in_app": False,
"type": "macho",
},
]
},
"environment": "test-app",
"sdk": {
"name": "sentry.cocoa",
"version": "8.1.0",
"integrations": [
"Crash",
"PerformanceTracking",
"MetricKit",
"WatchdogTerminationTracking",
"ViewHierarchy",
"NetworkTracking",
"ANRTracking",
"AutoBreadcrumbTracking",
"FramesTracking",
"AppStartTracking",
"Screenshot",
"FileIOTracking",
"UIEventTracking",
"AutoSessionTracking",
"CoreDataTracking",
"PreWarmedAppStartTracing",
],
},
"threads": {
"values": [
{
"id": 0,
"stacktrace": {
"frames": [
{
"function": "<redacted>",
"in_app": False,
"data": {"symbolicator_status": "unknown_image"},
"image_addr": "0x0",
"instruction_addr": "0x1129be52e",
"symbol_addr": "0x0",
},
{
"function": "<redacted>",
"in_app": False,
"data": {"symbolicator_status": "unknown_image"},
"image_addr": "0x0",
"instruction_addr": "0x104405f21",
"symbol_addr": "0x0",
},
],
},
"raw_stacktrace": {
"frames": [
{
"function": "<redacted>",
"in_app": False,
"image_addr": "0x0",
"instruction_addr": "0x1129be52e",
"symbol_addr": "0x0",
},
{
"function": "<redacted>",
"in_app": False,
"image_addr": "0x0",
"instruction_addr": "0x104405f21",
"symbol_addr": "0x0",
},
],
},
"crashed": True,
}
]
},
"user": {
"id": "803F5C87-0F8B-41C7-8499-27BD71A92738",
"ip_address": "192.168.0.1",
"geo": {"country_code": "US", "region": "United States"},
},
"logger": "my.logger.name",
}
result.update(kwargs)
return result
5 changes: 5 additions & 0 deletions src/sentry/conf/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -1568,6 +1568,8 @@ def SOCIAL_AUTH_DEFAULT_USERNAME() -> str:
"organizations:slack-escape-messages": False,
# If true, allow to create/use org auth tokens
"organizations:org-auth-tokens": False,
# Enable detecting SDK crashes during event processing
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code was already reviewed in #49528

"organizations:sdk-crash-detection": False,
# Enables commenting on PRs from the Sentry comment bot.
"organizations:pr-comment-bot": False,
# Adds additional filters and a new section to issue alert rules.
Expand Down Expand Up @@ -3446,3 +3448,6 @@ class TopicDefinition(TypedDict):
# The Redis cluster to use for monitoring the health of
# Celery queues.
SENTRY_QUEUE_MONITORING_REDIS_CLUSTER = "default"

# The project ID for SDK Crash Monitoring to save the detected SDK crashed to.
SDK_CRASH_DETECTION_PROJECT_ID: Optional[int] = None
1 change: 1 addition & 0 deletions src/sentry/features/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@
default_manager.add("organizations:set-grouping-config", OrganizationFeature, FeatureHandlerStrategy.INTERNAL)
default_manager.add("organizations:slack-escape-messages", OrganizationFeature, FeatureHandlerStrategy.INTERNAL)
default_manager.add("organizations:slack-overage-notifications", OrganizationFeature, FeatureHandlerStrategy.REMOTE)
default_manager.add("organizations:sdk-crash-detection", OrganizationFeature, FeatureHandlerStrategy.INTERNAL)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code was reviewed in #49528

default_manager.add("organizations:starfish-view", OrganizationFeature, FeatureHandlerStrategy.REMOTE)
default_manager.add("organizations:starfish-test-endpoint", OrganizationFeature, FeatureHandlerStrategy.REMOTE)
default_manager.add("organizations:streamline-targeting-context", OrganizationFeature, FeatureHandlerStrategy.REMOTE)
Expand Down
25 changes: 25 additions & 0 deletions src/sentry/tasks/post_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -1056,6 +1056,30 @@ def fire_error_processed(job: PostProcessJob):
)


def sdk_crash_monitoring(job: PostProcessJob):
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code was reviewed in #49170

from sentry.utils.sdk_crashes.sdk_crash_detection import sdk_crash_detection

if job["is_reprocessed"]:
return

event = job["event"]

if not features.has("organizations:sdk-crash-detection", event.project.organization):
return

if settings.SDK_CRASH_DETECTION_PROJECT_ID is None:
logger.warning(
"SDK crash detection is enabled but SDK_CRASH_DETECTION_PROJECT_ID is not set."
)
return None

with metrics.timer("post_process.sdk_crash_monitoring.duration"):
with sentry_sdk.start_span(op="tasks.post_process_group.sdk_crash_monitoring"):
sdk_crash_detection.detect_sdk_crash(
event=event, event_project_id=settings.SDK_CRASH_DETECTION_PROJECT_ID
)


def plugin_post_process_group(plugin_slug, event, **kwargs):
"""
Fires post processing hooks for a group.
Expand Down Expand Up @@ -1091,6 +1115,7 @@ def plugin_post_process_group(plugin_slug, event, **kwargs):
process_similarity,
update_existing_attachments,
fire_error_processed,
sdk_crash_monitoring,
],
GroupCategory.PERFORMANCE: [
process_snoozes,
Expand Down
Empty file.
45 changes: 45 additions & 0 deletions src/sentry/utils/sdk_crashes/cocoa_sdk_crash_detector.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from typing import Any, Mapping, Sequence

from sentry.utils.glob import glob_match
from sentry.utils.sdk_crashes.sdk_crash_detector import SDKCrashDetector


class CocoaSDKCrashDetector(SDKCrashDetector):
def is_sdk_crash(self, frames: Sequence[Mapping[str, Any]]) -> bool:
if not frames:
return False

# The frames are ordered from caller to callee, or oldest to youngest.
# The last frame is the one creating the exception.
# Therefore, we must iterate in reverse order.
for frame in reversed(frames):
if self.is_sdk_frame(frame):
return True

if frame.get("in_app") is True:
return False

return False

def is_sdk_frame(self, frame: Mapping[str, Any]) -> bool:
function = frame.get("function")

if function is not None:
# [SentrySDK crash] is a testing function causing a crash.
# Therefore, we don't want to mark it a as a SDK crash.
if "SentrySDK crash" in function:
return False

function_matchers = ["*sentrycrash*", "**[[]Sentry*"]
for matcher in function_matchers:
if glob_match(frame.get("function"), matcher, ignorecase=True):
return True

filename = frame.get("filename")
if filename is not None:
filenameMatchers = ["Sentry**"]
for matcher in filenameMatchers:
if glob_match(filename, matcher, ignorecase=True):
return True

return False
Loading