Skip to content

Commit

Permalink
Move ProcessInfo out of MobileDevice and into AppContext
Browse files Browse the repository at this point in the history
Also move configuration manipulation into FeaturesConfiguration where that is possible.
  • Loading branch information
fuzzybinary committed Dec 6, 2021
1 parent 59bb278 commit 186f16b
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 55 deletions.
13 changes: 10 additions & 3 deletions Sources/Datadog/Core/FeaturesConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -152,15 +152,21 @@ extension FeaturesConfiguration {
let source = (configuration.additionalConfiguration[CrossPlatformAttributes.ddsource] as? String) ?? Datadog.Constants.ddsource
let sdkVersion = (configuration.additionalConfiguration[CrossPlatformAttributes.sdkVersion] as? String) ?? __sdkVersion

let debugOverride = appContext.processInfo.arguments.contains(Datadog.LaunchArguments.Debug)
if debugOverride {
consolePrint("⚠️ Overriding sampling, verbosity, and upload frequency due to \(Datadog.LaunchArguments.Debug) launch argument")
Datadog.verbosityLevel = .debug
}

let common = Common(
applicationName: appContext.bundleName ?? appContext.bundleType.rawValue,
applicationVersion: appContext.bundleVersion ?? "0.0.0",
applicationBundleIdentifier: appContext.bundleIdentifier ?? "unknown",
serviceName: configuration.serviceName ?? appContext.bundleIdentifier ?? "ios",
environment: try ifValid(environment: configuration.environment),
performance: PerformancePreset(
batchSize: configuration.batchSize,
uploadFrequency: configuration.uploadFrequency,
batchSize: debugOverride ? .small : configuration.batchSize,
uploadFrequency: debugOverride ? .frequent : configuration.uploadFrequency,
bundleType: appContext.bundleType
),
source: source,
Expand Down Expand Up @@ -205,7 +211,7 @@ extension FeaturesConfiguration {
uploadURL: try ifValid(endpointURLString: rumEndpoint.url),
clientToken: try ifValid(clientToken: configuration.clientToken),
applicationID: rumApplicationID,
sessionSamplingRate: configuration.rumSessionsSamplingRate,
sessionSamplingRate: debugOverride ? 100.0 : configuration.rumSessionsSamplingRate,
viewEventMapper: configuration.rumViewEventMapper,
resourceEventMapper: configuration.rumResourceEventMapper,
actionEventMapper: configuration.rumActionEventMapper,
Expand All @@ -215,6 +221,7 @@ extension FeaturesConfiguration {
backgroundEventTrackingEnabled: configuration.rumBackgroundEventTrackingEnabled,
onSessionStart: configuration.rumSessionsListener
)

} else {
let error = ProgrammerError(
description: """
Expand Down
7 changes: 0 additions & 7 deletions Sources/Datadog/Core/System/MobileDevice.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ internal class MobileDevice {
let model: String
let osName: String
let osVersion: String
let processInfo: ProcessInfo

// MARK: - Battery status monitoring

Expand Down Expand Up @@ -41,15 +40,13 @@ internal class MobileDevice {
model: String,
osName: String,
osVersion: String,
processInfo: ProcessInfo,
enableBatteryStatusMonitoring: @escaping () -> Void,
resetBatteryStatusMonitoring: @escaping () -> Void,
currentBatteryStatus: @escaping () -> BatteryStatus
) {
self.model = model
self.osName = osName
self.osVersion = osVersion
self.processInfo = processInfo
self.enableBatteryStatusMonitoring = enableBatteryStatusMonitoring
self.resetBatteryStatusMonitoring = resetBatteryStatusMonitoring
self.currentBatteryStatus = currentBatteryStatus
Expand All @@ -66,7 +63,6 @@ internal class MobileDevice {
model: uiDevice.model,
osName: uiDevice.systemName,
osVersion: uiDevice.systemVersion,
processInfo: processInfo,
enableBatteryStatusMonitoring: { uiDevice.isBatteryMonitoringEnabled = true },
resetBatteryStatusMonitoring: { uiDevice.isBatteryMonitoringEnabled = wasBatteryMonitoringEnabled },
currentBatteryStatus: {
Expand All @@ -93,16 +89,13 @@ internal class MobileDevice {
model: UIDevice.current.model,
osName: UIDevice.current.systemName,
osVersion: UIDevice.current.systemVersion,
processInfo: ProcessInfo.processInfo,
enableBatteryStatusMonitoring: {},
resetBatteryStatusMonitoring: {},
currentBatteryStatus: { BatteryStatus(state: .full, level: 1, isLowPowerModeEnabled: false) }
)
#endif
}

static var current = MobileDevice()

private static func toBatteryState(_ uiDeviceBatteryState: UIDevice.BatteryState) -> BatteryStatus.State {
switch uiDeviceBatteryState {
case .unknown: return .unknown
Expand Down
31 changes: 11 additions & 20 deletions Sources/Datadog/Datadog.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,34 @@ public class Datadog {
internal let bundleVersion: String?
/// Executable name (i.e. application name or app extension name)
internal let bundleName: String?
/// Process info
internal let processInfo: ProcessInfo

public init(mainBundle: Bundle = Bundle.main) {
public init(mainBundle: Bundle = Bundle.main, processInfo: ProcessInfo = ProcessInfo.processInfo) {
let bundleVersion = mainBundle.object(forInfoDictionaryKey: "CFBundleVersion") as? String
let bundleShortVersion = mainBundle.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String

self.init(
bundleType: mainBundle.bundlePath.hasSuffix(".appex") ? .iOSAppExtension : .iOSApp,
bundleIdentifier: mainBundle.bundleIdentifier,
bundleVersion: bundleShortVersion ?? bundleVersion,
bundleName: mainBundle.object(forInfoDictionaryKey: "CFBundleExecutable") as? String
bundleName: mainBundle.object(forInfoDictionaryKey: "CFBundleExecutable") as? String,
processInfo: processInfo
)
}

internal init(
bundleType: BundleType,
bundleIdentifier: String?,
bundleVersion: String?,
bundleName: String?
bundleName: String?,
processInfo: ProcessInfo
) {
self.bundleType = bundleType
self.bundleIdentifier = bundleIdentifier
self.bundleVersion = bundleVersion
self.bundleName = bundleName
self.processInfo = processInfo
}
}

Expand Down Expand Up @@ -70,36 +75,22 @@ public class Datadog {
trackingConsent: TrackingConsent,
configuration: Configuration
) {
var mutableConfiguration = configuration

// TODO: RUMM-511 remove this warning
#if targetEnvironment(macCatalyst)
consolePrint("⚠️ Catalyst is not officially supported by Datadog SDK: some features may NOT be functional!")
#endif

let mobileDevice = MobileDevice.current
let debugOverride = mobileDevice.processInfo.arguments.contains(LaunchArguments.Debug)
if debugOverride {
consolePrint("⚠️ Overriding sampling, verbosity, and upload frequency due to \(LaunchArguments.Debug) launch argument")
let rebuilder = Configuration.Builder(configuration: mutableConfiguration)
.set(rumSessionsSamplingRate: 100)
.set(batchSize: .small)
.set(uploadFrequency: .frequent)
mutableConfiguration = rebuilder.build()
Datadog.verbosityLevel = .debug
}

do {
try initializeOrThrow(
initialTrackingConsent: trackingConsent,
configuration: try FeaturesConfiguration(
configuration: mutableConfiguration,
configuration: configuration,
appContext: appContext
)
)

// Now that RUM is potentially initialized, override the debugRUM value
let debugRumOverride = mobileDevice.processInfo.arguments.contains(LaunchArguments.DebugRUM)
let debugRumOverride = appContext.processInfo.arguments.contains(LaunchArguments.DebugRUM)
if debugRumOverride {
consolePrint("⚠️ Overriding RUM debugging due to \(LaunchArguments.DebugRUM) launch argument")
Datadog.debugRUM = true
Expand Down Expand Up @@ -220,7 +211,7 @@ public class Datadog {
consentProvider: consentProvider,
performance: configuration.common.performance,
httpClient: HTTPClient(proxyConfiguration: configuration.common.proxyConfiguration),
mobileDevice: MobileDevice.current,
mobileDevice: MobileDevice(),
dateProvider: dateProvider,
dateCorrector: dateCorrector,
userInfoProvider: userInfoProvider,
Expand Down
5 changes: 0 additions & 5 deletions Sources/Datadog/DatadogConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -345,11 +345,6 @@ extension Datadog {
)
}

/// Used internally to rebuild a configuration based on launch arguments
internal init(configuration: Configuration) {
self.configuration = configuration
}

/// Sets the Datadog server endpoint where data is sent.
///
/// If set, it will override values set by any of these deprecated APIs:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ import UIKit
class MobileDeviceTests: XCTestCase {
private let notificationCenter = NotificationCenter()

func testWhenRunningOnMobile_itReturnsDevice() {
XCTAssertNotNil(MobileDevice.current)
}

func testWhenRunningOnMobile_itUsesUIDeviceInfo() {
let uiDevice = UIDeviceMock(
model: "model mock",
Expand Down
18 changes: 6 additions & 12 deletions Tests/DatadogTests/Datadog/DatadogTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ class DatadogTests: XCTestCase {
}

override func tearDown() {
MobileDevice.current = .init()

consolePrint = { print($0) }
printFunction = nil
XCTAssertFalse(Datadog.isInitialized)
Expand Down Expand Up @@ -337,18 +335,16 @@ class DatadogTests: XCTestCase {
arguments: [Datadog.LaunchArguments.Debug]
)

MobileDevice.current = MobileDevice.mockWith(
processInfo: mockProcessInfo
)

let configuration = rumBuilder
.set(uploadFrequency: .rare)
.set(rumSessionsSamplingRate: 20.0)
.set(batchSize: .medium)
.build()

Datadog.initialize(
appContext: .mockAny(),
appContext: .mockWith(
processInfo: mockProcessInfo
),
trackingConsent: .pending,
configuration: configuration
)
Expand All @@ -373,15 +369,13 @@ class DatadogTests: XCTestCase {
arguments: [Datadog.LaunchArguments.DebugRUM]
)

MobileDevice.current = MobileDevice.mockWith(
processInfo: mockProcessInfo
)

let configuration = rumBuilder
.build()

Datadog.initialize(
appContext: .mockAny(),
appContext: .mockWith(
processInfo: mockProcessInfo
),
trackingConsent: .pending,
configuration: configuration
)
Expand Down
8 changes: 4 additions & 4 deletions Tests/DatadogTests/Datadog/Mocks/CoreMocks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -328,13 +328,15 @@ extension AppContext {
bundleType: BundleType = .iOSApp,
bundleIdentifier: String? = .mockAny(),
bundleVersion: String? = .mockAny(),
bundleName: String? = .mockAny()
bundleName: String? = .mockAny(),
processInfo: ProcessInfo = ProcessInfoMock()
) -> AppContext {
return AppContext(
bundleType: bundleType,
bundleIdentifier: bundleIdentifier,
bundleVersion: bundleVersion,
bundleName: bundleName
bundleName: bundleName,
processInfo: processInfo
)
}
}
Expand Down Expand Up @@ -786,7 +788,6 @@ extension MobileDevice {
model: String = .mockAny(),
osName: String = .mockAny(),
osVersion: String = .mockAny(),
processInfo: ProcessInfo = ProcessInfoMock(),
enableBatteryStatusMonitoring: @escaping () -> Void = {},
resetBatteryStatusMonitoring: @escaping () -> Void = {},
currentBatteryStatus: @escaping () -> BatteryStatus = { .mockAny() }
Expand All @@ -795,7 +796,6 @@ extension MobileDevice {
model: model,
osName: osName,
osVersion: osVersion,
processInfo: processInfo,
enableBatteryStatusMonitoring: enableBatteryStatusMonitoring,
resetBatteryStatusMonitoring: resetBatteryStatusMonitoring,
currentBatteryStatus: currentBatteryStatus
Expand Down

0 comments on commit 186f16b

Please sign in to comment.