Skip to content

New runloop observer #5632

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

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 4 additions & 0 deletions Sentry.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1098,6 +1098,7 @@
FAB359982E05D7E90083D5E3 /* SentryEventSwiftHelper.h in Headers */ = {isa = PBXBuildFile; fileRef = FAB359972E05D7E90083D5E3 /* SentryEventSwiftHelper.h */; };
FAB3599A2E05D8080083D5E3 /* SentryEventSwiftHelper.m in Sources */ = {isa = PBXBuildFile; fileRef = FAB359992E05D8080083D5E3 /* SentryEventSwiftHelper.m */; };
FAC62B652E15A4100003909D /* SentrySDKThreadTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = FAC62B642E15A40C0003909D /* SentrySDKThreadTests.swift */; };
FAE2DABC2E1F55C500262307 /* HangTracker.swift in Sources */ = {isa = PBXBuildFile; fileRef = FAE2DABB2E1F55C100262307 /* HangTracker.swift */; };
FAEC270E2DF3526000878871 /* SentryUserFeedback.swift in Sources */ = {isa = PBXBuildFile; fileRef = FAEC270D2DF3526000878871 /* SentryUserFeedback.swift */; };
FAEC273D2DF3933A00878871 /* NSData+Unzip.m in Sources */ = {isa = PBXBuildFile; fileRef = FAEC273C2DF3933200878871 /* NSData+Unzip.m */; };
/* End PBXBuildFile section */
Expand Down Expand Up @@ -2376,6 +2377,7 @@
FAB359972E05D7E90083D5E3 /* SentryEventSwiftHelper.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = SentryEventSwiftHelper.h; path = include/SentryEventSwiftHelper.h; sourceTree = "<group>"; };
FAB359992E05D8080083D5E3 /* SentryEventSwiftHelper.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SentryEventSwiftHelper.m; sourceTree = "<group>"; };
FAC62B642E15A40C0003909D /* SentrySDKThreadTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentrySDKThreadTests.swift; sourceTree = "<group>"; };
FAE2DABB2E1F55C100262307 /* HangTracker.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HangTracker.swift; sourceTree = "<group>"; };
FAEC270D2DF3526000878871 /* SentryUserFeedback.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryUserFeedback.swift; sourceTree = "<group>"; };
FAEC273C2DF3933200878871 /* NSData+Unzip.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "NSData+Unzip.m"; sourceTree = "<group>"; };
FAEC273E2DF393E000878871 /* NSData+Unzip.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "NSData+Unzip.h"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -4182,6 +4184,7 @@
D800942328F82E8D005D3943 /* Swift */ = {
isa = PBXGroup;
children = (
FAE2DABB2E1F55C100262307 /* HangTracker.swift */,
FA67DCF32DDBD4EA00896B02 /* Core */,
D8CAC02D2BA0663E00E38F34 /* Integrations */,
621D9F2D2B9B030E003D94DE /* Helper */,
Expand Down Expand Up @@ -5566,6 +5569,7 @@
63FE70FD20DA4C1000CDBAE8 /* SentryCrashCachedData.c in Sources */,
A8F17B2E2901765900990B25 /* SentryRequest.m in Sources */,
7BE1E33424F7E3CB009D3AD0 /* SentryMigrateSessionInit.m in Sources */,
FAE2DABC2E1F55C500262307 /* HangTracker.swift in Sources */,
D80299502BA83A88000F0081 /* SentryPixelBuffer.swift in Sources */,
15E0A8F22411A45A00F044E3 /* SentrySession.m in Sources */,
844EDCE62947DC3100C86F34 /* SentryNSTimerFactory.m in Sources */,
Expand Down
22 changes: 22 additions & 0 deletions Sources/Sentry/SentryDependencyContainer.m
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,18 @@

@end

@interface SentryDebugImageProvider () <DebugImageCache>

@end

@interface SentryThreadInspector () <ThreadInspector>

@end

@interface SentryCrashWrapper () <CrashWrapper>

@end

@implementation SentryDependencyContainer

static SentryDependencyContainer *instance;
Expand Down Expand Up @@ -250,6 +262,16 @@
[[SentryCrash alloc] initWithBasePath:SentrySDK.options.cacheDirectoryPath]);
}

