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

RUMM-1765 Update CrashContext with necessary info for handling App Launch crashes - part 3 #690

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions Datadog/Datadog.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,8 @@
619E16D82577C1CB00B2516B /* DataProcessor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 619E16D72577C1CB00B2516B /* DataProcessor.swift */; };
619E16E92578E73E00B2516B /* DataMigrator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 619E16E82578E73E00B2516B /* DataMigrator.swift */; };
619E16F12578E89700B2516B /* DeleteAllDataMigrator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 619E16F02578E89700B2516B /* DeleteAllDataMigrator.swift */; };
61A614E8276B2BD000A06CE7 /* RUMOffViewEventsHandlingRule.swift in Sources */ = {isa = PBXBuildFile; fileRef = 61A614E7276B2BD000A06CE7 /* RUMOffViewEventsHandlingRule.swift */; };
61A614EA276B9D4C00A06CE7 /* RUMOffViewEventsHandlingRuleTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 61A614E9276B9D4C00A06CE7 /* RUMOffViewEventsHandlingRuleTests.swift */; };
61A763DC252DB2B3005A23F2 /* NSURLSessionBridge.m in Sources */ = {isa = PBXBuildFile; fileRef = 61A763DB252DB2B3005A23F2 /* NSURLSessionBridge.m */; };
61A9238E256FCAA2009B9667 /* DateCorrectionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 61A9238D256FCAA2009B9667 /* DateCorrectionTests.swift */; };
61AADBDD263C7ECF008ABC6F /* EquatableInTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 61AADBDC263C7ECF008ABC6F /* EquatableInTests.swift */; };
Expand Down Expand Up @@ -523,8 +525,8 @@
D24C27EA270C8BEE005DE596 /* DataCompression.swift in Sources */ = {isa = PBXBuildFile; fileRef = D24C27E9270C8BEE005DE596 /* DataCompression.swift */; };
D2791EF927170A760046E07A /* RUMSwiftUIScenarioTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D2791EF827170A760046E07A /* RUMSwiftUIScenarioTests.swift */; };
D29889C9273413ED00A4D1A9 /* RUMViewsHandlerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D29889C72734136200A4D1A9 /* RUMViewsHandlerTests.swift */; };
D2EFF3D32731822A00D09F33 /* RUMViewsHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = D2EFF3D22731822A00D09F33 /* RUMViewsHandler.swift */; };
D29D5A4D273BF8B400A687C1 /* SwiftUIActionModifier.swift in Sources */ = {isa = PBXBuildFile; fileRef = D29D5A4C273BF8B400A687C1 /* SwiftUIActionModifier.swift */; };
D2EFF3D32731822A00D09F33 /* RUMViewsHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = D2EFF3D22731822A00D09F33 /* RUMViewsHandler.swift */; };
D2F1B81126D795F3009F3293 /* DDNoopRUMMonitor.swift in Sources */ = {isa = PBXBuildFile; fileRef = D2F1B81026D795F3009F3293 /* DDNoopRUMMonitor.swift */; };
D2F1B81326D8DA68009F3293 /* DDNoopRUMMonitorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D2F1B81226D8DA68009F3293 /* DDNoopRUMMonitorTests.swift */; };
D2F1B81526D8E5FF009F3293 /* DDNoopTracerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D2F1B81426D8E5FF009F3293 /* DDNoopTracerTests.swift */; };
Expand Down Expand Up @@ -983,6 +985,8 @@
619E16D72577C1CB00B2516B /* DataProcessor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DataProcessor.swift; sourceTree = "<group>"; };
619E16E82578E73E00B2516B /* DataMigrator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DataMigrator.swift; sourceTree = "<group>"; };
619E16F02578E89700B2516B /* DeleteAllDataMigrator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DeleteAllDataMigrator.swift; sourceTree = "<group>"; };
61A614E7276B2BD000A06CE7 /* RUMOffViewEventsHandlingRule.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RUMOffViewEventsHandlingRule.swift; sourceTree = "<group>"; };
61A614E9276B9D4C00A06CE7 /* RUMOffViewEventsHandlingRuleTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RUMOffViewEventsHandlingRuleTests.swift; sourceTree = "<group>"; };
61A763D9252DB2B3005A23F2 /* DatadogTests-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "DatadogTests-Bridging-Header.h"; sourceTree = "<group>"; };
61A763DA252DB2B3005A23F2 /* NSURLSessionBridge.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = NSURLSessionBridge.h; sourceTree = "<group>"; };
61A763DB252DB2B3005A23F2 /* NSURLSessionBridge.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = NSURLSessionBridge.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1192,8 +1196,8 @@
D24C27E9270C8BEE005DE596 /* DataCompression.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DataCompression.swift; sourceTree = "<group>"; };
D2791EF827170A760046E07A /* RUMSwiftUIScenarioTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RUMSwiftUIScenarioTests.swift; sourceTree = "<group>"; };
D29889C72734136200A4D1A9 /* RUMViewsHandlerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RUMViewsHandlerTests.swift; sourceTree = "<group>"; };
D2EFF3D22731822A00D09F33 /* RUMViewsHandler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RUMViewsHandler.swift; sourceTree = "<group>"; };
D29D5A4C273BF8B400A687C1 /* SwiftUIActionModifier.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SwiftUIActionModifier.swift; sourceTree = "<group>"; };
D2EFF3D22731822A00D09F33 /* RUMViewsHandler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RUMViewsHandler.swift; sourceTree = "<group>"; };
D2F1B81026D795F3009F3293 /* DDNoopRUMMonitor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DDNoopRUMMonitor.swift; sourceTree = "<group>"; };
D2F1B81226D8DA68009F3293 /* DDNoopRUMMonitorTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DDNoopRUMMonitorTests.swift; sourceTree = "<group>"; };
D2F1B81426D8E5FF009F3293 /* DDNoopTracerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DDNoopTracerTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2488,6 +2492,7 @@
isa = PBXGroup;
children = (
61C1510C25AC8C1B00362D4B /* RUMViewIdentityTests.swift */,
61A614E9276B9D4C00A06CE7 /* RUMOffViewEventsHandlingRuleTests.swift */,
617B953F24BF4DB300E6F443 /* RUMApplicationScopeTests.swift */,
61C2C20824C0C75500C0321C /* RUMSessionScopeTests.swift */,
6198D27024C6E3B700493501 /* RUMViewScopeTests.swift */,
Expand Down Expand Up @@ -2797,6 +2802,7 @@
isa = PBXGroup;
children = (
61FF9A4425AC5DEA001058CC /* RUMViewIdentity.swift */,
61A614E7276B2BD000A06CE7 /* RUMOffViewEventsHandlingRule.swift */,
61C3E63D24BF1B91008053F2 /* RUMApplicationScope.swift */,
61C2C20624C098FC00C0321C /* RUMSessionScope.swift */,
61C2C21124C5951400C0321C /* RUMViewScope.swift */,
Expand Down Expand Up @@ -4075,6 +4081,7 @@
61133BE72423979B00786299 /* LogUtilityOutputs.swift in Sources */,
61133BDA2423979B00786299 /* RequestBuilder.swift in Sources */,
61C3E63924BF19B4008053F2 /* RUMContext.swift in Sources */,
61A614E8276B2BD000A06CE7 /* RUMOffViewEventsHandlingRule.swift in Sources */,
61ED39D426C2A36B002C0F26 /* DataUploadStatus.swift in Sources */,
61133BE82423979B00786299 /* LogFileOutput.swift in Sources */,
61133BD72423979B00786299 /* DataUploadWorker.swift in Sources */,
Expand Down Expand Up @@ -4192,6 +4199,7 @@
6184751526EFCF1300C7C9C5 /* DatadogTestsObserver.swift in Sources */,
61133C602423990D00786299 /* RequestBuilderTests.swift in Sources */,
61133C572423990D00786299 /* FileReaderTests.swift in Sources */,
61A614EA276B9D4C00A06CE7 /* RUMOffViewEventsHandlingRuleTests.swift in Sources */,
D2F1B81326D8DA68009F3293 /* DDNoopRUMMonitorTests.swift in Sources */,
61133C5F2423990D00786299 /* DataUploaderTests.swift in Sources */,
61D6FF7924E42A2900D0E375 /* DataUploadWorkerMock.swift in Sources */,
Expand Down
13 changes: 13 additions & 0 deletions Sources/Datadog/Core/System/AppStateListener.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,16 @@ internal struct AppStateHistory: Equatable {
}
}

