Skip to content

Commit

Permalink
RUMM-2606 Make launchTime optional
Browse files Browse the repository at this point in the history
  • Loading branch information
maxep committed Oct 24, 2022
1 parent f4d4fef commit fd55617
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 25 deletions.
11 changes: 7 additions & 4 deletions Sources/Datadog/DatadogCore/Context/LaunchTimePublisher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -23,12 +23,15 @@ internal struct LaunchTimePublisher: ContextValuePublisher {
)
}

func publish(to receiver: @escaping ContextValueReceiver<LaunchTime>) {
func publish(to receiver: @escaping ContextValueReceiver<LaunchTime?>) {
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)
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/Datadog/DatadogInternal/Context/DatadogContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 2 additions & 8 deletions Sources/Datadog/DatadogInternal/Context/LaunchTime.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
13 changes: 7 additions & 6 deletions Sources/Datadog/RUM/RUMMonitor/Scopes/RUMViewScope.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class LaunchTimePublisherTests: XCTestCase {
let launchTime = publisher.initialValue

// Then
XCTAssertNotNil(launchTime.launchDate)
XCTAssertNotNil(launchTime?.launchDate)
}

func testThreadSafety() {
Expand Down Expand Up @@ -49,7 +49,7 @@ class LaunchTimePublisherTests: XCTestCase {
let publisher = LaunchTimePublisher()

// Then
XCTAssertTrue(publisher.initialValue.isActivePrewarm)
XCTAssertTrue(publisher.initialValue?.isActivePrewarm ?? false)
}

func testIsActivePrewarm_returnsFalse() {
Expand All @@ -60,6 +60,6 @@ class LaunchTimePublisherTests: XCTestCase {
let publisher = LaunchTimePublisher()

// Then
XCTAssertFalse(publisher.initialValue.isActivePrewarm)
XCTAssertFalse(publisher.initialValue?.isActivePrewarm ?? true)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -196,7 +200,7 @@ class RUMViewScopeTests: XCTestCase {
var context = self.context
context.launchTime = .init(
launchTime: 2,
launchDate: nil,
launchDate: .distantPast,
isActivePrewarm: true
)

Expand Down

0 comments on commit fd55617

Please sign in to comment.