Skip to content

Commit

Permalink
ref: centralize SentrySysctl access (#3242)
Browse files Browse the repository at this point in the history
  • Loading branch information
armcknight authored Sep 6, 2023
1 parent be2977c commit 289c0b8
Show file tree
Hide file tree
Showing 11 changed files with 38 additions and 34 deletions.
28 changes: 13 additions & 15 deletions Sources/Sentry/SentryAppStartTracker.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 =
Expand Down Expand Up @@ -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.
Expand All @@ -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;
};
Expand Down
4 changes: 0 additions & 4 deletions Sources/Sentry/SentryAppStartTrackingIntegration.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
# import <SentryCrashWrapper.h>
# import <SentryDependencyContainer.h>
# import <SentryDispatchQueueWrapper.h>
# import <SentrySysctl.h>

@interface
SentryAppStartTrackingIntegration ()
Expand All @@ -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];

Expand Down
7 changes: 3 additions & 4 deletions Sources/Sentry/SentryAppStateManager.m
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#import "SentryCrashSysCtl.h"
#import "SentryDependencyContainer.h"
#import "SentrySysctl.h"
#import <Foundation/Foundation.h>
#import <SentryAppState.h>
Expand All @@ -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;
Expand All @@ -34,15 +34,13 @@ @implementation SentryAppStateManager
- (instancetype)initWithOptions:(SentryOptions *)options
crashWrapper:(SentryCrashWrapper *)crashWrapper
fileManager:(SentryFileManager *)fileManager
sysctl:(SentrySysctl *)sysctl
dispatchQueueWrapper:(SentryDispatchQueueWrapper *)dispatchQueueWrapper
notificationCenterWrapper:(SentryNSNotificationCenterWrapper *)notificationCenterWrapper
{
if (self = [super init]) {
self.options = options;
self.crashWrapper = crashWrapper;
self.fileManager = fileManager;
self.sysctl = sysctl;
self.dispatchQueue = dispatchQueueWrapper;
self.notificationCenterWrapper = notificationCenterWrapper;
self.startCount = 0;
Expand Down Expand Up @@ -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
Expand Down
14 changes: 13 additions & 1 deletion Sources/Sentry/SentryDependencyContainer.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#import "SentryNSProcessInfoWrapper.h"
#import "SentryNSTimerFactory.h"
#import "SentryRandom.h"
#import "SentrySysctl.h"
#import "SentrySystemWrapper.h"
#import "SentryUIDeviceWrapper.h"
#import <SentryAppStateManager.h>
Expand Down Expand Up @@ -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];
}
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
@class SentryNSProcessInfoWrapper;
@class SentryNSTimerFactory;
@class SentrySwizzleWrapper;
@class SentrySysctl;
@class SentrySystemWrapper;
@class SentryThreadWrapper;
@protocol SentryRandom;
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions Sources/Sentry/include/SentryAppStartTracker.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

# import "SentryCurrentDateProvider.h"

@class SentryDispatchQueueWrapper, SentryAppStateManager, SentrySysctl;
@class SentryDispatchQueueWrapper;
@class SentryAppStateManager;

NS_ASSUME_NONNULL_BEGIN

Expand All @@ -21,7 +22,6 @@ SENTRY_NO_INIT

- (instancetype)initWithDispatchQueueWrapper:(SentryDispatchQueueWrapper *)dispatchQueueWrapper
appStateManager:(SentryAppStateManager *)appStateManager
sysctl:(SentrySysctl *)sysctl
enablePreWarmedAppStartTracing:(BOOL)enablePreWarmedAppStartTracing;

- (void)start;
Expand Down
3 changes: 1 addition & 2 deletions Sources/Sentry/include/SentryAppStateManager.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#import "SentryDefines.h"

@class SentryOptions, SentryCrashWrapper, SentryAppState, SentryFileManager, SentrySysctl,
@class SentryOptions, SentryCrashWrapper, SentryAppState, SentryFileManager,
SentryDispatchQueueWrapper, SentryNSNotificationCenterWrapper;

NS_ASSUME_NONNULL_BEGIN
Expand All @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion Tests/SentryTests/Helper/SentryAppStateManagerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
)
Expand All @@ -49,7 +50,6 @@ class SentryAppStartTrackerTests: NotificationCenterTestCase {
let sut = SentryAppStartTracker(
dispatchQueueWrapper: TestSentryDispatchQueueWrapper(),
appStateManager: appStateManager,
sysctl: sysctl,
enablePreWarmedAppStartTracing: enablePreWarmedAppStartTracing
)
return sut
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class SentryWatchdogTerminationTrackerTests: NotificationCenterTestCase {
let dispatchQueue = TestSentryDispatchQueueWrapper()

init() {
SentryDependencyContainer.sharedInstance().sysctlWrapper = sysctl
options = Options()
options.maxBreadcrumbs = 2
options.dsn = SentryWatchdogTerminationTrackerTests.dsnAsString
Expand All @@ -40,7 +41,6 @@ class SentryWatchdogTerminationTrackerTests: NotificationCenterTestCase {
options: options,
crashWrapper: crashWrapper,
fileManager: fileManager,
sysctl: sysctl,
dispatchQueueWrapper: self.dispatchQueue,
notificationCenterWrapper: SentryNSNotificationCenterWrapper()
)
Expand Down

0 comments on commit 289c0b8

Please sign in to comment.