/// An observer of `AppStateHistory` value.
internal typealias AppStateHistoryObserver = ValueObserver
maxep marked this conversation as resolved.
Show resolved Hide resolved

/// Provides history of app foreground / background states.
internal protocol AppStateListening: AnyObject {
/// Last published `AppStateHistory`.
var history: AppStateHistory { get }

/// Subscribers observer to be notified on `AppStateHistory` changes.
func subscribe<Observer: AppStateHistoryObserver>(_ subscriber: Observer) where Observer.ObservedValue == AppStateHistory
}

internal class AppStateListener: AppStateListening {
Expand Down Expand Up @@ -144,4 +151,10 @@ internal class AppStateListener: AppStateListening {
value.changes.append(Snapshot(isActive: true, date: now))
publisher.publishAsync(value)
}

// MARK: - Managing Subscribers

func subscribe<Observer: AppStateHistoryObserver>(_ subscriber: Observer) where Observer.ObservedValue == AppStateHistory {
publisher.subscribe(subscriber)
}
}
16 changes: 15 additions & 1 deletion Sources/Datadog/CrashReporting/CrashContext/CrashContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,17 @@ internal struct CrashContext: Codable {
lastUserInfo: UserInfo,
lastRUMViewEvent: RUMEvent<RUMViewEvent>?,
lastNetworkConnectionInfo: NetworkConnectionInfo?,
lastCarrierInfo: CarrierInfo?
lastCarrierInfo: CarrierInfo?,
lastRUMSessionState: RUMSessionState?,
lastIsAppInForeground: Bool
) {
self.codableTrackingConsent = .init(from: lastTrackingConsent)
self.codableLastUserInfo = .init(from: lastUserInfo)
self.codableLastRUMViewEvent = lastRUMViewEvent.flatMap { .init(from: $0) }
self.codableLastNetworkConnectionInfo = lastNetworkConnectionInfo.flatMap { .init(from: $0) }
self.codableLastCarrierInfo = lastCarrierInfo.flatMap { .init(from: $0) }
self.lastRUMSessionState = lastRUMSessionState
self.lastIsAppInForeground = lastIsAppInForeground
}

