Skip to content
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

feat: Enhance the UIViewController breadcrumbs with more data #1945

Merged
merged 24 commits into from
Jul 13, 2022
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
d4639f3
feat: Enhance the UIViewController breadcrumbs with more data
kevinrenskers Jul 6, 2022
24ab320
Format code
getsentry-bot Jul 6, 2022
87a13de
Add changelog
kevinrenskers Jul 6, 2022
6ed0aac
Merge branch 'master' into feat/extra-controller-breadcrumb-data
kevinrenskers Jul 7, 2022
8e4e078
Add a bunch more information
kevinrenskers Jul 7, 2022
0424de7
Merge branch 'master' into feat/extra-controller-breadcrumb-data
kevinrenskers Jul 7, 2022
480fe3f
Format code
getsentry-bot Jul 7, 2022
17a3727
Add a few tests
kevinrenskers Jul 7, 2022
f11ddda
Make it build on macOS
kevinrenskers Jul 7, 2022
c2f2948
Remove the storyboard info, not very useful
kevinrenskers Jul 7, 2022
ce09f8c
Added more information and solved an iOS crash about unrecognised sel…
kevinrenskers Jul 8, 2022
34717a7
Merge branch 'master' into feat/extra-controller-breadcrumb-data
kevinrenskers Jul 8, 2022
d4c35da
Format code
getsentry-bot Jul 8, 2022
cc4c5b0
Add some more tests
kevinrenskers Jul 8, 2022
d62cb92
Merge branch 'master' into feat/extra-controller-breadcrumb-data
kevinrenskers Jul 8, 2022
773e10b
Add test for parentViewController
kevinrenskers Jul 8, 2022
d00e5b4
Added UI test: testBreadcrumbData
kevinrenskers Jul 8, 2022
79682e2
Fix build error in iOS13-Swift
kevinrenskers Jul 11, 2022
341eb5a
Merge branch 'master' into feat/extra-controller-breadcrumb-data
kevinrenskers Jul 11, 2022
8d990e2
Remove unused import
kevinrenskers Jul 12, 2022
476e262
Fix the test
kevinrenskers Jul 12, 2022
beee960
Merge branch 'master' into feat/extra-controller-breadcrumb-data
kevinrenskers Jul 12, 2022
aec148d
Different way of getting the breadcrumb data on screen
kevinrenskers Jul 13, 2022
0df8b25
Remove force cast and force unwrap
kevinrenskers Jul 13, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Features

