Skip to content
This repository has been archived by the owner on Dec 14, 2021. It is now read-only.

Commit

Permalink
Add Sentry integration (#636)
Browse files Browse the repository at this point in the history
  • Loading branch information
joeyg authored and devinreams committed Aug 24, 2018
1 parent 586b98e commit af919c2
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cartfile
Expand Up @@ -9,3 +9,4 @@ github "mozilla-mobile/MappaMundi" "master"
github "mozilla/application-services" "0.2.1"
github "mozilla-lockbox/lockbox-ios-fxa-sync" "c41fac5a33519e8d9e7fdeb1a57a6ac231fdb832"
github "adjust/ios_sdk" ~> 4.14.1
github "getsentry/sentry-cocoa" "4.1.0"
3 changes: 2 additions & 1 deletion Cartfile.resolved
Expand Up @@ -6,7 +6,8 @@ github "ReactiveX/RxSwift" "4.2.0"
github "RxSwiftCommunity/RxDataSources" "3.0.2"
github "RxSwiftCommunity/RxOptional" "3.5.0"
github "SwiftyJSON/SwiftyJSON" "3.1.4"
github "adjust/ios_sdk" "v4.14.1"
github "adjust/ios_sdk" "v4.14.3"
github "getsentry/sentry-cocoa" "4.1.0"
github "jrendel/SwiftKeychainWrapper" "3.0.1"
github "mozilla-lockbox/lockbox-ios-fxa-sync" "c41fac5a33519e8d9e7fdeb1a57a6ac231fdb832"
github "mozilla-mobile/MappaMundi" "981095dff1c014a458624820ac8f2cda39c92144"
Expand Down
9 changes: 9 additions & 0 deletions Lockbox.xcodeproj/project.pbxproj
Expand Up @@ -172,6 +172,8 @@
D8783613207C080300C19BC7 /* ExternalLinkAction.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8783612207C080300C19BC7 /* ExternalLinkAction.swift */; };
D8783615207D303E00C19BC7 /* ExternalLinkStoreSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8783614207D303E00C19BC7 /* ExternalLinkStoreSpec.swift */; };
D8783617207D70D100C19BC7 /* PreferredBrowserSettingPresenterSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8783616207D70D100C19BC7 /* PreferredBrowserSettingPresenterSpec.swift */; };
D88177BD212F2DB100436B3D /* Sentry.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D88177BC212F2DB100436B3D /* Sentry.framework */; };
D88177BF212F30F600436B3D /* SentryManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = D88177BE212F30F600436B3D /* SentryManager.swift */; };
D88C40C6209CF27E0022696B /* AutoLockStoreSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = D88C40C5209CF27E0022696B /* AutoLockStoreSpec.swift */; };
D895605C20967C5300CC7E47 /* AutoLockStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = D895605B20967C5300CC7E47 /* AutoLockStore.swift */; };
D8C9C520207DC53B007874E1 /* PreferredBrowserSettingViewSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8C9C51F207DC53B007874E1 /* PreferredBrowserSettingViewSpec.swift */; };
Expand Down Expand Up @@ -409,6 +411,8 @@
D8783612207C080300C19BC7 /* ExternalLinkAction.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ExternalLinkAction.swift; sourceTree = "<group>"; };
D8783614207D303E00C19BC7 /* ExternalLinkStoreSpec.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ExternalLinkStoreSpec.swift; sourceTree = "<group>"; };
D8783616207D70D100C19BC7 /* PreferredBrowserSettingPresenterSpec.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PreferredBrowserSettingPresenterSpec.swift; sourceTree = "<group>"; };
D88177BC212F2DB100436B3D /* Sentry.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Sentry.framework; path = Carthage/Build/iOS/Sentry.framework; sourceTree = "<group>"; };
D88177BE212F30F600436B3D /* SentryManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryManager.swift; sourceTree = "<group>"; };
D88C40C5209CF27E0022696B /* AutoLockStoreSpec.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AutoLockStoreSpec.swift; sourceTree = "<group>"; };
D895605B20967C5300CC7E47 /* AutoLockStore.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AutoLockStore.swift; sourceTree = "<group>"; };
D8C9C51F207DC53B007874E1 /* PreferredBrowserSettingViewSpec.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PreferredBrowserSettingViewSpec.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -446,6 +450,7 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
D88177BD212F2DB100436B3D /* Sentry.framework in Frameworks */,
D8551EBE211954E00056B24A /* AdjustSdk.framework in Frameworks */,
D8551EBC211950BF0056B24A /* iAd.framework in Frameworks */,
D8551EBA211950AF0056B24A /* AdSupport.framework in Frameworks */,
Expand Down Expand Up @@ -557,6 +562,7 @@
isa = PBXGroup;
children = (
7AA5408D5D52952F11E20EEC /* BiometryManager.swift */,
D88177BE212F30F600436B3D /* SentryManager.swift */,
);
path = Helpers;
sourceTree = "<group>";
Expand Down Expand Up @@ -678,6 +684,7 @@
7D1B1A8A1F98F86400C1F5FF /* Frameworks */ = {
isa = PBXGroup;
children = (
D88177BC212F2DB100436B3D /* Sentry.framework */,
D8551EBD211954E00056B24A /* AdjustSdk.framework */,
D8551EBB211950BF0056B24A /* iAd.framework */,
D8551EB9211950AF0056B24A /* AdSupport.framework */,
Expand Down Expand Up @@ -1013,6 +1020,7 @@
"$(SRCROOT)/Carthage/Build/iOS/Deferred.framework",
"$(SRCROOT)/Carthage/Build/iOS/FxAClient.framework",
"$(SRCROOT)/Carthage/Build/iOS/AdjustSdk.framework",
"$(SRCROOT)/Carthage/Build/iOS/Sentry.framework",
);
outputPaths = (
"$(BUILT_PRODUCTS_DIR)/$(FRAMEWORKS_FOLDER_PATH)/RxCocoa.framework",
Expand Down Expand Up @@ -1136,6 +1144,7 @@
7DA4C6862028F85A00B61DD8 /* ItemListPresenter.swift in Sources */,
7AA54C8928B257655856C516 /* AppDelegate.swift in Sources */,
7AA540D7395F48BD6983CC3E /* Dispatcher.swift in Sources */,
D88177BF212F30F600436B3D /* SentryManager.swift in Sources */,
D8D029282064441600CC01C6 /* AutoLockSettingsView.swift in Sources */,
D831FEE0204C92CD00EAE19A /* SettingListPresenter.swift in Sources */,
4C7F3C7D20B3C32A00C4C414 /* UIButton+.swift in Sources */,
Expand Down
3 changes: 3 additions & 0 deletions buddybuild_prebuild.sh
@@ -0,0 +1,3 @@
#!/usr/bin/env bash
echo "Setting SentryDSN to $SENTRY_DSN"
/usr/libexec/PlistBuddy -c "Set SentryDSN $SENTRY_DSN" "lockbox-ios/Common/Resources/Info.plist"
39 changes: 39 additions & 0 deletions lockbox-ios/Common/Helpers/SentryManager.swift
@@ -0,0 +1,39 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

import Foundation
import Sentry

class Sentry {
private let SentryDSNKey = "SentryDSN"

public static let shared = Sentry()

init() {
}

func setup(sendUsageData: Bool) {
if isSimulator() || !sendUsageData {
return
}

guard let dsn = Bundle.main.object(forInfoDictionaryKey: SentryDSNKey) as? String, !dsn.isEmpty else {
return
}

do {
Client.shared = try Client(dsn: dsn)
try Client.shared?.startCrashHandler()

// https://docs.sentry.io/clients/cocoa/advanced/#breadcrumbs
Client.shared?.enableAutomaticBreadcrumbTracking()
} catch let error {
print("\(error)")
}
}

private func isSimulator() -> Bool {
return ProcessInfo.processInfo.environment["SIMULATOR_ROOT"] != nil
}
}
2 changes: 2 additions & 0 deletions lockbox-ios/Common/Resources/Info.plist
Expand Up @@ -33,6 +33,8 @@
<string>$(DEVELOPMENT_TEAM)</string>
<key>NSFaceIDUsageDescription</key>
<string>Firefox Lockbox will use Face ID to sign you in to the app.</string>
<key>SentryDSN</key>
<string>$(SENTRY_DSN)</string>
<key>UIAppFonts</key>
<array>
<string>FiraSans-Bold.ttf</string>
Expand Down
14 changes: 13 additions & 1 deletion lockbox-ios/Presenter/RootPresenter.swift
Expand Up @@ -51,6 +51,7 @@ class RootPresenter {
fileprivate let lifecycleStore: LifecycleStore
fileprivate let telemetryActionHandler: TelemetryActionHandler
fileprivate let biometryManager: BiometryManager
fileprivate let sentryManager: Sentry

var fxa: FirefoxAccount?

Expand All @@ -63,7 +64,8 @@ class RootPresenter {
userDefaultStore: UserDefaultStore = .shared,
lifecycleStore: LifecycleStore = .shared,
telemetryActionHandler: TelemetryActionHandler = TelemetryActionHandler.shared,
biometryManager: BiometryManager = BiometryManager()
biometryManager: BiometryManager = BiometryManager(),
sentryManager: Sentry = Sentry.shared
) {
self.view = view
self.dispatcher = dispatcher
Expand All @@ -75,6 +77,7 @@ class RootPresenter {
self.lifecycleStore = lifecycleStore
self.telemetryActionHandler = telemetryActionHandler
self.biometryManager = biometryManager
self.sentryManager = sentryManager

// todo: update tests with populated oauth and profile info
Observable.combineLatest(self.accountStore.oauthInfo, self.accountStore.profile)
Expand Down Expand Up @@ -120,6 +123,7 @@ class RootPresenter {
self.dispatcher.dispatch(action: OnboardingStatusAction(onboardingInProgress: false))
self.startTelemetry()
self.startAdjust()
self.startSentry()
}

func onViewReady() {
Expand Down Expand Up @@ -281,4 +285,12 @@ extension RootPresenter {
Adjust.setEnabled(enabled)
}).disposed(by: self.disposeBag)
}

fileprivate func startSentry() {
self.userDefaultStore.recordUsageData
.take(1)
.subscribe(onNext: { enabled in
self.sentryManager.setup(sendUsageData: enabled)
}).disposed(by: self.disposeBag)
}
}
41 changes: 41 additions & 0 deletions lockbox-iosTests/RootPresenterSpec.swift
Expand Up @@ -179,6 +179,16 @@ class RootPresenterSpec: QuickSpec {
}
}

class FakeSentryManager: Sentry {
var setupCalled: Bool = false

override func setup(sendUsageData: Bool) {
if sendUsageData {
self.setupCalled = true
}
}
}

private var view: FakeRootView!
private var dispatcher: FakeDispatcher!
private var routeStore: FakeRouteStore!
Expand All @@ -188,6 +198,7 @@ class RootPresenterSpec: QuickSpec {
private var userDefaultStore: FakeUserDefaultStore!
private var telemetryActionHandler: FakeTelemetryActionHandler!
private var biometryManager: FakeBiometryManager!
private var sentryManager: FakeSentryManager!
private let scheduler = TestScheduler(initialClock: 0)
var subject: RootPresenter!

Expand All @@ -203,6 +214,7 @@ class RootPresenterSpec: QuickSpec {
self.userDefaultStore = FakeUserDefaultStore()
self.telemetryActionHandler = FakeTelemetryActionHandler()
self.biometryManager = FakeBiometryManager()
self.sentryManager = FakeSentryManager()
self.telemetryActionHandler.telemetryListener = self.scheduler.createObserver(TelemetryAction.self)
self.biometryManager.deviceAuthAvailableStub = true

Expand Down Expand Up @@ -1082,6 +1094,35 @@ class RootPresenterSpec: QuickSpec {
}
}
}
describe("usage") {
describe("when usage data can be recorded") {
beforeEach {
self.userDefaultStore.recordUsageStub.onNext(true)
self.sentryManager.setup(sendUsageData: true)
}

it("sends data to Sentry") {
expect(self.sentryManager.setupCalled).to(equal(true))
}
it("sends data to Adjust") {
// TODO: add Adjust spec
}
}

describe("when usage data cannot be recorded") {
beforeEach {
self.userDefaultStore.recordUsageStub.onNext(false)
self.sentryManager.setup(sendUsageData: false)
}

it("does not send data to Sentry") {
expect(self.sentryManager.setupCalled).to(equal(false))
}
it("does not send data to Adjust") {
// TODO: add Adjust spec
}
}
}
}
}
}
Expand Down

0 comments on commit af919c2

Please sign in to comment.