// MARK: - Codable values
Expand All @@ -42,6 +46,8 @@ internal struct CrashContext: Codable {
case codableLastUserInfo = "lui"
case codableLastNetworkConnectionInfo = "lni"
case codableLastCarrierInfo = "lci"
case lastRUMSessionState = "rst"
case lastIsAppInForeground = "aif"
}

// MARK: - Setters & Getters using managed types
Expand Down Expand Up @@ -70,6 +76,14 @@ internal struct CrashContext: Codable {
set { codableLastCarrierInfo = newValue.flatMap { CodableCarrierInfo(from: $0) } }
get { codableLastCarrierInfo?.managedValue }
}

// MARK: - Direct Codable values

/// State of the last RUM session in crashed app process.
var lastRUMSessionState: RUMSessionState?

/// The last _"Is app in foreground?"_ information from crashed app process.
var lastIsAppInForeground: Bool
}

// MARK: - Bridging managed types to Codable representation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,26 @@ internal class CrashContextProvider: CrashContextProviderType {
self.unsafeCrashContext.lastRUMViewEvent = newValue
}

/// Updates `CrashContext` with last RUM session state.
private lazy var rumSessionStateUpdater = ContextValueUpdater<RUMSessionState?>(queue: queue) { newValue in
self.unsafeCrashContext.lastRUMSessionState = newValue
}

/// Updates `CrashContext` with last app foreground / background state information.
private lazy var isAppInForegroundUpdater = ContextValueUpdater<AppStateHistory>(queue: queue) { newValue in
self.unsafeCrashContext.lastIsAppInForeground = newValue.currentState.isActive
}

// MARK: - Initializer

