Skip to content

Commit c4d254f

Browse files
authored
ref: Simplify integrations setup (#6668)
1 parent 5e5d875 commit c4d254f

File tree

5 files changed

+68
-113
lines changed

5 files changed

+68
-113
lines changed

Sources/Sentry/SentryOptions.m

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,6 @@ - (void)setBeforeSendLogDynamic:(id)beforeSendLogDynamic
4444

4545
#endif // SWIFT_PACKAGE || SENTRY_TEST
4646

47-
+ (NSArray<NSString *> *)defaultIntegrations
48-
{
49-
NSArray<Class> *defaultIntegrationClasses = [SentryOptionsInternal defaultIntegrationClasses];
50-
NSMutableArray<NSString *> *defaultIntegrationNames =
51-
[[NSMutableArray alloc] initWithCapacity:defaultIntegrationClasses.count];
52-
53-
for (Class class in defaultIntegrationClasses) {
54-
[defaultIntegrationNames addObject:NSStringFromClass(class)];
55-
}
56-
57-
return defaultIntegrationNames;
58-
}
59-
6047
- (instancetype)init
6148
{
6249
if (self = [super init]) {

Sources/Sentry/SentryOptionsInternal.m

Lines changed: 0 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,13 @@
11
#import "SentryOptionsInternal.h"
2-
#import "SentryANRTrackingIntegration.h"
3-
#import "SentryAutoBreadcrumbTrackingIntegration.h"
4-
#import "SentryAutoSessionTrackingIntegration.h"
5-
#import "SentryCoreDataTrackingIntegration.h"
6-
#import "SentryCrashIntegration.h"
72
#import "SentryDsn.h"
8-
#import "SentryFileIOTrackingIntegration.h"
93
#import "SentryInternalDefines.h"
104
#import "SentryLevelMapper.h"
11-
#import "SentryNetworkTrackingIntegration.h"
125
#import "SentryOptions+Private.h"
136
#import "SentryOptions.h"
14-
#import "SentrySessionReplayIntegration.h"
157
#import "SentrySwift.h"
16-
#import "SentrySwiftAsyncIntegration.h"
17-
18-
#if SENTRY_HAS_UIKIT
19-
# import "SentryAppStartTrackingIntegration.h"
20-
# import "SentryFramesTrackingIntegration.h"
21-
# import "SentryPerformanceTrackingIntegration.h"
22-
# import "SentryScreenshotIntegration.h"
23-
# import "SentryUIEventTrackingIntegration.h"
24-
# import "SentryUserFeedbackIntegration.h"
25-
# import "SentryViewHierarchyIntegration.h"
26-
# import "SentryWatchdogTerminationTrackingIntegration.h"
27-
#endif // SENTRY_HAS_UIKIT
28-
29-
#if SENTRY_HAS_METRIC_KIT
30-
# import "SentryMetricKitIntegration.h"
31-
#endif // SENTRY_HAS_METRIC_KIT
328

339
@implementation SentryOptionsInternal
3410

35-
+ (NSArray<Class> *)defaultIntegrationClasses
36-
{
37-
// The order of integrations here is important.
38-
// SentryCrashIntegration needs to be initialized before SentryAutoSessionTrackingIntegration.
39-
// And SentrySessionReplayIntegration before SentryCrashIntegration.
40-
NSMutableArray<Class> *defaultIntegrations = [NSMutableArray<Class> arrayWithObjects:
41-
#if SENTRY_TARGET_REPLAY_SUPPORTED
42-
[SentrySessionReplayIntegration class],
43-
#endif // SENTRY_TARGET_REPLAY_SUPPORTED
44-
[SentryCrashIntegration class],
45-
#if SENTRY_HAS_UIKIT
46-
[SentryAppStartTrackingIntegration class], [SentryFramesTrackingIntegration class],
47-
[SentryPerformanceTrackingIntegration class], [SentryUIEventTrackingIntegration class],
48-
[SentryViewHierarchyIntegration class],
49-
[SentryWatchdogTerminationTrackingIntegration class],
50-
#endif // SENTRY_HAS_UIKIT
51-
#if SENTRY_TARGET_REPLAY_SUPPORTED
52-
[SentryScreenshotIntegration class],
53-
#endif // SENTRY_TARGET_REPLAY_SUPPORTED
54-
[SentryANRTrackingIntegration class], [SentryAutoBreadcrumbTrackingIntegration class],
55-
[SentryAutoSessionTrackingIntegration class], [SentryCoreDataTrackingIntegration class],
56-
[SentryFileIOTrackingIntegration class], [SentryNetworkTrackingIntegration class],
57-
[SentrySwiftAsyncIntegration class], nil];
58-
59-
#if TARGET_OS_IOS && SENTRY_HAS_UIKIT
60-
[defaultIntegrations addObject:[SentryUserFeedbackIntegration class]];
61-
#endif // TARGET_OS_IOS && SENTRY_HAS_UIKIT
62-
63-
#if SENTRY_HAS_METRIC_KIT
64-
[defaultIntegrations addObject:[SentryMetricKitIntegration class]];
65-
#endif // SENTRY_HAS_METRIC_KIT
66-
67-
return defaultIntegrations;
68-
}
69-
7011
+ (nullable SentryOptions *)initWithDict:(NSDictionary<NSString *, id> *)options
7112
didFailWithError:(NSError *_Nullable *_Nullable)error
7213
{

Sources/Sentry/SentrySDKInternal.m

Lines changed: 68 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,19 @@
22
#import "PrivateSentrySDKOnly.h"
33
#import "SentryANRTrackingIntegration.h"
44
#import "SentryAppStartMeasurement.h"
5+
#import "SentryAutoBreadcrumbTrackingIntegration.h"
6+
#import "SentryAutoSessionTrackingIntegration.h"
57
#import "SentryBreadcrumb.h"
68
#import "SentryClient+Private.h"
9+
#import "SentryCoreDataTrackingIntegration.h"
710
#import "SentryCrash.h"
11+
#import "SentryCrashIntegration.h"
12+
#import "SentryFileIOTrackingIntegration.h"
813
#import "SentryHub+Private.h"
914
#import "SentryInternalDefines.h"
1015
#import "SentryLogC.h"
1116
#import "SentryMeta.h"
17+
#import "SentryNetworkTrackingIntegration.h"
1218
#import "SentryOptions+Private.h"
1319
#import "SentryOptionsInternal.h"
1420
#import "SentryProfilingConditionals.h"
@@ -17,11 +23,28 @@
1723
#import "SentrySamplingContext.h"
1824
#import "SentryScope.h"
1925
#import "SentrySerialization.h"
26+
#import "SentrySessionReplayIntegration.h"
2027
#import "SentrySwift.h"
28+
#import "SentrySwiftAsyncIntegration.h"
2129
#import "SentryTransactionContext.h"
2230
#import "SentryUseNSExceptionCallstackWrapper.h"
2331
#import "SentryUserFeedbackIntegration.h"
2432

33+
#if SENTRY_HAS_UIKIT
34+
# import "SentryAppStartTrackingIntegration.h"
35+
# import "SentryFramesTrackingIntegration.h"
36+
# import "SentryPerformanceTrackingIntegration.h"
37+
# import "SentryScreenshotIntegration.h"
38+
# import "SentryUIEventTrackingIntegration.h"
39+
# import "SentryUserFeedbackIntegration.h"
40+
# import "SentryViewHierarchyIntegration.h"
41+
# import "SentryWatchdogTerminationTrackingIntegration.h"
42+
#endif // SENTRY_HAS_UIKIT
43+
44+
#if SENTRY_HAS_METRIC_KIT
45+
# import "SentryMetricKitIntegration.h"
46+
#endif // SENTRY_HAS_METRIC_KIT
47+
2548
#if TARGET_OS_OSX
2649
# import "SentryCrashExceptionApplication.h"
2750
#endif // TARGET_OS_MAC
@@ -520,6 +543,41 @@ + (void)endSession
520543
[SentrySDKInternal.currentHub endSession];
521544
}
522545

546+
+ (NSArray<Class> *)defaultIntegrationClasses
547+
{
548+
// The order of integrations here is important.
549+
// SentryCrashIntegration needs to be initialized before SentryAutoSessionTrackingIntegration.
550+
// And SentrySessionReplayIntegration before SentryCrashIntegration.
551+
NSMutableArray<Class> *defaultIntegrations = [NSMutableArray<Class> arrayWithObjects:
552+
#if SENTRY_TARGET_REPLAY_SUPPORTED
553+
[SentrySessionReplayIntegration class],
554+
#endif // SENTRY_TARGET_REPLAY_SUPPORTED
555+
[SentryCrashIntegration class],
556+
#if SENTRY_HAS_UIKIT
557+
[SentryAppStartTrackingIntegration class], [SentryFramesTrackingIntegration class],
558+
[SentryPerformanceTrackingIntegration class], [SentryUIEventTrackingIntegration class],
559+
[SentryViewHierarchyIntegration class],
560+
[SentryWatchdogTerminationTrackingIntegration class],
561+
#endif // SENTRY_HAS_UIKIT
562+
#if SENTRY_TARGET_REPLAY_SUPPORTED
563+
[SentryScreenshotIntegration class],
564+
#endif // SENTRY_TARGET_REPLAY_SUPPORTED
565+
[SentryANRTrackingIntegration class], [SentryAutoBreadcrumbTrackingIntegration class],
566+
[SentryAutoSessionTrackingIntegration class], [SentryCoreDataTrackingIntegration class],
567+
[SentryFileIOTrackingIntegration class], [SentryNetworkTrackingIntegration class],
568+
[SentrySwiftAsyncIntegration class], nil];
569+
570+
#if TARGET_OS_IOS && SENTRY_HAS_UIKIT
571+
[defaultIntegrations addObject:[SentryUserFeedbackIntegration class]];
572+
#endif // TARGET_OS_IOS && SENTRY_HAS_UIKIT
573+
574+
#if SENTRY_HAS_METRIC_KIT
575+
[defaultIntegrations addObject:[SentryMetricKitIntegration class]];
576+
#endif // SENTRY_HAS_METRIC_KIT
577+
578+
return defaultIntegrations;
579+
}
580+
523581
/**
524582
* Install integrations and keeps ref in @c SentryHub.integrations
525583
*/
@@ -530,48 +588,24 @@ + (void)installIntegrations
530588
return;
531589
}
532590
SentryOptions *options = [SentrySDKInternal.currentHub getClient].options;
533-
NSMutableArray<NSString *> *integrationNames = [SentryOptions defaultIntegrations].mutableCopy;
534-
535-
NSArray<Class> *defaultIntegrations = SentryOptionsInternal.defaultIntegrationClasses;
536-
537-
// Since 8.22.0, we use a precompiled XCFramework for SPM, which can lead to Sentry's
538-
// definition getting duplicated in the app with a warning “SentrySDK is defined in both
539-
// ModuleA and ModuleB”. This doesn't happen when users use Sentry-Dynamic and
540-
// when compiling Sentry from source via SPM. Due to the duplication, some users didn't
541-
// see any crashes reported to Sentry cause the SentryCrashReportSink couldn't find
542-
// a hub bound to the SentrySDK, and it dropped the crash events. This problem
543-
// is fixed now by using a dictionary that links the classes with their names
544-
// so we can quickly check whether that class is in the option integrations collection.
545-
// We cannot load the class itself with NSClassFromString because doing so may load a class
546-
// that was duplicated in another module, leading to undefined behavior.
547-
NSMutableDictionary<NSString *, Class> *integrationDictionary =
548-
[[NSMutableDictionary alloc] init];
549-
550-
for (Class integrationClass in defaultIntegrations) {
551-
integrationDictionary[NSStringFromClass(integrationClass)] = integrationClass;
552-
}
553591

554-
for (NSString *integrationName in integrationNames) {
555-
Class integrationClass
556-
= integrationDictionary[integrationName] ?: NSClassFromString(integrationName);
557-
if (nil == integrationClass) {
558-
SENTRY_LOG_ERROR(@"[SentryHub doInstallIntegrations] "
559-
@"couldn't find \"%@\" -> skipping.",
560-
integrationName);
561-
continue;
562-
} else if ([SentrySDKInternal.currentHub isIntegrationInstalled:integrationClass]) {
592+
NSArray<Class> *integrationClasses = [SentrySDKInternal defaultIntegrationClasses];
593+
594+
for (Class integrationClass in integrationClasses) {
595+
if ([SentrySDKInternal.currentHub isIntegrationInstalled:integrationClass]) {
563596
SENTRY_LOG_ERROR(
564597
@"[SentryHub doInstallIntegrations] already installed \"%@\" -> skipping.",
565-
integrationName);
598+
NSStringFromClass(integrationClass));
566599
continue;
567600
}
601+
568602
id<SentryIntegrationProtocol> integrationInstance = [[integrationClass alloc] init];
569603
BOOL shouldInstall = [integrationInstance installWithOptions:options];
570-
571604
if (shouldInstall) {
572-
SENTRY_LOG_DEBUG(@"Integration installed: %@", integrationName);
573-
[SentrySDKInternal.currentHub addInstalledIntegration:integrationInstance
574-
name:integrationName];
605+
SENTRY_LOG_DEBUG(@"Integration installed: %@", NSStringFromClass(integrationClass));
606+
[SentrySDKInternal.currentHub
607+
addInstalledIntegration:integrationInstance
608+
name:NSStringFromClass(integrationClass)];
575609
}
576610
}
577611
}

Sources/Sentry/include/HybridPublic/SentryOptionsInternal.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ NS_ASSUME_NONNULL_BEGIN
66

77
@interface SentryOptionsInternal : NSObject
88

9-
@property (nonatomic, readonly, class) NSArray<Class> *defaultIntegrationClasses;
10-
119
+ (nullable SentryOptions *)initWithDict:(NSDictionary<NSString *, id> *)options
1210
didFailWithError:(NSError *_Nullable *_Nullable)error;
1311

Sources/Sentry/include/SentryOptions+Private.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,6 @@ FOUNDATION_EXPORT NSString *const kSentryDefaultEnvironment;
3232
@property (nonatomic, nullable, strong) SentryProfileOptions *profiling;
3333
#endif // SENTRY_TARGET_PROFILING_SUPPORTED
3434

35-
/**
36-
* Array of default integrations. Will be used if @c integrations is @c nil .
37-
*/
38-
+ (NSArray<NSString *> *)defaultIntegrations;
39-
4035
#if SENTRY_TARGET_REPLAY_SUPPORTED
4136

4237
- (BOOL)enableViewRendererV2;

0 commit comments

Comments
 (0)