-
Notifications
You must be signed in to change notification settings - Fork 133
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1687 from DataDog/ncreated/RUM-2925/record-the-st…
…ack-trace-of-app-hang RUM-2925 feat: Add backtrace generation capability to `DatadogCoreProtocol`
- Loading branch information
Showing
21 changed files
with
616 additions
and
183 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
63 changes: 63 additions & 0 deletions
63
Datadog/IntegrationUnitTests/CrashReporting/GeneratingBacktraceTests.swift
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,63 @@ | ||
/* | ||
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. | ||
* This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
* Copyright 2019-Present Datadog, Inc. | ||
*/ | ||
|
||
import XCTest | ||
import DatadogCrashReporting | ||
@testable import DatadogInternal | ||
|
||
/// Tests integration of `DatadogCore` and `DatadogCrashReporting` for backtrace generation. | ||
class GeneratingBacktraceTests: XCTestCase { | ||
private var core: DatadogCoreProxy! // swiftlint:disable:this implicitly_unwrapped_optional | ||
|
||
override func setUp() { | ||
super.setUp() | ||
core = DatadogCoreProxy(context: .mockWith(trackingConsent: .granted)) | ||
} | ||
|
||
override func tearDown() { | ||
core.flushAndTearDown() | ||
core = nil | ||
super.tearDown() | ||
} | ||
|
||
func testGivenCrashReportingIsEnabled_thenCoreCanGenerateBacktrace() throws { | ||
// Given | ||
CrashReporting.enable(in: core) | ||
XCTAssertNotNil(core.get(feature: BacktraceReportingFeature.self), "`BacktraceReportingFeature` is registered") | ||
|
||
// When | ||
let backtrace = try XCTUnwrap(core.backtraceReporter.generateBacktrace()) | ||
|
||
// Then | ||
XCTAssertGreaterThan(backtrace.threads.count, 0, "Some thread(s) should be recorded") | ||
XCTAssertGreaterThan(backtrace.binaryImages.count, 0, "Some binary image(s) should be recorded") | ||
|
||
XCTAssertTrue( | ||
backtrace.stack.contains("DatadogCoreTests"), | ||
"Backtrace stack should include at least one frame from `DatadogCoreTests` image" | ||
) | ||
XCTAssertTrue( | ||
backtrace.stack.contains("XCTest"), | ||
"Backtrace stack should include at least one frame from `XCTest` image" | ||
) | ||
#if os(iOS) | ||
XCTAssertTrue( | ||
backtrace.binaryImages.contains(where: { $0.libraryName == "DatadogCoreTests iOS" }), | ||
"Backtrace should include the image for `DatadogCoreTests iOS`" | ||
) | ||
#elseif os(tvOS) | ||
XCTAssertTrue( | ||
backtrace.binaryImages.contains(where: { $0.libraryName == "DatadogCoreTests tvOS" }), | ||
"Backtrace should include the image for `DatadogCoreTests tvOS`" | ||
) | ||
#endif | ||
XCTAssertTrue( | ||
// Assert on prefix as it is `XCTestCore` on iOS 15+ and `XCTest` earlier: | ||
backtrace.binaryImages.contains(where: { $0.libraryName.hasPrefix("XCTest") }), | ||
"Backtrace should include the image for `XCTest`" | ||
) | ||
} | ||
} |
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
71 changes: 71 additions & 0 deletions
71
DatadogCore/Tests/Datadog/DatadogCore/DatadogCore+FeatureDirectoriesTests.swift
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,71 @@ | ||
/* | ||
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. | ||
* This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
* Copyright 2019-Present Datadog, Inc. | ||
*/ | ||
|
||
import XCTest | ||
import DatadogInternal | ||
@testable import DatadogCore | ||
|
||
private struct RemoteFeatureMock: DatadogRemoteFeature { | ||
static let name: String = "remote-feature-mock" | ||
|
||
var requestBuilder: FeatureRequestBuilder = FeatureRequestBuilderMock() | ||
var messageReceiver: FeatureMessageReceiver = NOPFeatureMessageReceiver() | ||
} | ||
|
||
private struct FeatureMock: DatadogFeature { | ||
static let name: String = "feature-mock" | ||
|
||
var messageReceiver: FeatureMessageReceiver = NOPFeatureMessageReceiver() | ||
} | ||
|
||
class DatadogCore_FeatureDirectoriesTests: XCTestCase { | ||
private var core: DatadogCore! // swiftlint:disable:this implicitly_unwrapped_optional | ||
|
||
override func setUp() { | ||
super.setUp() | ||
temporaryCoreDirectory.create() | ||
core = DatadogCore( | ||
directory: temporaryCoreDirectory, | ||
dateProvider: SystemDateProvider(), | ||
initialConsent: .mockRandom(), | ||
performance: .mockRandom(), | ||
httpClient: HTTPClientMock(), | ||
encryption: nil, | ||
contextProvider: .mockAny(), | ||
applicationVersion: .mockAny(), | ||
maxBatchesPerUpload: .mockRandom(min: 1, max: 100), | ||
backgroundTasksEnabled: .mockAny() | ||
) | ||
} | ||
|
||
override func tearDown() { | ||
core.flushAndTearDown() | ||
core = nil | ||
temporaryCoreDirectory.delete() | ||
super.tearDown() | ||
} | ||
|
||
func testWhenRegisteringRemoteFeature_itCreatesFeatureDirectories() throws { | ||
// When | ||
try core.register(feature: RemoteFeatureMock()) | ||
|
||
// Then | ||
let featureDirectory = try temporaryCoreDirectory.coreDirectory.subdirectory(path: RemoteFeatureMock.name) | ||
XCTAssertNoThrow(try featureDirectory.subdirectory(path: "v2"), "Authorized data directory must exist") | ||
XCTAssertNoThrow(try featureDirectory.subdirectory(path: "intermediate-v2"), "Intermediate data directory must exist") | ||
} | ||
|
||
func testWhenRegisteringFeature_itDoesNotCreateFeatureDirectories() throws { | ||
// When | ||
try core.register(feature: FeatureMock()) | ||
|
||
// Then | ||
XCTAssertThrowsError( | ||
try temporaryCoreDirectory.coreDirectory.subdirectory(path: FeatureMock.name), | ||
"Feature directory must not exist" | ||
) | ||
} | ||
} |
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
20 changes: 20 additions & 0 deletions
20
DatadogCrashReporting/Sources/Integrations/BacktraceReporter.swift
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,20 @@ | ||
/* | ||
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. | ||
* This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
* Copyright 2019-Present Datadog, Inc. | ||
*/ | ||
|
||
import DatadogInternal | ||
|
||
internal struct BacktraceReporter: DatadogInternal.BacktraceReporting { | ||
let reporter: ThirdPartyCrashReporter | ||
|
||
func generateBacktrace() -> DatadogInternal.BacktraceReport? { | ||
do { | ||
return try reporter.generateBacktrace() | ||
} catch let error { | ||
DD.logger.error("Encountered an error when generating backtrace", error: error) | ||
return nil | ||
} | ||
} | ||
} |
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
Oops, something went wrong.