From 289c0b80ea381b2630e2725782345968c43cc751 Mon Sep 17 00:00:00 2001 From: Andrew McKnight Date: Tue, 5 Sep 2023 21:12:25 -0400 Subject: [PATCH] ref: centralize SentrySysctl access (#3242) --- Sources/Sentry/SentryAppStartTracker.m | 28 +++++++++---------- .../SentryAppStartTrackingIntegration.m | 4 --- Sources/Sentry/SentryAppStateManager.m | 7 ++--- Sources/Sentry/SentryDependencyContainer.m | 14 +++++++++- .../HybridPublic/SentryDependencyContainer.h | 2 ++ .../Sentry/include/SentryAppStartTracker.h | 4 +-- .../Sentry/include/SentryAppStateManager.h | 3 +- .../Helper/SentryAppStateManagerTests.swift | 2 +- .../SentryAppStartTrackerTests.swift | 4 +-- ...ntryAppStartTrackingIntegrationTests.swift | 2 -- ...ntryWatchdogTerminationsTrackerTests.swift | 2 +- 11 files changed, 38 insertions(+), 34 deletions(-) diff --git a/Sources/Sentry/SentryAppStartTracker.m b/Sources/Sentry/SentryAppStartTracker.m index 00c88873524..d1eda240c80 100644 --- a/Sources/Sentry/SentryAppStartTracker.m +++ b/Sources/Sentry/SentryAppStartTracker.m @@ -32,7 +32,6 @@ @property (nonatomic, strong) SentryAppState *previousAppState; @property (nonatomic, strong) SentryDispatchQueueWrapper *dispatchQueue; @property (nonatomic, strong) SentryAppStateManager *appStateManager; -@property (nonatomic, strong) SentrySysctl *sysctl; @property (nonatomic, assign) BOOL wasInBackground; @property (nonatomic, strong) NSDate *didFinishLaunchingTimestamp; @property (nonatomic, assign) BOOL enablePreWarmedAppStartTracing; @@ -55,13 +54,11 @@ + (void)load - (instancetype)initWithDispatchQueueWrapper:(SentryDispatchQueueWrapper *)dispatchQueueWrapper appStateManager:(SentryAppStateManager *)appStateManager - sysctl:(SentrySysctl *)sysctl enablePreWarmedAppStartTracing:(BOOL)enablePreWarmedAppStartTracing { if (self = [super init]) { self.dispatchQueue = dispatchQueueWrapper; self.appStateManager = appStateManager; - self.sysctl = sysctl; self.previousAppState = [self.appStateManager loadPreviousAppState]; self.wasInBackground = NO; self.didFinishLaunchingTimestamp = @@ -170,14 +167,15 @@ - (void)buildAppStartMeasurement // https://eisel.me/startup NSTimeInterval appStartDuration = 0.0; NSDate *appStartTimestamp; + SentrySysctl *sysctl = SentryDependencyContainer.sharedInstance.sysctlWrapper; if (isPreWarmed) { appStartDuration = [[SentryDependencyContainer.sharedInstance.dateProvider date] - timeIntervalSinceDate:self.sysctl.moduleInitializationTimestamp]; - appStartTimestamp = self.sysctl.moduleInitializationTimestamp; + timeIntervalSinceDate:sysctl.moduleInitializationTimestamp]; + appStartTimestamp = sysctl.moduleInitializationTimestamp; } else { appStartDuration = [[SentryDependencyContainer.sharedInstance.dateProvider date] - timeIntervalSinceDate:self.sysctl.processStartTimestamp]; - appStartTimestamp = self.sysctl.processStartTimestamp; + timeIntervalSinceDate:sysctl.processStartTimestamp]; + appStartTimestamp = sysctl.processStartTimestamp; } // Safety check to not report app starts that are completely off. @@ -199,14 +197,14 @@ - (void)buildAppStartMeasurement appStartDuration = 0; } - SentryAppStartMeasurement *appStartMeasurement = [[SentryAppStartMeasurement alloc] - initWithType:appStartType - isPreWarmed:isPreWarmed - appStartTimestamp:appStartTimestamp - duration:appStartDuration - runtimeInitTimestamp:runtimeInit - moduleInitializationTimestamp:self.sysctl.moduleInitializationTimestamp - didFinishLaunchingTimestamp:self.didFinishLaunchingTimestamp]; + SentryAppStartMeasurement *appStartMeasurement = + [[SentryAppStartMeasurement alloc] initWithType:appStartType + isPreWarmed:isPreWarmed + appStartTimestamp:appStartTimestamp + duration:appStartDuration + runtimeInitTimestamp:runtimeInit + moduleInitializationTimestamp:sysctl.moduleInitializationTimestamp + didFinishLaunchingTimestamp:self.didFinishLaunchingTimestamp]; SentrySDK.appStartMeasurement = appStartMeasurement; }; diff --git a/Sources/Sentry/SentryAppStartTrackingIntegration.m b/Sources/Sentry/SentryAppStartTrackingIntegration.m index 0810f621e46..4daec01180a 100644 --- a/Sources/Sentry/SentryAppStartTrackingIntegration.m +++ b/Sources/Sentry/SentryAppStartTrackingIntegration.m @@ -10,7 +10,6 @@ # import # import # import -# import @interface SentryAppStartTrackingIntegration () @@ -28,15 +27,12 @@ - (BOOL)installWithOptions:(SentryOptions *)options return NO; } - SentrySysctl *sysctl = [[SentrySysctl alloc] init]; - SentryAppStateManager *appStateManager = [SentryDependencyContainer sharedInstance].appStateManager; self.tracker = [[SentryAppStartTracker alloc] initWithDispatchQueueWrapper:[[SentryDispatchQueueWrapper alloc] init] appStateManager:appStateManager - sysctl:sysctl enablePreWarmedAppStartTracing:options.enablePreWarmedAppStartTracing]; [self.tracker start]; diff --git a/Sources/Sentry/SentryAppStateManager.m b/Sources/Sentry/SentryAppStateManager.m index c3ad54f6f69..eae323a1177 100644 --- a/Sources/Sentry/SentryAppStateManager.m +++ b/Sources/Sentry/SentryAppStateManager.m @@ -1,4 +1,5 @@ #import "SentryCrashSysCtl.h" +#import "SentryDependencyContainer.h" #import "SentrySysctl.h" #import #import @@ -22,7 +23,6 @@ @property (nonatomic, strong) SentryOptions *options; @property (nonatomic, strong) SentryCrashWrapper *crashWrapper; @property (nonatomic, strong) SentryFileManager *fileManager; -@property (nonatomic, strong) SentrySysctl *sysctl; @property (nonatomic, strong) SentryDispatchQueueWrapper *dispatchQueue; @property (nonatomic, strong) SentryNSNotificationCenterWrapper *notificationCenterWrapper; @property (nonatomic) NSInteger startCount; @@ -34,7 +34,6 @@ @implementation SentryAppStateManager - (instancetype)initWithOptions:(SentryOptions *)options crashWrapper:(SentryCrashWrapper *)crashWrapper fileManager:(SentryFileManager *)fileManager - sysctl:(SentrySysctl *)sysctl dispatchQueueWrapper:(SentryDispatchQueueWrapper *)dispatchQueueWrapper notificationCenterWrapper:(SentryNSNotificationCenterWrapper *)notificationCenterWrapper { @@ -42,7 +41,6 @@ - (instancetype)initWithOptions:(SentryOptions *)options self.options = options; self.crashWrapper = crashWrapper; self.fileManager = fileManager; - self.sysctl = sysctl; self.dispatchQueue = dispatchQueueWrapper; self.notificationCenterWrapper = notificationCenterWrapper; self.startCount = 0; @@ -187,7 +185,8 @@ - (SentryAppState *)buildCurrentAppState osVersion:UIDevice.currentDevice.systemVersion vendorId:vendorId isDebugging:isDebugging - systemBootTimestamp:self.sysctl.systemBootTimestamp]; + systemBootTimestamp:SentryDependencyContainer.sharedInstance + .sysctlWrapper.systemBootTimestamp]; } - (SentryAppState *)loadPreviousAppState diff --git a/Sources/Sentry/SentryDependencyContainer.m b/Sources/Sentry/SentryDependencyContainer.m index 61796908490..5f566ce23da 100644 --- a/Sources/Sentry/SentryDependencyContainer.m +++ b/Sources/Sentry/SentryDependencyContainer.m @@ -8,6 +8,7 @@ #import "SentryNSProcessInfoWrapper.h" #import "SentryNSTimerFactory.h" #import "SentryRandom.h" +#import "SentrySysctl.h" #import "SentrySystemWrapper.h" #import "SentryUIDeviceWrapper.h" #import @@ -82,7 +83,6 @@ - (SentryAppStateManager *)appStateManager [[SentryAppStateManager alloc] initWithOptions:options crashWrapper:self.crashWrapper fileManager:self.fileManager - sysctl:[[SentrySysctl alloc] init] dispatchQueueWrapper:self.dispatchQueueWrapper notificationCenterWrapper:self.notificationCenterWrapper]; } @@ -102,6 +102,18 @@ - (SentryCrashWrapper *)crashWrapper return _crashWrapper; } +- (SentrySysctl *)sysctlWrapper +{ + if (_sysctlWrapper == nil) { + @synchronized(sentryDependencyContainerLock) { + if (_sysctlWrapper == nil) { + _sysctlWrapper = [[SentrySysctl alloc] init]; + } + } + } + return _sysctlWrapper; +} + - (SentryExtraContextProvider *)extraContextProvider { if (_extraContextProvider == nil) { diff --git a/Sources/Sentry/include/HybridPublic/SentryDependencyContainer.h b/Sources/Sentry/include/HybridPublic/SentryDependencyContainer.h index ca8ed944015..213a898d323 100644 --- a/Sources/Sentry/include/HybridPublic/SentryDependencyContainer.h +++ b/Sources/Sentry/include/HybridPublic/SentryDependencyContainer.h @@ -14,6 +14,7 @@ @class SentryNSProcessInfoWrapper; @class SentryNSTimerFactory; @class SentrySwizzleWrapper; +@class SentrySysctl; @class SentrySystemWrapper; @class SentryThreadWrapper; @protocol SentryRandom; @@ -62,6 +63,7 @@ SENTRY_NO_INIT @property (nonatomic, strong) SentryCurrentDateProvider *dateProvider; @property (nonatomic, strong) SentryBinaryImageCache *binaryImageCache; @property (nonatomic, strong) SentryExtraContextProvider *extraContextProvider; +@property (nonatomic, strong) SentrySysctl *sysctlWrapper; #if SENTRY_HAS_UIKIT @property (nonatomic, strong) SentryFramesTracker *framesTracker; diff --git a/Sources/Sentry/include/SentryAppStartTracker.h b/Sources/Sentry/include/SentryAppStartTracker.h index 99e388d7417..b853873167c 100644 --- a/Sources/Sentry/include/SentryAppStartTracker.h +++ b/Sources/Sentry/include/SentryAppStartTracker.h @@ -4,7 +4,8 @@ # import "SentryCurrentDateProvider.h" -@class SentryDispatchQueueWrapper, SentryAppStateManager, SentrySysctl; +@class SentryDispatchQueueWrapper; +@class SentryAppStateManager; NS_ASSUME_NONNULL_BEGIN @@ -21,7 +22,6 @@ SENTRY_NO_INIT - (instancetype)initWithDispatchQueueWrapper:(SentryDispatchQueueWrapper *)dispatchQueueWrapper appStateManager:(SentryAppStateManager *)appStateManager - sysctl:(SentrySysctl *)sysctl enablePreWarmedAppStartTracing:(BOOL)enablePreWarmedAppStartTracing; - (void)start; diff --git a/Sources/Sentry/include/SentryAppStateManager.h b/Sources/Sentry/include/SentryAppStateManager.h index 6db56106b62..21963905094 100644 --- a/Sources/Sentry/include/SentryAppStateManager.h +++ b/Sources/Sentry/include/SentryAppStateManager.h @@ -1,6 +1,6 @@ #import "SentryDefines.h" -@class SentryOptions, SentryCrashWrapper, SentryAppState, SentryFileManager, SentrySysctl, +@class SentryOptions, SentryCrashWrapper, SentryAppState, SentryFileManager, SentryDispatchQueueWrapper, SentryNSNotificationCenterWrapper; NS_ASSUME_NONNULL_BEGIN @@ -13,7 +13,6 @@ SENTRY_NO_INIT - (instancetype)initWithOptions:(SentryOptions *)options crashWrapper:(SentryCrashWrapper *)crashWrapper fileManager:(SentryFileManager *)fileManager - sysctl:(SentrySysctl *)sysctl dispatchQueueWrapper:(SentryDispatchQueueWrapper *)dispatchQueueWrapper notificationCenterWrapper:(SentryNSNotificationCenterWrapper *)notificationCenterWrapper; diff --git a/Tests/SentryTests/Helper/SentryAppStateManagerTests.swift b/Tests/SentryTests/Helper/SentryAppStateManagerTests.swift index af8104cf928..88296adeabd 100644 --- a/Tests/SentryTests/Helper/SentryAppStateManagerTests.swift +++ b/Tests/SentryTests/Helper/SentryAppStateManagerTests.swift @@ -22,11 +22,11 @@ class SentryAppStateManagerTests: XCTestCase { } func getSut() -> SentryAppStateManager { + SentryDependencyContainer.sharedInstance().sysctlWrapper = TestSysctl() return SentryAppStateManager( options: options, crashWrapper: TestSentryCrashWrapper.sharedInstance(), fileManager: fileManager, - sysctl: TestSysctl(), dispatchQueueWrapper: TestSentryDispatchQueueWrapper(), notificationCenterWrapper: notificationCenterWrapper ) diff --git a/Tests/SentryTests/Integrations/Performance/AppStartTracking/SentryAppStartTrackerTests.swift b/Tests/SentryTests/Integrations/Performance/AppStartTracking/SentryAppStartTrackerTests.swift index c5b90645d60..b3f78aa6711 100644 --- a/Tests/SentryTests/Integrations/Performance/AppStartTracking/SentryAppStartTrackerTests.swift +++ b/Tests/SentryTests/Integrations/Performance/AppStartTracking/SentryAppStartTrackerTests.swift @@ -30,12 +30,13 @@ class SentryAppStartTrackerTests: NotificationCenterTestCase { SentryDependencyContainer.sharedInstance().dateProvider = currentDate fileManager = try! SentryFileManager(options: options, dispatchQueueWrapper: dispatchQueue) + + SentryDependencyContainer.sharedInstance().sysctlWrapper = sysctl appStateManager = SentryAppStateManager( options: options, crashWrapper: crashWrapper, fileManager: fileManager, - sysctl: sysctl, dispatchQueueWrapper: dispatchQueue, notificationCenterWrapper: SentryNSNotificationCenterWrapper() ) @@ -49,7 +50,6 @@ class SentryAppStartTrackerTests: NotificationCenterTestCase { let sut = SentryAppStartTracker( dispatchQueueWrapper: TestSentryDispatchQueueWrapper(), appStateManager: appStateManager, - sysctl: sysctl, enablePreWarmedAppStartTracing: enablePreWarmedAppStartTracing ) return sut diff --git a/Tests/SentryTests/Integrations/Performance/AppStartTracking/SentryAppStartTrackingIntegrationTests.swift b/Tests/SentryTests/Integrations/Performance/AppStartTracking/SentryAppStartTrackingIntegrationTests.swift index b4b2aff8048..173f02b7a5a 100644 --- a/Tests/SentryTests/Integrations/Performance/AppStartTracking/SentryAppStartTrackingIntegrationTests.swift +++ b/Tests/SentryTests/Integrations/Performance/AppStartTracking/SentryAppStartTrackingIntegrationTests.swift @@ -114,8 +114,6 @@ class SentryAppStartTrackingIntegrationTests: NotificationCenterTestCase { XCTAssertEqual(appStateManager, SentryDependencyContainer.sharedInstance().appStateManager) - _ = try XCTUnwrap(Dynamic(tracker).sysctl.asObject as? SentrySysctl, "Tracker does not have a Sysctl") - XCTAssertTrue(tracker.isRunning, "AppStartTracking should be running") } diff --git a/Tests/SentryTests/Integrations/WatchdogTerminations/SentryWatchdogTerminationsTrackerTests.swift b/Tests/SentryTests/Integrations/WatchdogTerminations/SentryWatchdogTerminationsTrackerTests.swift index b23c38b3ef5..a53d539cc28 100644 --- a/Tests/SentryTests/Integrations/WatchdogTerminations/SentryWatchdogTerminationsTrackerTests.swift +++ b/Tests/SentryTests/Integrations/WatchdogTerminations/SentryWatchdogTerminationsTrackerTests.swift @@ -18,6 +18,7 @@ class SentryWatchdogTerminationTrackerTests: NotificationCenterTestCase { let dispatchQueue = TestSentryDispatchQueueWrapper() init() { + SentryDependencyContainer.sharedInstance().sysctlWrapper = sysctl options = Options() options.maxBreadcrumbs = 2 options.dsn = SentryWatchdogTerminationTrackerTests.dsnAsString @@ -40,7 +41,6 @@ class SentryWatchdogTerminationTrackerTests: NotificationCenterTestCase { options: options, crashWrapper: crashWrapper, fileManager: fileManager, - sysctl: sysctl, dispatchQueueWrapper: self.dispatchQueue, notificationCenterWrapper: SentryNSNotificationCenterWrapper() )