- (HangTrackerObjcBridge *)hangTracker SENTRY_THREAD_SANITIZER_DOUBLE_CHECKED_LOCK
{
SENTRY_LAZY_INIT(_hangTracker,
[[HangTrackerObjcBridge alloc] initWithDateProvider:self.dateProvider
threadInspector:self.threadInspector
debugImageCache:self.debugImageProvider
fileManager:self.fileManager
crashWrapper:self.crashWrapper]);

Check warning on line 272 in Sources/Sentry/SentryDependencyContainer.m

View check run for this annotation

Codecov / codecov/patch

Sources/Sentry/SentryDependencyContainer.m#L266-L272

Added lines #L266 - L272 were not covered by tests
}

- (id<SentryANRTracker>)getANRTracker:(NSTimeInterval)timeout
SENTRY_THREAD_SANITIZER_DOUBLE_CHECKED_LOCK
{
Expand Down
17 changes: 17 additions & 0 deletions Sources/Sentry/SentryDependencyContainerSwiftHelper.m
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#import "SentryDependencyContainerSwiftHelper.h"
#import "SentryDependencyContainer.h"
#import "SentrySDK+Private.h"
#import "SentryScope+Private.h"
#import "SentryScope.h"
#import "SentrySwift.h"
#import "SentryUIApplication.h"

Expand All @@ -19,4 +22,18 @@
[SentryDependencyContainer.sharedInstance.dispatchQueueWrapper dispatchSyncOnMainQueue:block];
}

+ (void)applyScopeTo:(SentryEvent *)event
{
SentryScope *scope = [SentrySDK currentHub].scope;
SentryOptions *options = SentrySDK.options;
if (scope != nil && options != nil) {
[scope applyToEvent:event maxBreadcrumb:options.maxBreadcrumbs];

Check warning on line 30 in Sources/Sentry/SentryDependencyContainerSwiftHelper.m

View check run for this annotation

Codecov / codecov/patch

Sources/Sentry/SentryDependencyContainerSwiftHelper.m#L26-L30

Added lines #L26 - L30 were not covered by tests
}
}

+ (void)captureFatalAppHangEvent:(SentryEvent *)event
{
[SentrySDK captureFatalAppHangEvent:event];

Check failure on line 36 in Sources/Sentry/SentryDependencyContainerSwiftHelper.m

View workflow job for this annotation

GitHub Actions / Check no UIKit linkage (DebugWithoutUIKit)

no known class method for selector 'captureFatalAppHangEvent:'

Check failure on line 36 in Sources/Sentry/SentryDependencyContainerSwiftHelper.m

View workflow job for this annotation

GitHub Actions / Check no UIKit linkage (ReleaseWithoutUIKit)

no known class method for selector 'captureFatalAppHangEvent:'

Check failure on line 36 in Sources/Sentry/SentryDependencyContainerSwiftHelper.m

View workflow job for this annotation

GitHub Actions / Xcode Analyze

no known class method for selector 'captureFatalAppHangEvent:'

Check failure on line 36 in Sources/Sentry/SentryDependencyContainerSwiftHelper.m

View workflow job for this annotation

GitHub Actions / Unit macOS - Xcode 16.2 - OS latest Sentry

no known class method for selector 'captureFatalAppHangEvent:'

Check failure on line 36 in Sources/Sentry/SentryDependencyContainerSwiftHelper.m

View workflow job for this annotation

GitHub Actions / Unit macOS - Xcode 15.4 - OS latest Sentry

no known class method for selector 'captureFatalAppHangEvent:'

Check warning on line 36 in Sources/Sentry/SentryDependencyContainerSwiftHelper.m

View check run for this annotation

Codecov / codecov/patch

Sources/Sentry/SentryDependencyContainerSwiftHelper.m#L35-L36

Added lines #L35 - L36 were not covered by tests
}

@end
28 changes: 20 additions & 8 deletions Sources/Sentry/SentrySDK.m
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,10 @@
[SentryCrashWrapper.sharedInstance startBinaryImageCache];
[SentryDependencyContainer.sharedInstance.binaryImageCache start];

if (options.experimental.enableRunLoopObserverAppHangs) {
[SentryDependencyContainer.sharedInstance.hangTracker start];

Check warning on line 291 in Sources/Sentry/SentrySDK.m

View check run for this annotation

Codecov / codecov/patch

Sources/Sentry/SentrySDK.m#L290-L291

Added lines #L290 - L291 were not covered by tests
}