- Enhance the UIViewController breadcrumbs with more data (#1945)
- feat: Add extra app start span (#1952)
- Add enableAutoBreadcrumbTracking option (#1958)

Expand Down
6 changes: 6 additions & 0 deletions Samples/iOS-Swift/iOS-Swift.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
objects = {

/* Begin PBXBuildFile section */
0A1C358B287C17AF007D01E3 /* UINotificationExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0A1C358A287C17AF007D01E3 /* UINotificationExtension.swift */; };
0A1C358D287C17BA007D01E3 /* UINotificationExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0A1C358A287C17AF007D01E3 /* UINotificationExtension.swift */; };
630853532440C60F00DDE4CE /* Sentry.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 630853322440C44F00DDE4CE /* Sentry.framework */; };
637AFDAA243B02760034958B /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 637AFDA9243B02760034958B /* AppDelegate.swift */; };
637AFDAE243B02760034958B /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 637AFDAD243B02760034958B /* ViewController.swift */; };
Expand Down Expand Up @@ -191,6 +193,7 @@
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
0A1C358A287C17AF007D01E3 /* UINotificationExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UINotificationExtension.swift; sourceTree = "<group>"; };
6308532C2440C44F00DDE4CE /* Sentry.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = Sentry.xcodeproj; path = ../../Sentry.xcodeproj; sourceTree = "<group>"; };
637AFDA6243B02760034958B /* iOS-Swift.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "iOS-Swift.app"; sourceTree = BUILT_PRODUCTS_DIR; };
637AFDA9243B02760034958B /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -451,6 +454,7 @@
D8F3D061274EBD4800B56F8C /* SpanExtension.swift */,
D8D7BB4B2750095800044146 /* UIViewExtension.swift */,
D8444E4B275E38090042F4DE /* UIViewControllerExtension.swift */,
0A1C358A287C17AF007D01E3 /* UINotificationExtension.swift */,
);
path = Extensions;
sourceTree = "<group>";
Expand Down Expand Up @@ -687,6 +691,7 @@
files = (
D8F3D05F274E6A8B00B56F8C /* AssertView.swift in Sources */,
D890CD3F26CEE31B001246CF /* NibViewController.swift in Sources */,
0A1C358B287C17AF007D01E3 /* UINotificationExtension.swift in Sources */,
D845F35B27BAD4CC00A4D7A2 /* SentryData.xcdatamodeld in Sources */,
D8444E4C275E38090042F4DE /* UIViewControllerExtension.swift in Sources */,
637AFDAE243B02760034958B /* ViewController.swift in Sources */,
Expand Down Expand Up @@ -729,6 +734,7 @@
D8444E57275F795D0042F4DE /* UIViewControllerExtension.swift in Sources */,
D8F3D058274E57D600B56F8C /* TableViewController.swift in Sources */,
D8269A58274C0FC700BD5BD5 /* ViewController.swift in Sources */,
0A1C358D287C17BA007D01E3 /* UINotificationExtension.swift in Sources */,
844DA821282584C300E6B62E /* CoreDataViewController.swift in Sources */,
D8444E55275F79570042F4DE /* SpanExtension.swift in Sources */,
D8444E51275F79240042F4DE /* AssertView.swift in Sources */,
Expand Down
7 changes: 6 additions & 1 deletion Samples/iOS-Swift/iOS-Swift/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import UIKit
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

static let defaultDSN = "https://a92d50327ac74b8b9aa4ea80eccfb267@o447951.ingest.sentry.io/5428557"

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
Expand Down Expand Up @@ -35,6 +35,11 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
if !ProcessInfo.processInfo.arguments.contains("--io.sentry.test.benchmarking") {
options.enableUserInteractionTracing = true
}

options.beforeBreadcrumb = { breadcrumb in
NotificationCenter.default.post(name: .breadcrumb, object: breadcrumb)
return breadcrumb
}
kevinrenskers marked this conversation as resolved.
Show resolved Hide resolved
}

