Skip to content

Commit

Permalink
ref: Add delegate to SentryBreadcrumbTracker (#2914)
Browse files Browse the repository at this point in the history
Replace usages of SentrySDK.addBreadcrumb with delegate except
one usage inside of swizzling, which can't be easily replaced
cause we can't reference an instance of SentryBreadcrumbTracker
inside SwizzleInstanceMethod. To get rid of SentrySDK we need
to add swizzling viewDidAppear to swizzleWrapper, which we are
going to do in a subsequent PR.
  • Loading branch information
philipphofmann committed Apr 17, 2023
1 parent 43aa39d commit 7419285
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 35 deletions.
2 changes: 1 addition & 1 deletion Sources/Sentry/SentryAutoBreadcrumbTrackingIntegration.m
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ - (void)installWithOptions:(nonnull SentryOptions *)options
systemEventBreadcrumbs:(SentrySystemEventBreadcrumbs *)systemEventBreadcrumbs
{
self.breadcrumbTracker = breadcrumbTracker;
[self.breadcrumbTracker start];
[self.breadcrumbTracker startWithDelegate:self];

if (options.enableSwizzling) {
[self.breadcrumbTracker startSwizzle];
Expand Down
39 changes: 19 additions & 20 deletions Sources/Sentry/SentryBreadcrumbTracker.m
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#import "SentryBreadcrumbTracker.h"
#import "SentryBreadcrumb.h"
#import "SentryBreadcrumbDelegate.h"
#import "SentryClient.h"
#import "SentryDefines.h"
#import "SentryHub.h"
Expand All @@ -25,6 +26,7 @@
SentryBreadcrumbTracker ()

@property (nonatomic, strong) SentrySwizzleWrapper *swizzleWrapper;
@property (nonatomic, weak) id<SentryBreadcrumbDelegate> delegate;

@end

Expand All @@ -38,8 +40,9 @@ - (instancetype)initWithSwizzleWrapper:(SentrySwizzleWrapper *)swizzleWrapper
return self;
}

- (void)start
- (void)startWithDelegate:(id<SentryBreadcrumbDelegate>)delegate
{
_delegate = delegate;
[self addEnabledCrumb];
[self trackApplicationUIKitNotifications];
}
Expand All @@ -57,6 +60,7 @@ - (void)stop
#if SENTRY_HAS_UIKIT
[self.swizzleWrapper removeSwizzleSendActionForKey:SentryBreadcrumbTrackerSwizzleSendAction];
#endif
_delegate = nil;
}

- (void)trackApplicationUIKitNotifications
Expand All @@ -81,15 +85,13 @@ - (void)trackApplicationUIKitNotifications
object:nil
queue:nil
usingBlock:^(NSNotification *notification) {
if (nil != [SentrySDK.currentHub getClient]) {
SentryBreadcrumb *crumb =
[[SentryBreadcrumb alloc] initWithLevel:kSentryLevelWarning
category:@"device.event"];
crumb.type = @"system";
crumb.data = @ { @"action" : @"LOW_MEMORY" };
crumb.message = @"Low memory";
[SentrySDK addBreadcrumb:crumb];
}
SentryBreadcrumb *crumb =
[[SentryBreadcrumb alloc] initWithLevel:kSentryLevelWarning
category:@"device.event"];
crumb.type = @"system";
crumb.data = @ { @"action" : @"LOW_MEMORY" };
crumb.message = @"Low memory";
[self.delegate addBreadcrumb:crumb];
}];
#endif

Expand Down Expand Up @@ -124,12 +126,10 @@ - (void)addBreadcrumbWithType:(NSString *)type
withDataKey:(NSString *)key
withDataValue:(NSString *)value
{
if (nil != [SentrySDK.currentHub getClient]) {
SentryBreadcrumb *crumb = [[SentryBreadcrumb alloc] initWithLevel:level category:category];
crumb.type = type;
crumb.data = @{ key : value };
[SentrySDK addBreadcrumb:crumb];
}
SentryBreadcrumb *crumb = [[SentryBreadcrumb alloc] initWithLevel:level category:category];
crumb.type = type;
crumb.data = @{ key : value };
[self.delegate addBreadcrumb:crumb];
}

- (void)addEnabledCrumb
Expand All @@ -138,7 +138,7 @@ - (void)addEnabledCrumb
category:@"started"];
crumb.type = @"debug";
crumb.message = @"Breadcrumb Tracking";
[SentrySDK addBreadcrumb:crumb];
[self.delegate addBreadcrumb:crumb];
}

