Skip to content

Commit

Permalink
Merge pull request #679 from DataDog/jward/RUM-1755-debug-launch-argu…
Browse files Browse the repository at this point in the history
…ments

🚩RUM-1755 Add config overrides for debug launch arguments
  • Loading branch information
fuzzybinary authored Dec 6, 2021
2 parents 8e7c19e + 2c437f4 commit 1b56d0b
Show file tree
Hide file tree
Showing 10 changed files with 113 additions and 20 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ instrumented-tests/LICENSE
venv
*.pyc
__pycache__
*.swp
10 changes: 10 additions & 0 deletions Datadog/Datadog.xcodeproj/xcshareddata/xcschemes/Example.xcscheme
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@
ReferencedContainer = "container:Datadog.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
<CommandLineArguments>
<CommandLineArgument
argument = "DD_DEBUG"
isEnabled = "NO">
</CommandLineArgument>
<CommandLineArgument
argument = "DD_DEBUG_RUM"
isEnabled = "NO">
</CommandLineArgument>
</CommandLineArguments>
<EnvironmentVariables>
<EnvironmentVariable
key = "DD_TEST_SCENARIO_CLASS_NAME"
Expand Down
12 changes: 9 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 @@ -199,7 +205,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 Down
9 changes: 4 additions & 5 deletions Sources/Datadog/Core/System/MobileDevice.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,19 +74,18 @@ internal class MobileDevice {
}
)
}
/// Returns current mobile device if `UIDevice` is available on this platform.
/// On other platforms returns `nil`.
static var current: MobileDevice {

convenience init() {
#if !targetEnvironment(simulator)
// Real device
return MobileDevice(
self.init(
uiDevice: UIDevice.current,
processInfo: ProcessInfo.processInfo,
notificationCenter: .default
)
#else
// iOS Simulator - battery monitoring doesn't work on Simulator, so return "always OK" value
return MobileDevice(
self.init(
model: UIDevice.current.model,
osName: UIDevice.current.systemName,
osVersion: UIDevice.current.systemVersion,
Expand Down
25 changes: 21 additions & 4 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 @@ -74,6 +79,7 @@ public class Datadog {
#if targetEnvironment(macCatalyst)
consolePrint("⚠️ Catalyst is not officially supported by Datadog SDK: some features may NOT be functional!")
#endif

do {
try initializeOrThrow(
initialTrackingConsent: trackingConsent,
Expand All @@ -82,6 +88,13 @@ public class Datadog {
appContext: appContext
)
)

// Now that RUM is potentially initialized, override the debugRUM value
let debugRumOverride = appContext.processInfo.arguments.contains(LaunchArguments.DebugRUM)
if debugRumOverride {
consolePrint("⚠️ Overriding RUM debugging due to \(LaunchArguments.DebugRUM) launch argument")
Datadog.debugRUM = true
}
} catch {
consolePrint("\(error)")
}
Expand Down Expand Up @@ -140,6 +153,10 @@ public class Datadog {
}

// MARK: - Internal
internal struct LaunchArguments {
static let Debug = "DD_DEBUG"
static let DebugRUM = "DD_DEBUG_RUM"
}

internal static var instance: Datadog?

Expand Down Expand Up @@ -194,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
2 changes: 1 addition & 1 deletion Tests/DatadogBenchmarkTests/BenchmarkMocks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ extension FeaturesCommonDependencies {
consentProvider: ConsentProvider(initialConsent: .granted),
performance: .benchmarksPreset,
httpClient: HTTPClient(),
mobileDevice: .current,
mobileDevice: MobileDevice(),
dateProvider: SystemDateProvider(),
dateCorrector: DateCorrectorMock(),
userInfoProvider: UserInfoProvider(),
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
58 changes: 58 additions & 0 deletions Tests/DatadogTests/Datadog/DatadogTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class DatadogTests: XCTestCase {

override func setUp() {
super.setUp()

XCTAssertFalse(Datadog.isInitialized)
printFunction = PrintFunctionMock()
consolePrint = printFunction.print
Expand Down Expand Up @@ -329,6 +330,63 @@ class DatadogTests: XCTestCase {
}
}

func testSupplyingDebugLaunchArgument_itOverridesUserSettings() {
let mockProcessInfo = ProcessInfoMock(
arguments: [Datadog.LaunchArguments.Debug]
)

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

Datadog.initialize(
appContext: .mockWith(
processInfo: mockProcessInfo
),
trackingConsent: .pending,
configuration: configuration
)

let expectedPerformancePreset = PerformancePreset(
batchSize: .small,
uploadFrequency: .frequent,
bundleType: .iOSApp
)
XCTAssertEqual(RUMFeature.instance?.configuration.sessionSamplingRate, 100)
XCTAssertEqual(TracingFeature.instance?.configuration.common.performance, expectedPerformancePreset)
XCTAssertEqual(LoggingFeature.instance?.configuration.common.performance, expectedPerformancePreset)
XCTAssertEqual(Datadog.verbosityLevel, .debug)

// Clear default verbosity after this test
Datadog.verbosityLevel = nil
Datadog.flushAndDeinitialize()
}

func testSupplyingRumDebugLaunchArgument_itSetsRumDebug() {
let mockProcessInfo = ProcessInfoMock(
arguments: [Datadog.LaunchArguments.DebugRUM]
)

let configuration = rumBuilder
.build()

Datadog.initialize(
appContext: .mockWith(
processInfo: mockProcessInfo
),
trackingConsent: .pending,
configuration: configuration
)

XCTAssertTrue(Datadog.debugRUM)

// Clear debug after test
Datadog.debugRUM = false
Datadog.flushAndDeinitialize()
}

// MARK: - Public APIs

func testTrackingConsent() {
Expand Down
6 changes: 4 additions & 2 deletions Tests/DatadogTests/Datadog/Mocks/CoreMocks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -330,13 +330,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
Original file line number Diff line number Diff line change
Expand Up @@ -408,12 +408,16 @@ extension URLRequest: AnyMockable {

class ProcessInfoMock: ProcessInfo {
private var _isLowPowerModeEnabled: Bool
private var _arguments: [String]

init(isLowPowerModeEnabled: Bool = .mockAny()) {
init(isLowPowerModeEnabled: Bool = .mockAny(), arguments: [String] = []) {
_isLowPowerModeEnabled = isLowPowerModeEnabled
_arguments = arguments
}

override var isLowPowerModeEnabled: Bool { _isLowPowerModeEnabled }

override var arguments: [String] { _arguments }
}

// MARK: - URLSession
Expand Down

0 comments on commit 1b56d0b

Please sign in to comment.