return true
Expand Down
14 changes: 11 additions & 3 deletions Samples/iOS-Swift/iOS-Swift/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<stackView opaque="NO" contentMode="scaleAspectFit" axis="vertical" distribution="fillProportionally" translatesAutoresizingMaskIntoConstraints="NO" id="480-5y-FtF">
<rect key="frame" x="8" y="252.5" width="398" height="445"/>
<rect key="frame" x="8" y="242.5" width="398" height="465.5"/>
<subviews>
<stackView opaque="NO" contentMode="scaleToFill" distribution="fillProportionally" alignment="top" translatesAutoresizingMaskIntoConstraints="NO" id="VbH-LD-DBn">
<rect key="frame" x="0.0" y="0.0" width="398" height="300"/>
Expand Down Expand Up @@ -185,7 +185,7 @@
</subviews>
</stackView>
<stackView opaque="NO" contentMode="scaleToFill" axis="vertical" translatesAutoresizingMaskIntoConstraints="NO" id="UrL-kT-AJU">
<rect key="frame" x="0.0" y="300" width="398" height="145"/>
<rect key="frame" x="0.0" y="300" width="398" height="165.5"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="DSN " textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="m3h-wb-Xfa">
<rect key="frame" x="8" y="32" width="382" height="20.5"/>
Expand Down Expand Up @@ -215,6 +215,13 @@
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Last breadcrumb" textAlignment="natural" lineBreakMode="tailTruncation" numberOfLines="0" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="Ccs-Ny-MiI" userLabel="breadcrumb">
<rect key="frame" x="8" y="137" width="382" height="20.5"/>
<accessibility key="accessibilityConfiguration" identifier="breadcrumbLabel"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
</subviews>
<edgeInsets key="layoutMargins" top="32" left="8" bottom="8" right="8"/>
</stackView>
Expand All @@ -233,6 +240,7 @@
<connections>
<outlet property="anrFillingRunLoopButton" destination="TSF-10-5ts" id="Qp6-8q-IPH"/>
<outlet property="anrFullyBlockingButton" destination="Yi7-GN-26l" id="tMf-U3-onN"/>
<outlet property="breadcrumbLabel" destination="Ccs-Ny-MiI" id="WqJ-PA-KfJ"/>
<outlet property="dsnTextField" destination="L2a-LY-eQ7" id="TWn-0u-2v7"/>
<outlet property="framesLabel" destination="Lcm-n2-ys4" id="7mO-bc-HWw"/>
</connections>
Expand Down Expand Up @@ -372,7 +380,7 @@
</items>
</navigationBar>
<textView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" fixedFrame="YES" preservesSuperviewLayoutMargins="YES" layoutMarginsFollowReadableWidth="YES" delaysContentTouches="NO" editable="NO" adjustsFontForContentSizeCategory="YES" translatesAutoresizingMaskIntoConstraints="NO" id="sla-j3-cfX">
<rect key="frame" x="20" y="70" width="374" height="826"/>
<rect key="frame" x="19" y="70" width="374" height="825"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="backgroundColor" systemColor="systemBackgroundColor"/>
<color key="textColor" systemColor="labelColor"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
extension Notification.Name {
static let breadcrumb = Notification.Name("breadcrumb")
}
12 changes: 10 additions & 2 deletions Samples/iOS-Swift/iOS-Swift/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class ViewController: UIViewController {
@IBOutlet weak var anrFullyBlockingButton: UIButton!
@IBOutlet weak var anrFillingRunLoopButton: UIButton!
@IBOutlet weak var framesLabel: UILabel!
@IBOutlet weak var breadcrumbLabel: UILabel!

private let dispatchQueue = DispatchQueue(label: "ViewController")

Expand All @@ -18,6 +19,7 @@ class ViewController: UIViewController {
scope.setEnvironment("debug")
scope.setTag(value: "swift", key: "language")
scope.setExtra(value: String(describing: self), key: "currentViewController")

let user = Sentry.User(userId: "1")
user.email = "tony@example.com"
scope.setUser(user)
Expand All @@ -28,8 +30,8 @@ class ViewController: UIViewController {
if let data = "hello".data(using: .utf8) {
scope.add(Attachment(data: data, filename: "log.txt"))
}

}

// Also works
let user = Sentry.User(userId: "1")
user.email = "tony1@example.com"
Expand All @@ -43,7 +45,13 @@ class ViewController: UIViewController {
self.dsnTextField.backgroundColor = UIColor.systemGreen
}
}


NotificationCenter.default.addObserver(forName: .breadcrumb, object: nil, queue: .main) { notification in
guard let breadcrumb = notification.object as? Breadcrumb else { return }
if breadcrumb.data?["parentViewController"] != nil {
self.breadcrumbLabel.text = "{ category: \(breadcrumb.category), parentViewController: \(breadcrumb.data?["parentViewController"] ?? "nil"), beingPresented: \(breadcrumb.data?["beingPresented"] ?? "nil"), window_isKeyWindow: \(breadcrumb.data?["window_isKeyWindow"] ?? "nil"), is_window_rootViewController: \(breadcrumb.data?["is_window_rootViewController"] ?? "nil") }"
}
}
}

