From fd556177f18a130e568fb78c19850b30a41f79ad Mon Sep 17 00:00:00 2001 From: Maxime Epain Date: Mon, 24 Oct 2022 14:55:04 +0200 Subject: [PATCH] RUMM-2606 Make launchTime optional --- .../DatadogCore/Context/LaunchTimePublisher.swift | 11 +++++++---- .../DatadogInternal/Context/DatadogContext.swift | 4 ++-- .../DatadogInternal/Context/LaunchTime.swift | 10 ++-------- .../RUM/RUMMonitor/Scopes/RUMViewScope.swift | 13 +++++++------ .../DatadogCore/LaunchTimePublisherTests.swift | 6 +++--- .../RUM/RUMMonitor/Scopes/RUMViewScopeTests.swift | 8 ++++++-- 6 files changed, 27 insertions(+), 25 deletions(-) diff --git a/Sources/Datadog/DatadogCore/Context/LaunchTimePublisher.swift b/Sources/Datadog/DatadogCore/Context/LaunchTimePublisher.swift index 04125657e6..2e3531ce2a 100644 --- a/Sources/Datadog/DatadogCore/Context/LaunchTimePublisher.swift +++ b/Sources/Datadog/DatadogCore/Context/LaunchTimePublisher.swift @@ -13,7 +13,7 @@ import _Datadog_Private internal struct LaunchTimePublisher: ContextValuePublisher { private typealias AppLaunchHandler = __dd_private_AppLaunchHandler - let initialValue: LaunchTime + let initialValue: LaunchTime? init() { initialValue = LaunchTime( @@ -23,12 +23,15 @@ internal struct LaunchTimePublisher: ContextValuePublisher { ) } - func publish(to receiver: @escaping ContextValueReceiver) { + func publish(to receiver: @escaping ContextValueReceiver) { + let launchDate = AppLaunchHandler.shared.launchDate + let isActivePrewarm = AppLaunchHandler.shared.isActivePrewarm + AppLaunchHandler.shared.setApplicationDidBecomeActiveCallback { launchTime in let value = LaunchTime( launchTime: launchTime, - launchDate: initialValue.launchDate, - isActivePrewarm: initialValue.isActivePrewarm + launchDate: launchDate, + isActivePrewarm: isActivePrewarm ) receiver(value) } diff --git a/Sources/Datadog/DatadogInternal/Context/DatadogContext.swift b/Sources/Datadog/DatadogInternal/Context/DatadogContext.swift index 3c6a9884dc..23f6198d8d 100644 --- a/Sources/Datadog/DatadogInternal/Context/DatadogContext.swift +++ b/Sources/Datadog/DatadogInternal/Context/DatadogContext.swift @@ -61,8 +61,8 @@ public struct DatadogContext { /// Application launch time. /// - /// Can be `zero` if the launch could not yet been evaluated. - var launchTime: LaunchTime = .zero + /// Can be `nil` if the launch could not yet been evaluated. + var launchTime: LaunchTime? /// Provides the history of app foreground / background states. var applicationStateHistory: AppStateHistory diff --git a/Sources/Datadog/DatadogInternal/Context/LaunchTime.swift b/Sources/Datadog/DatadogInternal/Context/LaunchTime.swift index 97b40b8c51..08249d9144 100644 --- a/Sources/Datadog/DatadogInternal/Context/LaunchTime.swift +++ b/Sources/Datadog/DatadogInternal/Context/LaunchTime.swift @@ -15,15 +15,9 @@ import Foundation /// time this value is provided, it will represent the time interval between now and the process start time. /* public */ let launchTime: TimeInterval? - /* public */ let launchDate: Date? + /// The date when the application process started. + /* public */ let launchDate: Date /// Returns `true` if the application is pre-warmed. /* public */ let isActivePrewarm: Bool } - -extension LaunchTime { - /// Returns a zero launch time with inactive pre-warm. - /* public */ static var zero: LaunchTime { - .init(launchTime: 0, launchDate: nil, isActivePrewarm: false) - } -} diff --git a/Sources/Datadog/RUM/RUMMonitor/Scopes/RUMViewScope.swift b/Sources/Datadog/RUM/RUMMonitor/Scopes/RUMViewScope.swift index 37f0faa917..240df78113 100644 --- a/Sources/Datadog/RUM/RUMMonitor/Scopes/RUMViewScope.swift +++ b/Sources/Datadog/RUM/RUMMonitor/Scopes/RUMViewScope.swift @@ -314,21 +314,22 @@ internal class RUMViewScope: RUMScope, RUMContextProvider { var attributes = self.attributes var loadingTime: Int64? = nil - if context.launchTime.isActivePrewarm { + if context.launchTime?.isActivePrewarm == true { // Set `active_pre_warm` attribute to true in case // of pre-warmed app. attributes[Constants.activePrewarm] = true - } else if let launchTime = context.launchTime.launchTime { + } else if let launchTime = context.launchTime?.launchTime { // Report Application Launch Time only if not pre-warmed loadingTime = launchTime.toInt64Nanoseconds - } else if let launchDate = context.launchTime.launchDate { + } else if let launchDate = context.launchTime?.launchDate { // The launchTime can be `nil` if the application is not yet // active (UIApplicationDidBecomeActiveNotification). That is - // the case when instrumenting a SwiftUI application that starts - // a RUM view on `SwiftUI.View.onAppear`. + // the case when instrumenting a SwiftUI application that start + // a RUM view on `SwiftUI.View/onAppear`. // // In that case, we consider the time between the application - // launch and the view start as the application loading time. + // launch and the first view start as the application loading + // time. loadingTime = viewStartTime.timeIntervalSince(launchDate).toInt64Nanoseconds } diff --git a/Tests/DatadogTests/Datadog/DatadogCore/LaunchTimePublisherTests.swift b/Tests/DatadogTests/Datadog/DatadogCore/LaunchTimePublisherTests.swift index a4e3023bff..e75788c4c8 100644 --- a/Tests/DatadogTests/Datadog/DatadogCore/LaunchTimePublisherTests.swift +++ b/Tests/DatadogTests/Datadog/DatadogCore/LaunchTimePublisherTests.swift @@ -21,7 +21,7 @@ class LaunchTimePublisherTests: XCTestCase { let launchTime = publisher.initialValue // Then - XCTAssertNotNil(launchTime.launchDate) + XCTAssertNotNil(launchTime?.launchDate) } func testThreadSafety() { @@ -49,7 +49,7 @@ class LaunchTimePublisherTests: XCTestCase { let publisher = LaunchTimePublisher() // Then - XCTAssertTrue(publisher.initialValue.isActivePrewarm) + XCTAssertTrue(publisher.initialValue?.isActivePrewarm ?? false) } func testIsActivePrewarm_returnsFalse() { @@ -60,6 +60,6 @@ class LaunchTimePublisherTests: XCTestCase { let publisher = LaunchTimePublisher() // Then - XCTAssertFalse(publisher.initialValue.isActivePrewarm) + XCTAssertFalse(publisher.initialValue?.isActivePrewarm ?? true) } } diff --git a/Tests/DatadogTests/Datadog/RUM/RUMMonitor/Scopes/RUMViewScopeTests.swift b/Tests/DatadogTests/Datadog/RUM/RUMMonitor/Scopes/RUMViewScopeTests.swift index 3ccda62a22..29e9c6fa48 100644 --- a/Tests/DatadogTests/Datadog/RUM/RUMMonitor/Scopes/RUMViewScopeTests.swift +++ b/Tests/DatadogTests/Datadog/RUM/RUMMonitor/Scopes/RUMViewScopeTests.swift @@ -88,7 +88,11 @@ class RUMViewScopeTests: XCTestCase { // Given let currentTime: Date = .mockDecember15th2019At10AMUTC() var context = self.context - context.launchTime = .init(launchTime: 2, launchDate: nil, isActivePrewarm: false) + context.launchTime = .init( + launchTime: 2, + launchDate: .distantPast, + isActivePrewarm: false + ) let scope = RUMViewScope( isInitialView: true, @@ -196,7 +200,7 @@ class RUMViewScopeTests: XCTestCase { var context = self.context context.launchTime = .init( launchTime: 2, - launchDate: nil, + launchDate: .distantPast, isActivePrewarm: true )