init(
consentProvider: ConsentProvider,
userInfoProvider: UserInfoProvider,
networkConnectionInfoProvider: NetworkConnectionInfoProviderType,
carrierInfoProvider: CarrierInfoProviderType,
rumViewEventProvider: ValuePublisher<RUMEvent<RUMViewEvent>?>
rumViewEventProvider: ValuePublisher<RUMEvent<RUMViewEvent>?>,
rumSessionStateProvider: ValuePublisher<RUMSessionState?>,
appStateListener: AppStateListening
) {
self.queue = DispatchQueue(
label: "com.datadoghq.crash-context",
Expand All @@ -80,9 +92,11 @@ internal class CrashContextProvider: CrashContextProviderType {
self.unsafeCrashContext = CrashContext(
lastTrackingConsent: consentProvider.currentValue,
lastUserInfo: userInfoProvider.value,
lastRUMViewEvent: nil,
lastRUMViewEvent: rumViewEventProvider.currentValue,
lastNetworkConnectionInfo: networkConnectionInfoProvider.current,
lastCarrierInfo: carrierInfoProvider.current
lastCarrierInfo: carrierInfoProvider.current,
lastRUMSessionState: rumSessionStateProvider.currentValue,
lastIsAppInForeground: appStateListener.history.currentState.isActive
)

// Subscribe for context updates
Expand All @@ -91,6 +105,8 @@ internal class CrashContextProvider: CrashContextProviderType {
networkConnectionInfoProvider.subscribe(networkConnectionInfoUpdater)
carrierInfoProvider.subscribe(carrierInfoUpdater)
rumViewEventProvider.subscribe(rumViewEventUpdater)
rumSessionStateProvider.subscribe(rumSessionStateUpdater)
appStateListener.subscribe(isAppInForegroundUpdater)
}

// MARK: - CrashContextProviderType
Expand Down
4 changes: 3 additions & 1 deletion Sources/Datadog/CrashReporting/CrashReporter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ internal class CrashReporter {
userInfoProvider: crashReportingFeature.userInfoProvider,
networkConnectionInfoProvider: crashReportingFeature.networkConnectionInfoProvider,
carrierInfoProvider: crashReportingFeature.carrierInfoProvider,
rumViewEventProvider: crashReportingFeature.rumViewEventProvider
rumViewEventProvider: crashReportingFeature.rumViewEventProvider,
rumSessionStateProvider: crashReportingFeature.rumSessionStateProvider,
appStateListener: crashReportingFeature.appStateListener
),
loggingOrRUMIntegration: availableLoggingOrRUMIntegration
)
Expand Down
10 changes: 9 additions & 1 deletion Sources/Datadog/CrashReporting/CrashReportingFeature.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,13 @@ internal final class CrashReportingFeature {
/// Publishes recent `CarrierInfo` value so it can be persisted in `CrashContext`.
let carrierInfoProvider: CarrierInfoProviderType
/// Publishes recent `RUMEvent<RUMViewEvent>` value so it can be persisted in `CrashContext`.
/// It will provide `nil` until first view is tracked.
let rumViewEventProvider: ValuePublisher<RUMEvent<RUMViewEvent>?>
/// Publishes recent RUM session state so it can be persisted in `CrashContext`.
/// It will be used to decide if and how to track crashes which happen while there was no active view.
let rumSessionStateProvider: ValuePublisher<RUMSessionState?>
/// Publishes changes to app "foreground" / "background" state.
let appStateListener: AppStateListening

init(
configuration: FeaturesConfiguration.CrashReporting,
Expand All @@ -40,7 +46,9 @@ internal final class CrashReportingFeature {
self.userInfoProvider = commonDependencies.userInfoProvider
self.networkConnectionInfoProvider = commonDependencies.networkConnectionInfoProvider
self.carrierInfoProvider = commonDependencies.carrierInfoProvider
self.rumViewEventProvider = ValuePublisher(initialValue: nil)
self.rumViewEventProvider = ValuePublisher(initialValue: nil) // `nil` by default, because there cannot be any RUM view at this ponit
self.rumSessionStateProvider = ValuePublisher(initialValue: nil) // `nil` by default, because there cannot be any RUM session at this ponit
self.appStateListener = commonDependencies.appStateListener
}

#if DD_SDK_COMPILED_FOR_TESTING
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,38 @@

import Foundation

/// Updates `CrashContext` passed to crash reporter with the last `RUMViewEvent`.
/// Updates `CrashContext` passed to crash reporter with the last RUM view and RUM session state.
/// When the app restarts after crash, this event is used to create and send `RUMErrorEvent` describing the crash.
///
/// This integration isolates the direct link between RUM and Crash Reporting.
internal struct RUMWithCrashContextIntegration {
private weak var rumViewEventProvider: ValuePublisher<RUMEvent<RUMViewEvent>?>?
private weak var rumSessionStateProvider: ValuePublisher<RUMSessionState?>?

init?() {
if let crashReportingFeature = CrashReportingFeature.instance {
self.init(rumViewEventProvider: crashReportingFeature.rumViewEventProvider)
self.init(
rumViewEventProvider: crashReportingFeature.rumViewEventProvider,
rumSessionStateProvider: crashReportingFeature.rumSessionStateProvider
)
} else {
return nil
}
}

init(rumViewEventProvider: ValuePublisher<RUMEvent<RUMViewEvent>?>) {
init(
rumViewEventProvider: ValuePublisher<RUMEvent<RUMViewEvent>?>,
rumSessionStateProvider: ValuePublisher<RUMSessionState?>
) {
self.rumViewEventProvider = rumViewEventProvider
self.rumSessionStateProvider = rumSessionStateProvider
}

func update(lastRUMViewEvent: RUMEvent<RUMViewEvent>) {
rumViewEventProvider?.publishAsync(lastRUMViewEvent)
}

func update(lastRUMSessionState: RUMSessionState) {
rumSessionStateProvider?.publishAsync(lastRUMSessionState)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ internal struct RUMScopeDependencies {
let rumUUIDGenerator: RUMUUIDGenerator
/// Adjusts RUM events time (device time) to server time.
let dateCorrector: DateCorrectorType
/// Integration with Crash Reporting. It updates the crash context with RUM info.
/// `nil` if Crash Reporting feature is not enabled.
let crashContextIntegration: RUMWithCrashContextIntegration?

let vitalCPUReader: SamplingBasedVitalReader
let vitalMemoryReader: SamplingBasedVitalReader
Expand Down
Loading