override func viewDidAppear(_ animated: Bool) {
Expand Down
6 changes: 6 additions & 0 deletions Samples/iOS-Swift/iOS-SwiftUITests/LaunchUITests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ class LaunchUITests: XCTestCase {
super.tearDown()
}

func testBreadcrumbData() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding a UITest.

let breadcrumbLabel = app.staticTexts["breadcrumbLabel"]
XCTAssertTrue(breadcrumbLabel.waitForExistence(), "Breadcrumb label not found.")
XCTAssertEqual(breadcrumbLabel.label, "{ category: ui.lifecycle, parentViewController: UINavigationController, beingPresented: false, window_isKeyWindow: true, is_window_rootViewController: false }")
}

func testLoremIpsum() {
app.buttons["loremIpsumButton"].tap()
XCTAssertTrue(app.textViews.firstMatch.waitForExistence(), "Lorem Ipsum not loaded.")
Expand Down
43 changes: 39 additions & 4 deletions Sources/Sentry/SentryBreadcrumbTracker.m
Original file line number Diff line number Diff line change
Expand Up @@ -192,14 +192,12 @@ - (void)swizzleViewDidAppear
SentryBreadcrumb *crumb = [[SentryBreadcrumb alloc] initWithLevel:kSentryLevelInfo
category:@"ui.lifecycle"];
crumb.type = @"navigation";
NSString *viewControllerName = [SentryUIViewControllerSanitizer
sanitizeViewControllerName:[NSString stringWithFormat:@"%@", self]];
crumb.data = @ { @"screen" : viewControllerName };
crumb.data = [SentryBreadcrumbTracker fetchInfoAboutViewController:self];

// Adding crumb via the SDK calls SentryBeforeBreadcrumbCallback
[SentrySDK addBreadcrumb:crumb];
[SentrySDK.currentHub configureScope:^(SentryScope *_Nonnull scope) {
[scope setExtraValue:viewControllerName forKey:@"__sentry_transaction"];
[scope setExtraValue:crumb.data[@"screen"] forKey:@"__sentry_transaction"];
}];
}
SentrySWCallOriginal(animated);
Expand Down Expand Up @@ -236,6 +234,43 @@ + (NSDictionary *)extractDataFromView:(UIView *)view

return result;
}

+ (NSDictionary *)fetchInfoAboutViewController:(UIViewController *)controller
{
NSMutableDictionary *info = @{}.mutableCopy;

info[@"screen"] = [SentryUIViewControllerSanitizer
sanitizeViewControllerName:[NSString stringWithFormat:@"%@", controller]];

if ([controller.navigationItem.title length] != 0) {
info[@"title"] = controller.navigationItem.title;
} else if ([controller.title length] != 0) {
info[@"title"] = controller.title;
}

info[@"beingPresented"] = controller.beingPresented ? @"true" : @"false";

if (controller.presentingViewController != nil) {
info[@"presentingViewController"] = [SentryUIViewControllerSanitizer
sanitizeViewControllerName:controller.presentingViewController];
}

if (controller.parentViewController != nil) {
info[@"parentViewController"] = [SentryUIViewControllerSanitizer
sanitizeViewControllerName:controller.parentViewController];
}

if (controller.view.window != nil) {
info[@"window"] = controller.view.window.description;
info[@"window_isKeyWindow"] = controller.view.window.isKeyWindow ? @"true" : @"false";
info[@"window_windowLevel"] =
[NSString stringWithFormat:@"%f", controller.view.window.windowLevel];
info[@"is_window_rootViewController"]
= controller.view.window.rootViewController == controller ? @"true" : @"false";
}

return info;
}
#endif

@end
Expand Down
1 change: 0 additions & 1 deletion Sources/Sentry/include/SentryUIViewControllerSanitizer.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,24 @@ class SentryBreadcrumbTrackerTests: XCTestCase {
let sut = SentryBreadcrumbTracker(swizzleWrapper: SentrySwizzleWrapper.sharedInstance)
sut.start()
sut.startSwizzle()

let viewController = UIViewController()
_ = UINavigationController(rootViewController: viewController)
viewController.title = "test title"
viewController.viewDidAppear(false)

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

XCTAssertEqual(2, crumbs?.count)

let lifeCycleCrumb = crumbs?[1]
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)
}

func testExtractDataFrom_View() {
Expand Down