-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for uploading crash reports to Sentry (#777)
Task/Issue URL: https://app.asana.com/0/72649045549333/1205862129634403/f Description: Add CrashReportSender (moved over from the macOS app) and update CrashCollection to support sending crashes reported by MetricKit to the backend.
- Loading branch information
Showing
5 changed files
with
248 additions
and
38 deletions.
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
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 | ||
} | ||
|
||
} |