Skip to content

Commit b58e9fa

Browse files
committed
ref: Refactor performance tracker swift version
1 parent dad3d69 commit b58e9fa

8 files changed

+93
-48
lines changed

Sources/Sentry/SentryDefaultUIViewControllerPerformanceTracker.m

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,15 @@
2020
# import <objc/runtime.h>
2121

2222
@interface SentryTimeToDisplayTracker () <SentryInitialDisplayReporting>
23+
@end
2324

25+
@interface SentrySwiftUISpanHelper () <SentryInitialDisplayReporting>
2426
@end
2527

2628
@interface SentryObjCSwiftUISpanHelper ()
29+
30+
@property (nonatomic, strong) id<SentryInitialDisplayReporting> initialDisplayReporting;
31+
2732
@end
2833

2934
@implementation SentryObjCSwiftUISpanHelper
@@ -38,6 +43,11 @@ - (instancetype)initWithHasSpan:(BOOL)hasSpan
3843
return self;
3944
}
4045

46+
- (void)reportInitialDisplay
47+
{
48+
[self.initialDisplayReporting reportInitialDisplay];
49+
}
50+
4151
@end
4252

4353
// In a previous implementation, we used associated objects to store the time to display tracker,
@@ -90,10 +100,6 @@ - (instancetype)initWithTracker:(SentryPerformanceTracker *)tracker
90100
if (self = [super init]) {
91101
self.tracker = tracker;
92102

93-
SentryOptions *options = [SentrySDKInternal options];
94-
self.inAppLogic = [[SentryInAppLogic alloc] initWithInAppIncludes:options.inAppIncludes
95-
inAppExcludes:options.inAppExcludes];
96-
97103
_alwaysWaitForFullDisplay = NO;
98104
_dispatchQueueWrapper = dispatchQueueWrapper;
99105

@@ -106,9 +112,10 @@ - (instancetype)initWithTracker:(SentryPerformanceTracker *)tracker
106112
}
107113

