Skip to content

Commit

Permalink
feat(sdk-crashes): Add ReactNative options
Browse files Browse the repository at this point in the history
Add ReactNative options as a first step to enable SDK crash detection
for ReactNative. As the sample rate is 0.0 and no config for ReactNative
exists, this PR doesn't change any behavior.
  • Loading branch information
philipphofmann committed Nov 2, 2023
1 parent de2bdc9 commit e868538
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 18 deletions.
13 changes: 13 additions & 0 deletions src/sentry/options/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -1673,4 +1673,17 @@
default=1.0,
flags=FLAG_AUTOMATOR_MODIFIABLE,
)

# The project ID belongs to the sentry organization: https://sentry.sentry.io/projects/cocoa-sdk-crashes/?project=4506155486085120.
register(
"issues.sdk_crash_detection.react-native.project_id",
default=4506155486085120,
flags=FLAG_AUTOMATOR_MODIFIABLE,
)

register(
"issues.sdk_crash_detection.react-native.sample_rate",
default=0.0,
flags=FLAG_AUTOMATOR_MODIFIABLE,
)
# END: SDK Crash Detection
38 changes: 20 additions & 18 deletions src/sentry/utils/sdk_crashes/build_sdk_crash_detection_configs.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Sequence
from typing import Optional, Sequence

import sentry_sdk

Expand All @@ -7,20 +7,22 @@


def build_sdk_crash_detection_configs() -> Sequence[SDKCrashDetectionConfig]:
cocoa_project_id = options.get(
"issues.sdk_crash_detection.cocoa.project_id",
)
if not cocoa_project_id or cocoa_project_id == 0:
sentry_sdk.capture_message("Cocoa project_id is not set.")
return []

cocoa_sample_rate = options.get("issues.sdk_crash_detection.cocoa.sample_rate")
# When the sample rate is 0, we can skip the sdk crash detection.
if not cocoa_sample_rate or cocoa_sample_rate == 0:
return []

cocoa_config = SDKCrashDetectionConfig(
sdk_name=SdkName.Cocoa, project_id=cocoa_project_id, sample_rate=cocoa_sample_rate
)

return [cocoa_config]
def build_config(sdk_name: SdkName) -> Optional[SDKCrashDetectionConfig]:
options_prefix = f"issues.sdk_crash_detection.{sdk_name.value}"

project_id = options.get(f"{options_prefix}.project_id")
if not project_id or project_id == 0:
sentry_sdk.capture_message(f"{sdk_name.value} project_id is not set.")
return None

sample_rate = options.get(f"{options_prefix}.sample_rate")
if not sample_rate or sample_rate == 0:
return None

return SDKCrashDetectionConfig(
sdk_name=sdk_name, project_id=project_id, sample_rate=sample_rate
)

configs = [build_config(SdkName.Cocoa), build_config(SdkName.ReactNative)]

return [config for config in configs if config is not None]
1 change: 1 addition & 0 deletions src/sentry/utils/sdk_crashes/sdk_crash_detection_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
@unique
class SdkName(Enum):
Cocoa = "cocoa"
ReactNative = "react-native"


class SDKCrashDetectionConfig(TypedDict):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
from sentry.testutils.helpers.options import override_options
from sentry.utils.sdk_crashes.build_sdk_crash_detection_configs import (
build_sdk_crash_detection_configs,
)
from sentry.utils.sdk_crashes.sdk_crash_detection_config import SDKCrashDetectionConfig, SdkName


@override_options(
{
"issues.sdk_crash_detection.cocoa.project_id": 1,
"issues.sdk_crash_detection.cocoa.sample_rate": 0.1,
"issues.sdk_crash_detection.react-native.project_id": 2,
"issues.sdk_crash_detection.react-native.sample_rate": 0.2,
}
)
def test_build_sdk_crash_detection_configs():
configs = build_sdk_crash_detection_configs()

assert configs == [
SDKCrashDetectionConfig(sdk_name=SdkName.Cocoa, project_id=1, sample_rate=0.1),
SDKCrashDetectionConfig(sdk_name=SdkName.ReactNative, project_id=2, sample_rate=0.2),
]


@override_options(
{
"issues.sdk_crash_detection.cocoa.project_id": None,
"issues.sdk_crash_detection.cocoa.sample_rate": None,
"issues.sdk_crash_detection.react-native.project_id": 2,
"issues.sdk_crash_detection.react-native.sample_rate": 0.2,
}
)
def test_build_sdk_crash_detection_configs_only_react_native():
configs = build_sdk_crash_detection_configs()

assert configs == [
SDKCrashDetectionConfig(sdk_name=SdkName.ReactNative, project_id=2, sample_rate=0.2),
]


@override_options(
{
"issues.sdk_crash_detection.cocoa.project_id": 1.0,
"issues.sdk_crash_detection.cocoa.sample_rate": None,
"issues.sdk_crash_detection.react-native.project_id": 2,
"issues.sdk_crash_detection.react-native.sample_rate": 0.2,
}
)
def test_build_sdk_crash_detection_configs_no_sample_rate():
configs = build_sdk_crash_detection_configs()

assert configs == [
SDKCrashDetectionConfig(sdk_name=SdkName.ReactNative, project_id=2, sample_rate=0.2),
]


@override_options(
{
"issues.sdk_crash_detection.cocoa.project_id": None,
"issues.sdk_crash_detection.cocoa.sample_rate": None,
"issues.sdk_crash_detection.react-native.project_id": None,
"issues.sdk_crash_detection.react-native.sample_rate": None,
}
)
def test_build_sdk_crash_detection_configs_no_configs():
assert len(build_sdk_crash_detection_configs()) == 0

0 comments on commit e868538

Please sign in to comment.