Skip to content

Commit

Permalink
integration
Browse files Browse the repository at this point in the history
  • Loading branch information
brustolin committed Feb 20, 2024
1 parent b11fc9a commit d6d8ce0
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 70 deletions.
50 changes: 32 additions & 18 deletions Sources/Sentry/SentryBaseIntegration.m
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#import <Foundation/Foundation.h>
#import <SentryDependencyContainer.h>
#import <SentryOptions+Private.h>
#import "SentryReplaySettings.h"

NS_ASSUME_NONNULL_BEGIN

Expand Down Expand Up @@ -32,114 +33,127 @@ - (void)logWithReason:(NSString *)reason
- (BOOL)shouldBeEnabledWithOptions:(SentryOptions *)options
{
SentryIntegrationOption integrationOptions = [self integrationOptions];

if (integrationOptions & kIntegrationOptionNone) {
return YES;
}

if ((integrationOptions & kIntegrationOptionEnableAutoSessionTracking)
&& !options.enableAutoSessionTracking) {
[self logWithOptionName:@"enableAutoSessionTracking"];
return NO;
}

if ((integrationOptions & kIntegrationOptionEnableWatchdogTerminationTracking)
&& !options.enableWatchdogTerminationTracking) {
[self logWithOptionName:@"enableWatchdogTerminationTracking"];
return NO;
}

if ((integrationOptions & kIntegrationOptionEnableAutoPerformanceTracing)
&& !options.enableAutoPerformanceTracing) {
[self logWithOptionName:@"enableAutoPerformanceTracing"];
return NO;
}

#if SENTRY_HAS_UIKIT
if ((integrationOptions & kIntegrationOptionEnableUIViewControllerTracing)
&& !options.enableUIViewControllerTracing) {
[self logWithOptionName:@"enableUIViewControllerTracing"];
return NO;
}

# if SENTRY_HAS_UIKIT
if ((integrationOptions & kIntegrationOptionAttachScreenshot) && !options.attachScreenshot) {
[self logWithOptionName:@"attachScreenshot"];
return NO;
}
# endif // SENTRY_HAS_UIKIT

if ((integrationOptions & kIntegrationOptionEnableUserInteractionTracing)
&& !options.enableUserInteractionTracing) {
[self logWithOptionName:@"enableUserInteractionTracing"];
return NO;
}
#endif

if (integrationOptions & kIntegrationOptionEnableAppHangTracking) {
if (!options.enableAppHangTracking) {
[self logWithOptionName:@"enableAppHangTracking"];
return NO;
}

if (options.appHangTimeoutInterval == 0) {
[self logWithReason:@"because appHangTimeoutInterval is 0"];
return NO;
}
}

if ((integrationOptions & kIntegrationOptionEnableNetworkTracking)
&& !options.enableNetworkTracking) {
[self logWithOptionName:@"enableNetworkTracking"];
return NO;
}

if ((integrationOptions & kIntegrationOptionEnableFileIOTracing)
&& !options.enableFileIOTracing) {
[self logWithOptionName:@"enableFileIOTracing"];
return NO;
}

if ((integrationOptions & kIntegrationOptionEnableNetworkBreadcrumbs)
&& !options.enableNetworkBreadcrumbs) {
[self logWithOptionName:@"enableNetworkBreadcrumbs"];
return NO;
}

if ((integrationOptions & kIntegrationOptionEnableCoreDataTracing)
&& !options.enableCoreDataTracing) {
[self logWithOptionName:@"enableCoreDataTracing"];
return NO;
}

if ((integrationOptions & kIntegrationOptionEnableSwizzling) && !options.enableSwizzling) {
[self logWithOptionName:@"enableSwizzling"];
return NO;
}

if ((integrationOptions & kIntegrationOptionEnableAutoBreadcrumbTracking)
&& !options.enableAutoBreadcrumbTracking) {
[self logWithOptionName:@"enableAutoBreadcrumbTracking"];
return NO;
}

if ((integrationOptions & kIntegrationOptionIsTracingEnabled) && !options.isTracingEnabled) {
[self logWithOptionName:@"isTracingEnabled"];
return NO;
}

if ((integrationOptions & kIntegrationOptionDebuggerNotAttached) &&
[SentryDependencyContainer.sharedInstance.crashWrapper isBeingTraced]) {
[self logWithReason:@"because the debugger is attached"];
return NO;
}

#if SENTRY_HAS_UIKIT
if ((integrationOptions & kIntegrationOptionAttachViewHierarchy)
&& !options.attachViewHierarchy) {
[self logWithOptionName:@"attachViewHierarchy"];
return NO;
}

if (integrationOptions & kIntegrationOptionEnableReplay) {
if (@available(iOS 16.0, *)) {
if (options.sessionReplaySettings.replaysOnErrorSampleRate == 0
&& options.sessionReplaySettings.replaysSessionSampleRate == 0 ){
[self logWithOptionName:@"sessionReplaySettings"];
return NO;
}
} else {
[self logWithReason:@"Session replay requires iOS 16 or above"];
return NO;
}
}
#endif

if ((integrationOptions & kIntegrationOptionEnableCrashHandler)
Expand Down
2 changes: 2 additions & 0 deletions Sources/Sentry/SentryOptions.m
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
# import "SentryViewHierarchyIntegration.h"
# import "SentryWatchdogTerminationTrackingIntegration.h"
# import "SentryReplaySettings+Private.h"
# import "SentrySessionReplayIntegration.h"
#endif // SENTRY_HAS_UIKIT

#if SENTRY_HAS_METRIC_KIT
Expand Down Expand Up @@ -66,6 +67,7 @@ - (void)setMeasurement:(SentryMeasurementValue *)measurement
NSStringFromClass([SentryUIEventTrackingIntegration class]),
NSStringFromClass([SentryViewHierarchyIntegration class]),
NSStringFromClass([SentryWatchdogTerminationTrackingIntegration class]),
NSStringFromClass([SentrySessionReplayIntegration class]),
#endif // SENTRY_HAS_UIKIT
NSStringFromClass([SentryANRTrackingIntegration class]),
NSStringFromClass([SentryAutoBreadcrumbTrackingIntegration class]),
Expand Down
42 changes: 2 additions & 40 deletions Sources/Sentry/SentrySessionReplay.m
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
#import "SentrySessionReplay.h"
#import "SentryVideoReplay.h"
#import "SentryImagesReplay.h"
#import "SentryViewPhotographer.h"
#import "SentryOndemandReplay.h"
#import "SentryAttachment+Private.h"
#import "SentryLog.h"
#import "SentryTouchesTracker.h"
//#define use_video 1
#define use_ondemand 1
#import "SentryReplaySettings+Private.h"

@implementation SentrySessionReplay {
UIView * _rootView;
Expand All @@ -18,16 +14,9 @@ @implementation SentrySessionReplay {
NSURL * _urlToCache;
NSDate * _sessionStart;
SentryReplaySettings * _settings;
#if use_video
SentryVideoReplay * replayMaker;
#elif use_ondemand
SentryOnDemandReplay * _replayMaker;
#else
SentryImagesReplay * replayMaker;
#endif

NSMutableArray<UIImage *>* imageCollection;
SentryTouchesTracker * _touchesTracker;
}

- (instancetype)initWithSettings:(SentryReplaySettings *)replaySettings {
Expand Down Expand Up @@ -56,8 +45,6 @@ - (void)start:(UIView *)rootView fullSession:(BOOL)full {
_lastScreenShot = [[NSDate alloc] init];
_videoSegmentStart = nil;
_sessionStart = _lastScreenShot;
_touchesTracker = [[SentryTouchesTracker alloc] init];
[_touchesTracker start];

NSURL * docs = [[NSFileManager.defaultManager URLsForDirectory:NSCachesDirectory inDomains:NSUserDomainMask].firstObject URLByAppendingPathComponent:@"io.sentry"];

Expand All @@ -69,15 +56,9 @@ - (void)start:(UIView *)rootView fullSession:(BOOL)full {
}

_replayMaker =
#if use_video
[[SentryVideoReplay alloc] initWithOutputPath:[urlToCache URLByAppendingPathComponent:@"sr.mp4"].path frameSize:rootView.frame.size framesPerSec:1];
#elif use_ondemand
[[SentryOnDemandReplay alloc] initWithOutputPath:_urlToCache.path];
_replayMaker.bitRate = _settings.replayBitRate;
_replayMaker.cacheMaxSize = full ? NSUIntegerMax : 32;
#else
[[SentryImagesReplay alloc] initWithOutputPath:urlToCache.path];
#endif
imageCollection = [NSMutableArray array];

NSLog(@"Recording session to %@",_urlToCache);
Expand All @@ -87,19 +68,11 @@ - (void)start:(UIView *)rootView fullSession:(BOOL)full {
- (void)stop {
[_displayLink invalidate];
_displayLink = nil;
#ifdef use_video
[videoReplay finalizeVideoWithCompletion:^(BOOL success, NSError * _Nonnull error) {
if (!success) {
NSLog(@"%@", error);
}
}];
#endif
}

- (NSArray<SentryAttachment *> *)processAttachments:(NSArray<SentryAttachment *> *)attachments
forEvent:(nonnull SentryEvent *)event
{
#if use_ondemand
if (event.error == nil && (event.exceptions == nil || event.exceptions.count == 0)) {
return attachments;
}
Expand Down Expand Up @@ -128,15 +101,10 @@ - (void)stop {
[result addObject:attachment];

return result;
#else
return attachments;
#endif
}

- (void)sendReplayForEvent:(SentryEvent *)event {
#if use_ondemand

#endif
}

- (void)newFrame:(CADisplayLink *)sender {
Expand Down Expand Up @@ -199,13 +167,7 @@ - (void)takeScreenshot {

dispatch_queue_t backgroundQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(backgroundQueue, ^{
#if use_video
[self->replayMaker addFrame:screenshot withCompletion:^(BOOL success, NSError * _Nonnull error) {

}];
#else
[self->_replayMaker addFrame:screenshot];
#endif
[self->_replayMaker addFrame:screenshot];
});
}

Expand Down
30 changes: 18 additions & 12 deletions Sources/Sentry/SentrySessionReplayIntegration.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#import "SentrySDK+Private.h"
#import "SentryReplaySettings.h"
#import "SentryRandom.h"
#import "SentryOptions.h"


@implementation SentrySessionReplayIntegration {
SentrySessionReplay * sessionReplay;
Expand All @@ -19,20 +21,24 @@ - (BOOL)installWithOptions:(nonnull SentryOptions *)options
return NO;
}

if (options.replaySettings.replaysSessionSampleRate == 0 && options.replaySettings.replaysOnErrorSampleRate == 0) {
if (@available(iOS 16.0, *)) {
if (options.sessionReplaySettings.replaysSessionSampleRate == 0 && options.sessionReplaySettings.replaysOnErrorSampleRate == 0) {
return NO;
}

sessionReplay = [[SentrySessionReplay alloc] initWithSettings:options.sessionReplaySettings];

[sessionReplay start:SentryDependencyContainer.sharedInstance.application.windows.firstObject
fullSession:[self shouldReplayFullSession:options.sessionReplaySettings.replaysSessionSampleRate]];

SentryClient *client = [SentrySDK.currentHub getClient];
[client addAttachmentProcessor:sessionReplay];

[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(stop) name:UIApplicationDidEnterBackgroundNotification object:nil];
return YES;
} else {
return NO;
}

sessionReplay = [[SentrySessionReplay alloc] initWithSettings:options.replaySettings];

[sessionReplay start:SentryDependencyContainer.sharedInstance.application.windows.firstObject
fullSession:[self shouldReplayFullSession:options.replaySettings.replaysSessionSampleRate]];

SentryClient *client = [SentrySDK.currentHub getClient];
[client addAttachmentProcessor:sessionReplay];

[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(stop) name:UIApplicationDidEnterBackgroundNotification object:nil];
return YES;
}

-(void)stop {
Expand Down
1 change: 1 addition & 0 deletions Sources/Sentry/include/SentryBaseIntegration.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ typedef NS_OPTIONS(NSUInteger, SentryIntegrationOption) {
kIntegrationOptionAttachViewHierarchy = 1 << 15,
kIntegrationOptionEnableCrashHandler = 1 << 16,
kIntegrationOptionEnableMetricKit = 1 << 17,
kIntegrationOptionEnableReplay = 1 << 18,
};

@interface SentryBaseIntegration : NSObject
Expand Down

0 comments on commit d6d8ce0

Please sign in to comment.