108114
- (void)viewControllerLoadView:(UIViewController *)controller
115+
isInApp:(BOOL (^)(Class))isInApp
109116
callbackToOrigin:(void (^)(void))callbackToOrigin
110117
{
111-
if (![self.inAppLogic isClassInApp:[controller class]]) {
118+
if (!isInApp([controller class])) {
112119
SENTRY_LOG_DEBUG(
113120
@"Won't track view controller that is not part of the app bundle: %@.", controller);
114121
callbackToOrigin();
@@ -273,34 +280,34 @@ - (nullable SentryTimeToDisplayTracker *)startTimeToDisplayTrackerForScreen:(NSS
273280
return ttdTracker;
274281
}
275282

276-
- (SentrySwiftUISpanHelper *)startTimeToDisplayTrackerForScreen:(NSString *)screenName
277-
waitForFullDisplay:(BOOL)waitforFullDisplay
278-
transactionId:(SentrySpanId *)transactionId;
283+
- (SentryObjCSwiftUISpanHelper *)startTimeToDisplayTrackerForScreen:(NSString *)screenName
284+
waitForFullDisplay:(BOOL)waitforFullDisplay
285+
transactionId:(SentrySpanId *)transactionId;
279286
{
280287
id<SentrySpan> span = [SentryPerformanceTracker.shared getSpan:transactionId];
281288
if (span != nil && [span isKindOfClass:[SentryTracer class]]) {
282289
SentryTimeToDisplayTracker *displayReporting =
283290
[self startTimeToDisplayTrackerForScreen:screenName
284291
waitForFullDisplay:waitforFullDisplay
285292
tracer:(SentryTracer *)span];
286-
return [[SentrySwiftUISpanHelper alloc] initWithHasSpan:YES
287-
initialDisplayReporting:displayReporting];
293+
return [[SentryObjCSwiftUISpanHelper alloc] initWithHasSpan:YES
294+
initialDisplayReporting:displayReporting];
288295
}
289-
return [[SentrySwiftUISpanHelper alloc] initWithHasSpan:NO initialDisplayReporting:nil];
296+
return [[SentryObjCSwiftUISpanHelper alloc] initWithHasSpan:NO initialDisplayReporting:nil];
290297
}
291298

292299
+ (SentryObjCSwiftUISpanHelper *)startTimeToDisplayTrackerForScreen:(NSString *)screenName
293300
waitForFullDisplay:(BOOL)waitforFullDisplay
294-
transactionId:(SentrySpanId *)transactionId;
301+
transactionId:(SentrySpanId *)transactionId
295302
{
296-
id<SentryUIViewControllerPerformanceTracker> vcTracker
303+
SentryUIViewControllerPerformanceTracker *vcTracker
297304
= SentryDependencyContainer.sharedInstance.uiViewControllerPerformanceTracker;
298305
SentrySwiftUISpanHelper *result =
299306
[vcTracker startTimeToDisplayTrackerForScreen:screenName
300307
waitForFullDisplay:waitforFullDisplay
301308
transactionId:transactionId];
302309
return [[SentryObjCSwiftUISpanHelper alloc] initWithHasSpan:result.hasSpan
303-
initialDisplayReporting:result.initialDisplayReporting];
310+
initialDisplayReporting:result];
304311
}
305312

306313
- (void)viewControllerViewWillAppear:(UIViewController *)controller

Sources/Sentry/SentryDependencyContainer.m

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,6 @@ @interface SentryDefaultAppStateManager () <SentryAppStateManager>
6464
#if SENTRY_HAS_UIKIT
6565
@interface SentryWatchdogTerminationScopeObserver () <SentryScopeObserver>
6666
@end
67-
68-
@interface SentryDefaultUIViewControllerPerformanceTracker () <
69-
SentryUIViewControllerPerformanceTracker>
70-
@end
7167
#endif
7268

7369
@interface SentryDependencyContainer ()
@@ -317,14 +313,13 @@ - (SentryViewHierarchyProvider *)viewHierarchyProvider SENTRY_THREAD_SANITIZER_D
317313
# endif // SENTRY_HAS_UIKIT
318314
}
319315

320-
- (id<SentryUIViewControllerPerformanceTracker>)
316+
- (SentryUIViewControllerPerformanceTracker *)
321317
uiViewControllerPerformanceTracker SENTRY_THREAD_SANITIZER_DOUBLE_CHECKED_LOCK
322318
{
323319
# if SENTRY_HAS_UIKIT
324320
SENTRY_LAZY_INIT(_uiViewControllerPerformanceTracker,
325-
[[SentryDefaultUIViewControllerPerformanceTracker alloc]
326-
initWithTracker:SentryPerformanceTracker.shared
327-
dispatchQueueWrapper:[self dispatchQueueWrapper]]);
321+
[[SentryUIViewControllerPerformanceTracker alloc]
322+
initWithDispatchQueueWrapper:[self dispatchQueueWrapper]]);
328323
# else
329324
SENTRY_LOG_DEBUG(@"SentryDependencyContainer.uiViewControllerPerformanceTracker only works "
330325
@"with UIKit enabled. Ensure you're "

Sources/Sentry/SentryPerformanceTrackingIntegration.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ - (BOOL)installWithOptions:(SentryOptions *)options
4343
binaryImageCache:[SentryDependencyContainer.sharedInstance binaryImageCache]];
4444

4545
[self.swizzling start];
46-
id<SentryUIViewControllerPerformanceTracker> performanceTracker =
46+
SentryUIViewControllerPerformanceTracker *performanceTracker =
4747
[SentryDependencyContainer.sharedInstance uiViewControllerPerformanceTracker];
4848
performanceTracker.alwaysWaitForFullDisplay = options.enableTimeToFullDisplayTracing;
4949

Sources/Sentry/SentryUIViewControllerSwizzling.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ - (void)start
110110
}
111111

112112
[self swizzleUIViewController];
113-
id<SentryUIViewControllerPerformanceTracker> performanceTracker =
113+
SentryUIViewControllerPerformanceTracker *performanceTracker =
114114
[SentryDependencyContainer.sharedInstance uiViewControllerPerformanceTracker];
115115
performanceTracker.inAppLogic = self.inAppLogic;
116116
}

Sources/Sentry/include/HybridPublic/SentryDependencyContainer.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@
4444
@class SentryScreenshotSource;
4545
@class SentryViewHierarchyProvider;
4646
@class SentryWatchdogTerminationAttributesProcessor;
47+
@class SentryUIViewControllerPerformanceTracker;
4748

4849
@protocol SentryScopeObserver;
49-
@protocol SentryUIViewControllerPerformanceTracker;
5050
#endif // SENTRY_UIKIT_AVAILABLE
5151

5252
#if SENTRY_HAS_UIKIT
@@ -120,8 +120,8 @@ SENTRY_NO_INIT
120120
#if SENTRY_UIKIT_AVAILABLE
121121
@property (nonatomic, strong) SentryFramesTracker *framesTracker;
122122
@property (nonatomic, strong) SentryViewHierarchyProvider *viewHierarchyProvider;
123-
@property (nonatomic, strong) id<SentryUIViewControllerPerformanceTracker>
124-
uiViewControllerPerformanceTracker;
123+
@property (nonatomic, strong)
124+
SentryUIViewControllerPerformanceTracker *uiViewControllerPerformanceTracker;
125125
#endif // SENTRY_UIKIT_AVAILABLE
126126

