From c4a3dbf9456e7d101abdab8c5d4d2d7f9d62e523 Mon Sep 17 00:00:00 2001 From: louiszawadzki Date: Thu, 7 Sep 2023 15:36:31 +0200 Subject: [PATCH] Add tests for telemetry override --- .../ios/Sources/DdSdkImplementation.swift | 23 ++-- packages/core/ios/Tests/DdSdkTests.swift | 120 +++++------------- 2 files changed, 45 insertions(+), 98 deletions(-) diff --git a/packages/core/ios/Sources/DdSdkImplementation.swift b/packages/core/ios/Sources/DdSdkImplementation.swift index 7c470adc6..087d56609 100644 --- a/packages/core/ios/Sources/DdSdkImplementation.swift +++ b/packages/core/ios/Sources/DdSdkImplementation.swift @@ -79,27 +79,28 @@ public class DdSdkImplementation: NSObject { let sdkConfig = self.buildSDKConfiguration(configuration: sdkConfiguration) let consent = self.buildTrackingConsent(consent: sdkConfiguration.trackingConsent) - Datadog.initialize(with: sdkConfig, trackingConsent: consent) + let core = Datadog.initialize(with: sdkConfig, trackingConsent: consent) - self.enableFeatures(sdkConfiguration: sdkConfiguration) - self.sendConfigurationAsTelemetry(rnConfiguration: sdkConfiguration) + self.enableFeatures(sdkConfiguration: sdkConfiguration, core: core) self.startJSRefreshRateMonitoring(sdkConfiguration: sdkConfiguration) resolve(nil) } } - func enableFeatures(sdkConfiguration: DdSdkConfiguration) { - let rumConfig = self.buildRUMConfiguration(configuration: sdkConfiguration) - RUM.enable(with: rumConfig) + func enableFeatures(sdkConfiguration: DdSdkConfiguration, core: DatadogCoreProtocol) { + let rumConfig = buildRUMConfiguration(configuration: sdkConfiguration) + RUM.enable(with: rumConfig, in: core) - Logs.enable(with: Logs.Configuration()) + Logs.enable(with: Logs.Configuration(), in: core) - Trace.enable(with: Trace.Configuration()) + Trace.enable(with: Trace.Configuration(), in: core) if sdkConfiguration.nativeCrashReportEnabled ?? false { - CrashReporting.enable() + CrashReporting.enable(in: core) } + + overrideReactNativeTelemetry(rnConfiguration: sdkConfiguration, core: core) } @objc @@ -155,9 +156,9 @@ public class DdSdkImplementation: NSObject { resolve(nil) } - func sendConfigurationAsTelemetry(rnConfiguration: DdSdkConfiguration) -> Void { + func overrideReactNativeTelemetry(rnConfiguration: DdSdkConfiguration, core: DatadogCoreProtocol) -> Void { // TODO: missing some keys: initializationType, reactVersion, reactNativeVersion, trackNativeErrors - let telemetry = TelemetryCore(core: CoreRegistry.default) + let telemetry = TelemetryCore(core: core) telemetry.configuration( trackCrossPlatformLongTasks: rnConfiguration.longTaskThresholdMs != 0, trackErrors: rnConfiguration.configurationForTelemetry?.trackErrors, diff --git a/packages/core/ios/Tests/DdSdkTests.swift b/packages/core/ios/Tests/DdSdkTests.swift index 12057bc4c..4e03c606c 100644 --- a/packages/core/ios/Tests/DdSdkTests.swift +++ b/packages/core/ios/Tests/DdSdkTests.swift @@ -152,15 +152,13 @@ internal class DdSdkTests: XCTestCase { func testEnableAllFeatures() { let core = MockDatadogCore() - CoreRegistry.register(default: core) let configuration: DdSdkConfiguration = .mockAny() - DdSdkImplementation().enableFeatures(sdkConfiguration: configuration) + DdSdkImplementation().enableFeatures(sdkConfiguration: configuration, core: core) XCTAssertNotNil(core.features[RUMFeature.name]) XCTAssertNotNil(core.features[LogsFeature.name]) XCTAssertNotNil(core.features[TraceFeature.name]) - CoreRegistry.unregisterDefault() } func testBuildConfigurationDefaultEndpoint() { @@ -272,35 +270,29 @@ internal class DdSdkTests: XCTestCase { func testBuildConfigurationNoCrashReportByDefault() { let core = MockDatadogCore() - CoreRegistry.register(default: core) let configuration: DdSdkConfiguration = .mockAny(nativeCrashReportEnabled: nil) - DdSdkImplementation().enableFeatures(sdkConfiguration: configuration) + DdSdkImplementation().enableFeatures(sdkConfiguration: configuration, core: core) XCTAssertNil(core.features[CrashReportingFeature.name]) - CoreRegistry.unregisterDefault() } func testBuildConfigurationNoCrashReport() { let core = MockDatadogCore() - CoreRegistry.register(default: core) let configuration: DdSdkConfiguration = .mockAny(nativeCrashReportEnabled: false) - DdSdkImplementation().enableFeatures(sdkConfiguration: configuration) + DdSdkImplementation().enableFeatures(sdkConfiguration: configuration, core: core) XCTAssertNil(core.features[CrashReportingFeature.name]) - CoreRegistry.unregisterDefault() } func testBuildConfigurationWithCrashReport() { let core = MockDatadogCore() - CoreRegistry.register(default: core) let configuration: DdSdkConfiguration = .mockAny(nativeCrashReportEnabled: true) - DdSdkImplementation().enableFeatures(sdkConfiguration: configuration) + DdSdkImplementation().enableFeatures(sdkConfiguration: configuration, core: core) XCTAssertNotNil(core.features[CrashReportingFeature.name]) - CoreRegistry.unregisterDefault() } func testBuildConfigurationWithVersionSuffix() { @@ -722,79 +714,26 @@ internal class DdSdkTests: XCTestCase { XCTAssertEqual(ddConfig.trackBackgroundEvents, false) } - func testConfigurationTelemetryEventMapper() throws { - //TODO: rewrite this test -// DdSdkImplementation( -// mainDispatchQueue: DispatchQueueMock(), -// jsRefreshRateMonitor: JSRefreshRateMonitor()) -// .initialize( -// configuration: .mockAny( -// nativeCrashReportEnabled: false, -// nativeLongTaskThresholdMs: 0.0, -// longTaskThresholdMs: 0.1, -// configurationForTelemetry: ["initializationType": "LEGACY", "trackErrors": true, "trackInteractions": true, "trackNetworkRequests": true, "reactVersion": "18.2.0", "reactNativeVersion": "0.71.0"] -// ), -// resolve: mockResolve, -// reject: mockReject -// ) -// -// -// guard let configurationEventMapper = try XCTUnwrap(DD.telemetry as? RUMTelemetry).configurationEventMapper else { return } -// -// let mappedEvent = configurationEventMapper( -// TelemetryConfigurationEvent( -// dd: TelemetryConfigurationEvent.DD(), -// action: nil, -// application: nil, -// date: Int64(), -// experimentalFeatures: nil, -// service: "mockService", -// session: nil, -// source: .reactNative, -// telemetry: TelemetryConfigurationEvent.Telemetry( -// configuration: TelemetryConfigurationEvent.Telemetry.Configuration( -// actionNameAttribute: nil, -// batchSize: nil, -// batchUploadFrequency: nil, -// forwardConsoleLogs: nil, -// forwardErrorsToLogs: nil, -// forwardReports: nil, -// premiumSampleRate: nil, -// replaySampleRate: nil, -// selectedTracingPropagators: nil, -// sessionSampleRate: nil, -// silentMultipleInit: nil, -// telemetryConfigurationSampleRate: nil, -// telemetrySampleRate: nil, -// traceSampleRate: nil, -// trackSessionAcrossSubdomains: nil, -// useAllowedTracingOrigins: nil, -// useAllowedTracingUrls: nil, -// useBeforeSend: nil, -// useCrossSiteSessionCookie: nil, -// useExcludedActivityUrls: nil, -// useLocalEncryption: nil, -// useSecureSessionCookie: nil, -// useTracing: nil, -// viewTrackingStrategy: nil -// ) -// ), -// version: "1.0.0", -// view: nil -// ) -// ) -// -// XCTAssertEqual(mappedEvent.telemetry.configuration.initializationType, "LEGACY") -// XCTAssertEqual(mappedEvent.telemetry.configuration.trackErrors, true) -// XCTAssertEqual(mappedEvent.telemetry.configuration.trackInteractions, true) -// XCTAssertEqual(mappedEvent.telemetry.configuration.trackNetworkRequests, true) -// XCTAssertEqual(mappedEvent.telemetry.configuration.trackNativeErrors, false) -// XCTAssertEqual(mappedEvent.telemetry.configuration.trackNativeLongTasks, false) -// XCTAssertEqual(mappedEvent.telemetry.configuration.trackLongTask, true) -// XCTAssertEqual(mappedEvent.telemetry.configuration.reactVersion, "18.2.0") -// XCTAssertEqual(mappedEvent.telemetry.configuration.reactNativeVersion, "0.71.0") -// -// Datadog.internalFlushAndDeinitialize() + func testConfigurationTelemetryOverride() throws { + let core = MockDatadogCore() + let configuration: DdSdkConfiguration = .mockAny( + nativeCrashReportEnabled: false, + nativeLongTaskThresholdMs: 0.0, + longTaskThresholdMs: 0.1, + configurationForTelemetry: ["initializationType": "LEGACY", "trackErrors": true, "trackInteractions": true, "trackNetworkRequests": true, "reactVersion": "18.2.0", "reactNativeVersion": "0.71.0"] + ) + + DdSdkImplementation().overrideReactNativeTelemetry(rnConfiguration: configuration, core: core) + +// XCTAssertEqual(mockTelemetry.configuration?.initializationType, "LEGACY") + XCTAssertEqual(core.configuration?.trackErrors, true) + XCTAssertEqual(core.configuration?.trackInteractions, true) + XCTAssertEqual(core.configuration?.trackNetworkRequests, true) +// XCTAssertEqual(core.configuration?.trackNativeErrors, false) + XCTAssertEqual(core.configuration?.trackNativeLongTasks, false) + XCTAssertEqual(core.configuration?.trackLongTask, true) +// XCTAssertEqual(core.configuration?.reactVersion, "18.2.0") +// XCTAssertEqual(core.configuration?.reactNativeVersion, "0.71.0") } func testDropsResourceMarkedAsDropped() throws { @@ -932,8 +871,15 @@ extension NSDictionary { } internal class MockDatadogCore: DatadogCoreProtocol { - func send(message: DatadogInternal.FeatureMessage, else fallback: @escaping () -> Void) {} - + func send(message: FeatureMessage, else fallback: @escaping () -> Void) { + if // Configuration Telemetry Message + case .telemetry(let telemetry) = message, + case .configuration(let configuration) = telemetry { + self.configuration = configuration + } + } + + private(set) var configuration: ConfigurationTelemetry? private(set) var features: [String: DatadogFeature] = [:] func register(feature: T) throws where T : DatadogFeature {