#if SENTRY_HAS_UIKIT
Expand All @@ -165,8 +165,7 @@ - (void)swizzleSendAction
#if SENTRY_HAS_UIKIT
[self.swizzleWrapper
swizzleSendAction:^(NSString *action, id target, id sender, UIEvent *event) {
if ([SentrySDK.currentHub getClient] == nil ||
[SentryBreadcrumbTracker avoidSender:sender forTarget:target action:action]) {
if ([SentryBreadcrumbTracker avoidSender:sender forTarget:target action:action]) {
return;
}

Expand All @@ -182,7 +181,7 @@ - (void)swizzleSendAction
crumb.type = @"user";
crumb.message = action;
crumb.data = data;
[SentrySDK addBreadcrumb:crumb];
[self.delegate addBreadcrumb:crumb];
}
forKey:SentryBreadcrumbTrackerSwizzleSendAction];

Expand Down
4 changes: 3 additions & 1 deletion Sources/Sentry/include/SentryBreadcrumbTracker.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ NS_ASSUME_NONNULL_BEGIN

@class SentrySwizzleWrapper;

@protocol SentryBreadcrumbDelegate;

@interface SentryBreadcrumbTracker : NSObject
SENTRY_NO_INIT

- (instancetype)initWithSwizzleWrapper:(SentrySwizzleWrapper *)swizzleWrapper;

- (void)start;
- (void)startWithDelegate:(id<SentryBreadcrumbDelegate>)delegate;
- (void)startSwizzle;
- (void)stop;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ class SentryAutoBreadcrumbTrackingIntegrationTests: XCTestCase {

private class SentryTestBreadcrumbTracker: SentryBreadcrumbTracker {

let startInvocations = Invocations<Void>()
override func start() {
startInvocations.record(Void())
let startInvocations = Invocations<SentryBreadcrumbDelegate>()
override func start(with delegate: SentryBreadcrumbDelegate) {
startInvocations.record(delegate)
}

let startSwizzleInvocations = Invocations<Void>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,17 @@ import XCTest

class SentryBreadcrumbTrackerTests: XCTestCase {

private var scope: Scope!
private var delegate: SentryBreadcrumbTestDelegate!

override func setUp() {
super.setUp()

scope = Scope()
let client = TestClient(options: Options())
let hub = TestHub(client: client, andScope: scope)
SentrySDK.setCurrentHub(hub)
delegate = SentryBreadcrumbTestDelegate()
}

override func tearDown() {
super.tearDown()
clearTestState()
delegate = nil
}

#if os(iOS) || os(tvOS) || targetEnvironment(macCatalyst)
Expand All @@ -25,7 +22,7 @@ class SentryBreadcrumbTrackerTests: XCTestCase {
let swizzleWrapper = SentrySwizzleWrapper.sharedInstance
let sut = SentryBreadcrumbTracker(swizzleWrapper: swizzleWrapper)

sut.start()
sut.start(with: delegate)
sut.startSwizzle()
sut.stop()

Expand All @@ -34,8 +31,13 @@ class SentryBreadcrumbTrackerTests: XCTestCase {
}

func testSwizzlingStarted_ViewControllerAppears_AddsUILifeCycleBreadcrumb() {
let scope = Scope()
let client = TestClient(options: Options())
let hub = TestHub(client: client, andScope: scope)
SentrySDK.setCurrentHub(hub)

let sut = SentryBreadcrumbTracker(swizzleWrapper: SentrySwizzleWrapper.sharedInstance)
sut.start()
sut.start(with: delegate)
sut.startSwizzle()

let viewController = UIViewController()
Expand All @@ -45,16 +47,18 @@ class SentryBreadcrumbTrackerTests: XCTestCase {

let crumbs = Dynamic(scope).breadcrumbArray.asArray as? [Breadcrumb]

XCTAssertEqual(2, crumbs?.count)
XCTAssertEqual(1, crumbs?.count)

let lifeCycleCrumb = crumbs?[1]
let lifeCycleCrumb = crumbs?[0]
XCTAssertEqual("navigation", lifeCycleCrumb?.type)
XCTAssertEqual("ui.lifecycle", lifeCycleCrumb?.category)
XCTAssertEqual("false", lifeCycleCrumb?.data?["beingPresented"] as? String)
XCTAssertEqual("UIViewController", lifeCycleCrumb?.data?["screen"] as? String)
XCTAssertEqual("test title", lifeCycleCrumb?.data?["title"] as? String)
XCTAssertEqual("false", lifeCycleCrumb?.data?["beingPresented"] as? String)
XCTAssertEqual("UINavigationController", lifeCycleCrumb?.data?["parentViewController"] as? String)

clearTestState()
}

func testExtractDataFrom_View() {
Expand Down

0 comments on commit 7419285

Please sign in to comment.