[SentrySDK installIntegrations];

#if SENTRY_TARGET_PROFILING_SUPPORTED
Expand Down Expand Up @@ -602,20 +606,28 @@

+ (void)pauseAppHangTracking
{
SentryANRTrackingIntegration *anrTrackingIntegration
= (SentryANRTrackingIntegration *)[SentrySDK.currentHub
getInstalledIntegration:[SentryANRTrackingIntegration class]];
if (currentHub.client.options.experimental.enableRunLoopObserverAppHangs) {
[SentryDependencyContainer.sharedInstance.hangTracker stop];
} else {
SentryANRTrackingIntegration *anrTrackingIntegration
= (SentryANRTrackingIntegration *)[SentrySDK.currentHub
getInstalledIntegration:[SentryANRTrackingIntegration class]];

Check warning on line 614 in Sources/Sentry/SentrySDK.m

View check run for this annotation

Codecov / codecov/patch

Sources/Sentry/SentrySDK.m#L609-L614

Added lines #L609 - L614 were not covered by tests

[anrTrackingIntegration pauseAppHangTracking];
[anrTrackingIntegration pauseAppHangTracking];

Check warning on line 616 in Sources/Sentry/SentrySDK.m

View check run for this annotation

Codecov / codecov/patch

Sources/Sentry/SentrySDK.m#L616

Added line #L616 was not covered by tests
}
}

+ (void)resumeAppHangTracking
{
SentryANRTrackingIntegration *anrTrackingIntegration
= (SentryANRTrackingIntegration *)[SentrySDK.currentHub
getInstalledIntegration:[SentryANRTrackingIntegration class]];
if (currentHub.client.options.experimental.enableRunLoopObserverAppHangs) {
[SentryDependencyContainer.sharedInstance.hangTracker start];
} else {
SentryANRTrackingIntegration *anrTrackingIntegration
= (SentryANRTrackingIntegration *)[SentrySDK.currentHub
getInstalledIntegration:[SentryANRTrackingIntegration class]];

Check warning on line 627 in Sources/Sentry/SentrySDK.m

View check run for this annotation

Codecov / codecov/patch

Sources/Sentry/SentrySDK.m#L622-L627

Added lines #L622 - L627 were not covered by tests

[anrTrackingIntegration resumeAppHangTracking];
[anrTrackingIntegration resumeAppHangTracking];

Check warning on line 629 in Sources/Sentry/SentrySDK.m

View check run for this annotation

Codecov / codecov/patch

Sources/Sentry/SentrySDK.m#L629

Added line #L629 was not covered by tests
}
}

+ (void)flush:(NSTimeInterval)timeout
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
@class SentryScopePersistentStore;
@class SentryOptions;
@class SentrySessionTracker;
@class HangTrackerObjcBridge;
@class SentryGlobalEventProcessor;

@protocol SentryANRTracker;
Expand Down Expand Up @@ -122,6 +123,8 @@ SENTRY_NO_INIT
@property (nonatomic, strong) id<SentryDispatchQueueProviderProtocol> dispatchQueueProvider;
@property (nonatomic, strong) SentryNSTimerFactory *timerFactory;

@property (nonatomic, strong) HangTrackerObjcBridge *hangTracker;

@property (nonatomic, strong) SentrySwizzleWrapper *swizzleWrapper;
#if SENTRY_UIKIT_AVAILABLE
@property (nonatomic, strong) SentryFramesTracker *framesTracker;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
# import <UIKit/UIKit.h>
#endif // SENTRY_HAS_UIKIT

@class SentryScope;
@class SentryOptions;

NS_ASSUME_NONNULL_BEGIN

// Some Swift code needs to access SentryDependencyContainer. To
Expand All @@ -21,6 +24,10 @@ NS_ASSUME_NONNULL_BEGIN

+ (void)dispatchSyncOnMainQueue:(void (^)(void))block;

+ (void)applyScopeTo:(SentryEvent *)event;

+ (void)captureFatalAppHangEvent:(SentryEvent *)event;

@end

NS_ASSUME_NONNULL_END
Loading
Loading