-
Notifications
You must be signed in to change notification settings - Fork 37
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
Add support for uploading crash reports to Sentry #777
Merged
Merged
Changes from 17 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
4d6de38
Upload MetricKit crash diagnostics to crash collecting endpoing on iOS
miasma13 f9bb118
Remove first crash pixel param as it is obsolete now
miasma13 6d73102
Simplify pixel firing
miasma13 24584b5
Disable mock payload
miasma13 c73a94e
Use temporary crash collecting endpoint
miasma13 7742d8c
Parametrize User Agent
ayoy dcbd551
Make CrashReportSender available on macOS 11
ayoy 3062e9d
Pass payloads to showPromptIfCanSendCrashReport
ayoy 1d5cda0
Simplify CrashCollection callback
ayoy 3629ce1
Revert to production crash collection endpoint
ayoy 592068a
Add CrashCollectionPlatform.macOSAppStore
ayoy e55997d
Bring back 'first crash' parameter
ayoy db3c488
Make SwiftLint happy
ayoy f6ac4b5
Clean up the code
ayoy 21a9a11
Merge branch 'main' into dominik/sentry-appstore
ayoy e00c54b
Add CrashCollectionTests
ayoy 753db48
Fix a typo
ayoy 40f9082
Merge branch 'main' into dominik/sentry-appstore
ayoy f2beb83
Merge branch 'main' into dominik/sentry-appstore
ayoy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,61 +19,86 @@ | |
import Foundation | ||
import MetricKit | ||
|
||
public enum CrashCollectionPlatform { | ||
case iOS, macOS, macOSAppStore | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
var userAgent: String { | ||
switch self { | ||
case .iOS: | ||
return "ddg_ios" | ||
case .macOS: | ||
return "ddg_mac" | ||
case .macOSAppStore: | ||
return "ddg_mac_appstore" | ||
} | ||
} | ||
} | ||
|
||
@available(iOSApplicationExtension, unavailable) | ||
@available(iOS 13, macOS 12, *) | ||
public struct CrashCollection { | ||
|
||
// Need a strong reference | ||
static let collector = CrashCollector() | ||
public final class CrashCollection { | ||
|
||
public static func collectCrashesAsync(completion: @escaping ([String: String]) -> Void) { | ||
collector.completion = completion | ||
MXMetricManager.shared.add(collector) | ||
public var log: OSLog { | ||
getLog() | ||
} | ||
private let getLog: () -> OSLog | ||
|
||
final class CrashCollector: NSObject, MXMetricManagerSubscriber { | ||
public init(platform: CrashCollectionPlatform, log: @escaping @autoclosure () -> OSLog = .disabled) { | ||
self.getLog = log | ||
crashHandler = CrashHandler() | ||
crashSender = CrashReportSender(platform: platform, log: log()) | ||
} | ||
|
||
var completion: ([String: String]) -> Void = { _ in } | ||
public func start(_ didFindCrashReports: @escaping (_ pixelParameters: [[String: String]], | ||
_ payloads: [MXDiagnosticPayload], | ||
_ uploadReports: @escaping () -> Void) -> Void | ||
) { | ||
let first = isFirstCrash | ||
isFirstCrash = false | ||
|
||
func didReceive(_ payloads: [MXDiagnosticPayload]) { | ||
payloads | ||
.compactMap { $0.crashDiagnostics } | ||
crashHandler.crashDiagnosticsPayloadHandler = { payloads in | ||
let pixelParameters = payloads | ||
.compactMap(\.crashDiagnostics) | ||
.flatMap { $0 } | ||
.forEach { | ||
completion([ | ||
"appVersion": "\($0.applicationVersion).\($0.metaData.applicationBuildVersion)", | ||
"code": "\($0.exceptionCode ?? -1)", | ||
"type": "\($0.exceptionType ?? -1)", | ||
"signal": "\($0.signal ?? -1)" | ||
]) | ||
.map { diagnostic in | ||
var params = [ | ||
"appVersion": "\(diagnostic.applicationVersion).\(diagnostic.metaData.applicationBuildVersion)", | ||
"code": "\(diagnostic.exceptionCode ?? -1)", | ||
"type": "\(diagnostic.exceptionType ?? -1)", | ||
"signal": "\(diagnostic.signal ?? -1)" | ||
] | ||
if first { | ||
params["first"] = "1" | ||
} | ||
return params | ||
} | ||
|
||
didFindCrashReports(pixelParameters, payloads) { | ||
Task { | ||
for payload in payloads { | ||
await self.crashSender.send(payload.jsonRepresentation()) | ||
} | ||
} | ||
} | ||
} | ||
|
||
MXMetricManager.shared.add(crashHandler) | ||
} | ||
|
||
static let firstCrashKey = "CrashCollection.first" | ||
|
||
static var firstCrash: Bool { | ||
var isFirstCrash: Bool { | ||
get { | ||
UserDefaults().object(forKey: Self.firstCrashKey) as? Bool ?? true | ||
UserDefaults().object(forKey: Const.firstCrashKey) as? Bool ?? true | ||
} | ||
|
||
set { | ||
UserDefaults().set(newValue, forKey: Self.firstCrashKey) | ||
UserDefaults().set(newValue, forKey: Const.firstCrashKey) | ||
} | ||
} | ||
|
||
public static func start(firePixel: @escaping ([String: String]) -> Void) { | ||
let first = Self.firstCrash | ||
CrashCollection.collectCrashesAsync { params in | ||
var params = params | ||
if first { | ||
params["first"] = "1" | ||
} | ||
firePixel(params) | ||
} | ||
// Turn the flag off for next time | ||
Self.firstCrash = false | ||
} | ||
let crashHandler: CrashHandler | ||
let crashSender: CrashReportSender | ||
|
||
enum Const { | ||
static let firstCrashKey = "CrashCollection.first" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// | ||
// CrashHandler.swift | ||
// | ||
// Copyright © 2023 DuckDuckGo. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
import Foundation | ||
import MetricKit | ||
|
||
@available(iOSApplicationExtension, unavailable) | ||
@available(iOS 13, macOS 12, *) | ||
final class CrashHandler: NSObject, MXMetricManagerSubscriber { | ||
|
||
var crashDiagnosticsPayloadHandler: ([MXDiagnosticPayload]) -> Void = { _ in } | ||
|
||
func didReceive(_ payloads: [MXDiagnosticPayload]) { | ||
let payloadsWithCrash = payloads.filter { !($0.crashDiagnostics?.isEmpty ?? true) } | ||
crashDiagnosticsPayloadHandler(payloadsWithCrash) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// | ||
// CrashReportSender.swift | ||
// | ||
// Copyright © 2023 DuckDuckGo. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
import Foundation | ||
import MetricKit | ||
|
||
public final class CrashReportSender { | ||
|
||
static let reportServiceUrl = URL(string: "https://duckduckgo.com/crash.js")! | ||
|
||
public let platform: CrashCollectionPlatform | ||
|
||
public var log: OSLog { | ||
getLog() | ||
} | ||
private let getLog: () -> OSLog | ||
|
||
public init(platform: CrashCollectionPlatform, log: @escaping @autoclosure () -> OSLog = .disabled) { | ||
self.platform = platform | ||
getLog = log | ||
} | ||
|
||
public func send(_ crashReportData: Data) async { | ||
var request = URLRequest(url: Self.reportServiceUrl) | ||
request.setValue("text/plain", forHTTPHeaderField: "Content-Type") | ||
request.setValue(platform.userAgent, forHTTPHeaderField: "User-Agent") | ||
request.httpMethod = "POST" | ||
request.httpBody = crashReportData | ||
|
||
do { | ||
_ = try await session.data(for: request) | ||
} catch { | ||
assertionFailure("CrashReportSender: Failed to send the crash report") | ||
} | ||
} | ||
|
||
private let session = URLSession(configuration: .ephemeral) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// | ||
// CrashCollectionTests.swift | ||
// | ||
// Copyright © 2024 DuckDuckGo. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
@testable import Crashes | ||
import MetricKit | ||
import XCTest | ||
|
||
class CrashCollectionTests: XCTestCase { | ||
|
||
override func setUp() { | ||
super.setUp() | ||
clearUserDefaults() | ||
} | ||
|
||
override func tearDown() { | ||
super.tearDown() | ||
clearUserDefaults() | ||
} | ||
|
||
func testFirstCrashFlagSent() { | ||
let crashCollection = CrashCollection(platform: .iOS) | ||
// 2 pixels with first = true attached | ||
XCTAssertTrue(crashCollection.isFirstCrash) | ||
crashCollection.start { pixelParameters, _, _ in | ||
let firstFlags = pixelParameters.compactMap { $0["first"] } | ||
XCTAssertFalse(firstFlags.isEmpty) | ||
} | ||
crashCollection.crashHandler.didReceive([ | ||
MockPayload(mockCrashes: [ | ||
MXCrashDiagnostic(), | ||
MXCrashDiagnostic() | ||
]) | ||
]) | ||
XCTAssertFalse(crashCollection.isFirstCrash) | ||
} | ||
|
||
func testSubsequentPixelsDontSendFirstFlag() { | ||
let crashCollection = CrashCollection(platform: .iOS) | ||
// 2 pixels with no first parameter | ||
crashCollection.isFirstCrash = false | ||
crashCollection.start { pixelParameters, _, _ in | ||
let firstFlags = pixelParameters.compactMap { $0["first"] } | ||
XCTAssertTrue(firstFlags.isEmpty) | ||
} | ||
crashCollection.crashHandler.didReceive([ | ||
MockPayload(mockCrashes: [ | ||
MXCrashDiagnostic(), | ||
MXCrashDiagnostic() | ||
]) | ||
]) | ||
XCTAssertFalse(crashCollection.isFirstCrash) | ||
} | ||
|
||
private func clearUserDefaults() { | ||
UserDefaults().removeObject(forKey: CrashCollection.Const.firstCrashKey) | ||
} | ||
} | ||
|
||
class MockPayload: MXDiagnosticPayload { | ||
|
||
var mockCrashes: [MXCrashDiagnostic]? | ||
|
||
init(mockCrashes: [MXCrashDiagnostic]?) { | ||
self.mockCrashes = mockCrashes | ||
super.init() | ||
} | ||
|
||
required init?(coder: NSCoder) { | ||
fatalError("init(coder:) has not been implemented") | ||
} | ||
|
||
override var crashDiagnostics: [MXCrashDiagnostic]? { | ||
return mockCrashes | ||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's a single test case that I moved from iOS because it didn't depend on the iOS app at all.