127127
#if SENTRY_TARGET_REPLAY_SUPPORTED

Sources/Sentry/include/SentryDefaultUIViewControllerPerformanceTracker.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,13 @@
99
@class SentryPerformanceTracker;
1010
@class SentryDispatchQueueWrapper;
1111

12-
@protocol SentryInitialDisplayReporting;
13-
1412
NS_ASSUME_NONNULL_BEGIN
1513

1614
@interface SentryObjCSwiftUISpanHelper : NSObject
1715

1816
@property (nonatomic, readonly) BOOL hasSpan;
1917

20-
@property (nonatomic, strong, readonly, nullable) id<SentryInitialDisplayReporting>
21-
initialDisplayReporting;
18+
- (void)reportInitialDisplay;
2219

2320
@end
2421

@@ -28,8 +25,6 @@ NS_ASSUME_NONNULL_BEGIN
2825
*/
2926
@interface SentryDefaultUIViewControllerPerformanceTracker : NSObject
3027

31-
@property (nonatomic, strong) SentryInAppLogic *inAppLogic;
32-
3328
@property (nonatomic) BOOL alwaysWaitForFullDisplay;
3429

3530
SENTRY_NO_INIT
@@ -46,6 +41,7 @@ SENTRY_NO_INIT
4641
* @c loadView method.
4742
*/
4843
- (void)viewControllerLoadView:(UIViewController *)controller
44+
isInApp:(BOOL (^)(Class))isInApp
4945
callbackToOrigin:(void (^)(void))callback;
5046

5147
/**
@@ -106,6 +102,10 @@ SENTRY_NO_INIT
106102

107103
- (void)reportFullyDisplayed;
108104

105+
- (SentryObjCSwiftUISpanHelper *)startTimeToDisplayTrackerForScreen:(NSString *)screenName
106+
waitForFullDisplay:(BOOL)waitforFullDisplay
107+
transactionId:(SentrySpanId *)transactionId;
108+
109109
+ (SentryObjCSwiftUISpanHelper *)startTimeToDisplayTrackerForScreen:(NSString *)screenName
110110
waitForFullDisplay:(BOOL)waitforFullDisplay
111111
transactionId:(SentrySpanId *)transactionId;

Sources/Sentry/include/SentryPrivate.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#import "SentryCrashMonitor_System.h"
3131
#import "SentryDateUtils.h"
3232
#import "SentryDefaultThreadInspector.h"
33+
#import "SentryDefaultUIViewControllerPerformanceTracker.h"
3334
#import "SentryDependencyContainerSwiftHelper.h"
3435
#import "SentryDeviceContextKeys.h"
3536
#import "SentryEvent+Serialize.h"
@@ -41,6 +42,7 @@
4142
#import "SentryMsgPackSerializer.h"
4243
#import "SentryNSDictionarySanitize.h"
4344
#import "SentryOptions+Private.h"
45+
#import "SentryPerformanceTracker.h"
4446
#import "SentryProfiler+Private.h"
4547
#import "SentrySDKInternal.h"
4648
#import "SentryScope+PrivateSwift.h"
Lines changed: 56 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#if (os(iOS) || os(tvOS) || (swift(>=5.9) && os(visionOS))) && !SENTRY_NO_UIKIT
22

3+
@_implementationOnly import _SentryPrivate
34
import UIKit
45

56
@_spi(Private) @objc public protocol SentryInitialDisplayReporting {
@@ -8,40 +9,80 @@ import UIKit
89

910
@_spi(Private) @objc public class SentrySwiftUISpanHelper: NSObject {
1011
@objc public let hasSpan: Bool
11-
@objc public let initialDisplayReporting: SentryInitialDisplayReporting?
1212

13-
@objc public init(hasSpan: Bool, initialDisplayReporting: SentryInitialDisplayReporting?) {
13+
@objc public func reportInitialDisplay() {
14+
initialDisplayReporting()
15+
}
16+
private let initialDisplayReporting: () -> Void
17+
18+
@objc public init(hasSpan: Bool, initialDisplayReporting: @escaping () -> Void) {
1419
self.hasSpan = hasSpan
1520
self.initialDisplayReporting = initialDisplayReporting
1621
}
1722
}
1823

19-
@_spi(Private) @objc public protocol SentryUIViewControllerPerformanceTracker {
24+
@_spi(Private) @objc public final class SentryUIViewControllerPerformanceTracker: NSObject {
2025

21-
var inAppLogic: SentryInAppLogic { get set }
26+
private let helper: SentryDefaultUIViewControllerPerformanceTracker
2227

23-
var alwaysWaitForFullDisplay: Bool { get set }
28+
@objc public init(dispatchQueueWrapper: SentryDispatchQueueWrapper) {
29+
let options = SentrySDKInternal.options
30+
inAppLogic = SentryInAppLogic(inAppIncludes: options?.inAppIncludes ?? [], inAppExcludes: options?.inAppExcludes ?? [])
31+
helper = SentryDefaultUIViewControllerPerformanceTracker(tracker: SentryPerformanceTracker.shared, dispatchQueueWrapper: dispatchQueueWrapper)
32+
}
2433

25-
func viewControllerLoadView(_ controller: UIViewController, callbackToOrigin callback: @escaping () -> Void)
34+
@objc public var inAppLogic: SentryInAppLogic
2635

27-
func viewControllerViewDidLoad(_ controller: UIViewController, callbackToOrigin callback: @escaping () -> Void)
36+
@objc public var alwaysWaitForFullDisplay: Bool { get {
37+
helper.alwaysWaitForFullDisplay
38+
} set {
39+
helper.alwaysWaitForFullDisplay = newValue
40+
} }
2841

29-
func viewControllerViewWillAppear(_ controller: UIViewController, callbackToOrigin callback: @escaping () -> Void)
42+
@objc public func viewControllerLoadView(_ controller: UIViewController, callbackToOrigin callback: @escaping () -> Void) {
43+
let inAppLogic = self.inAppLogic
44+
helper.viewControllerLoadView(controller, isInApp: { c in
45+
inAppLogic.isClassInApp(c)
46+
}, callbackToOrigin: callback)
47+
}
3048

31-
func viewControllerViewWillDisappear(_ controller: UIViewController, callbackToOrigin callback: @escaping () -> Void)
49+
@objc public func viewControllerViewDidLoad(_ controller: UIViewController, callbackToOrigin callback: @escaping () -> Void) {
50+
helper.viewControllerViewDidLoad(controller, callbackToOrigin: callback)
51+
}
52+
53+
@objc public func viewControllerViewWillAppear(_ controller: UIViewController, callbackToOrigin callback: @escaping () -> Void) {
54+
helper.viewControllerViewWillAppear(controller, callbackToOrigin: callback)
55+
}
56+
57+
@objc public func viewControllerViewWillDisappear(_ controller: UIViewController, callbackToOrigin callback: @escaping () -> Void) {
58+
helper.viewControllerViewWillDisappear(controller, callbackToOrigin: callback)
59+
}
3260

33-
func viewControllerViewDidAppear(_ controller: UIViewController, callbackToOrigin callback: @escaping () -> Void)
61+
@objc public func viewControllerViewDidAppear(_ controller: UIViewController, callbackToOrigin callback: @escaping () -> Void) {
62+
helper.viewControllerViewDidAppear(controller, callbackToOrigin: callback)
63+
}
3464

35-
func viewControllerViewWillLayoutSubViews(_ controller: UIViewController, callbackToOrigin callback: @escaping () -> Void)
65+
@objc public func viewControllerViewWillLayoutSubViews(_ controller: UIViewController, callbackToOrigin callback: @escaping () -> Void) {
66+
helper.viewControllerViewWillLayoutSubViews(controller, callbackToOrigin: callback)
67+
}
3668

37-
func viewControllerViewDidLayoutSubViews(_ controller: UIViewController, callbackToOrigin callback: @escaping () -> Void)
69+
@objc public func viewControllerViewDidLayoutSubViews(_ controller: UIViewController, callbackToOrigin callback: @escaping () -> Void) {
70+
helper.viewControllerViewDidLayoutSubViews(controller, callbackToOrigin: callback)
71+
}
3872

39-
func reportFullyDisplayed()
73+
@objc public func reportFullyDisplayed() {
74+
helper.reportFullyDisplayed()
75+
}
4076

41-
func startTimeToDisplayTracker(
77+
@objc public func startTimeToDisplayTracker(
4278
forScreen screenName: String,
4379
waitForFullDisplay: Bool,
44-
transactionId: SpanId) -> SentrySwiftUISpanHelper
80+
transactionId: SpanId) -> SentrySwiftUISpanHelper {
81+
let objcType = helper.startTimeToDisplay(forScreen: screenName, waitForFullDisplay: waitForFullDisplay, transactionId: transactionId)
82+
return SentrySwiftUISpanHelper(hasSpan: objcType.hasSpan) {
83+
objcType.reportInitialDisplay()
84+
}
85+
}
4586
}
4687

4788
#endif

0 commit comments

Comments
 (0)