Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions packages/codepush/__mocks__/react-native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ actualRN.NativeModules.DdSdk = {
telemetryError: jest.fn().mockImplementation(
() => new Promise<void>(resolve => resolve())
) as jest.MockedFunction<DdNativeSdkType['telemetryError']>,
consumeWebviewEvent: jest.fn().mockImplementation(
() => new Promise<void>(resolve => resolve())
) as jest.MockedFunction<DdNativeSdkType['consumeWebviewEvent']>,
clearAllData: jest.fn().mockImplementation(
() => new Promise<void>(resolve => resolve())
) as jest.MockedFunction<DdNativeSdkType['clearAllData']>
Expand Down
3 changes: 0 additions & 3 deletions packages/core/__mocks__/react-native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@ actualRN.NativeModules.DdSdk = {
telemetryError: jest.fn().mockImplementation(
() => new Promise<void>(resolve => resolve())
) as jest.MockedFunction<DdNativeSdkType['telemetryError']>,
consumeWebviewEvent: jest.fn().mockImplementation(
() => new Promise<void>(resolve => resolve())
) as jest.MockedFunction<DdNativeSdkType['consumeWebviewEvent']>,
clearAllData: jest.fn().mockImplementation(
() => new Promise<void>(resolve => resolve())
) as jest.MockedFunction<DdNativeSdkType['clearAllData']>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,17 +87,6 @@ internal class DatadogSDKWrapper : DatadogWrapper {
return field
}

// We use Kotlin backing field here to initialize once the telemetry proxy
// and make sure it is only after SDK is initialized.
private var webViewProxy: WebViewTracking._InternalWebViewProxy? = null
get() {
if (field == null && isInitialized()) {
field = WebViewTracking._InternalWebViewProxy(DatadogSDKWrapperStorage.getSdkCore())
}

return field
}

override fun setVerbosity(level: Int) {
Datadog.setVerbosity(level)
}
Expand Down Expand Up @@ -188,10 +177,6 @@ internal class DatadogSDKWrapper : DatadogWrapper {
telemetryProxy?.error(message, throwable)
}

override fun consumeWebviewEvent(message: String) {
webViewProxy?.consumeWebviewEvent(message)
}

override fun isInitialized(): Boolean {
return Datadog.isInitialized()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,6 @@ interface DatadogWrapper {
*/
fun telemetryError(message: String, throwable: Throwable?)

/**
* Sends Webview events.
*/
fun consumeWebviewEvent(message: String)

/**
* Returns whether the SDK is initialized.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,15 +169,6 @@ class DdSdkImplementation(
promise.resolve(null)
}

/**
* Sends WebView Events.
* @param message User action.
*/
fun consumeWebviewEvent(message: String, promise: Promise) {
datadog.consumeWebviewEvent(message)
promise.resolve(null)
}

/**
* Clears all data that has not already been sent to Datadog servers.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@
package com.datadog.reactnative

import android.app.Activity
import androidx.annotation.MainThread
import com.facebook.react.bridge.LifecycleEventListener
import com.facebook.react.bridge.Promise
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.ReactMethod
import com.facebook.react.bridge.ReadableMap
import com.facebook.react.modules.core.DeviceEventManagerModule

/** The entry point to initialize Datadog's features. */
class DdSdk(
Expand Down Expand Up @@ -119,15 +117,6 @@ class DdSdk(
implementation.telemetryError(message, stack, kind, promise)
}

/**
* Sends WebView Events.
* @param message User action.
*/
@ReactMethod
override fun consumeWebviewEvent(message: String, promise: Promise) {
implementation.consumeWebviewEvent(message, promise)
}

/**
* Clears all data that has not already been sent to Datadog servers.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,6 @@ class DdSdk(
implementation.telemetryError(message, stack, kind, promise)
}

/**
* Sends WebView Events.
* @param message User action.
*/
@ReactMethod
fun consumeWebviewEvent(message: String, promise: Promise) {
implementation.consumeWebviewEvent(message, promise)
}

/**
* Clears all data that has not already been sent to Datadog servers.
*/
Expand Down
145 changes: 10 additions & 135 deletions packages/core/ios/Sources/DatadogSDKWrapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,37 +10,25 @@ import DatadogRUM
import DatadogLogs
import DatadogTrace
import DatadogCrashReporting
import DatadogWebViewTracking
import DatadogInternal
import Foundation

public typealias OnCoreInitializedListener = (DatadogCoreProtocol) -> Void
public typealias OnSdkInitializedListener = () -> Void

/// Wrapper around the Datadog SDK. Use DatadogSDKWrapper.shared to access the instance.
public class DatadogSDKWrapper {
// Singleton
public static var shared = DatadogSDKWrapper()

// Initialization callbacks
internal var onCoreInitializedListeners: [OnCoreInitializedListener] = []
internal var loggerConfiguration = DatadogLogs.Logger.Configuration()
// Core instance
private var coreInstance: DatadogCoreProtocol? = nil
internal var onSdkInitializedListeners: [OnSdkInitializedListener] = []

private init() { }

public func addOnCoreInitializedListener(listener:@escaping OnCoreInitializedListener) {
onCoreInitializedListeners.append(listener)
}
internal private(set) var loggerConfiguration = DatadogLogs.Logger.Configuration()

/// This is intended for internal testing only.
public func setCoreInstance(core: DatadogCoreProtocol?) {
self.coreInstance = core
}
private init() { }

/// This is not supposed to be used in the SDK itself, rather by other SDKs like Session Replay.
public func getCoreInstance() -> DatadogCoreProtocol? {
return coreInstance
public func addOnSdkInitializedListener(listener:@escaping OnSdkInitializedListener) {
onSdkInitializedListeners.append(listener)
}

// SDK Wrapper
Expand All @@ -49,126 +37,13 @@ public class DatadogSDKWrapper {
loggerConfiguration: DatadogLogs.Logger.Configuration,
trackingConsent: TrackingConsent
) -> Void {
let core = Datadog.initialize(with: coreConfiguration, trackingConsent: trackingConsent)
setCoreInstance(core: core)
for listener in onCoreInitializedListeners {
listener(core)
}

self.loggerConfiguration = loggerConfiguration
}

internal func isInitialized() -> Bool {
return Datadog.isInitialized()
}

internal func clearAllData() -> Void {
if let core = coreInstance {
Datadog.clearAllData(in: core)
} else {
Datadog.clearAllData()
}
}

// Features
internal func enableRUM(with configuration: RUM.Configuration) {
if let core = coreInstance {
RUM.enable(with: configuration, in: core)
} else {
consolePrint("Core instance was not found when initializing RUM.", .critical)
}
}
Datadog.initialize(with: coreConfiguration, trackingConsent: trackingConsent)

internal func enableLogs(with configuration: Logs.Configuration) {
if let core = coreInstance {
Logs.enable(with: configuration, in: core)
} else {
consolePrint("Core instance was not found when initializing Logs.", .critical)
for listener in onSdkInitializedListeners {
listener()
}
}

internal func enableTrace(with configuration: Trace.Configuration) {
if let core = coreInstance {
Trace.enable(with: configuration, in: core)
} else {
consolePrint("Core instance was not found when initializing Trace.", .critical)
}
}

internal func enableCrashReporting() {
if let core = coreInstance {
CrashReporting.enable(in: core)
} else {
consolePrint("Core instance was not found when initializing CrashReporting.", .critical)
}
}

internal func createLogger() -> LoggerProtocol {
let core = coreInstance ?? {
consolePrint("Core instance was not found when creating Logger.", .critical)
return CoreRegistry.default
}()

return DatadogLogs.Logger.create(with: loggerConfiguration, in: core)
}

// Telemetry
internal func sendTelemetryLog(message: String, attributes: [String: any Encodable], config: [String: any Encodable]) {
if let core = coreInstance {
let id = (config["onlyOnce"] as? Bool) == true ? message : UUID().uuidString
core.telemetry.debug(id: id, message: message, attributes: attributes)
} else {
consolePrint("Core instance was not found when calling sendTelemetryLog.", .warn)
}
}

internal func telemetryDebug(id: String, message: String) {
return Datadog._internal.telemetry.debug(id: id, message: message)
}

internal func telemetryError(id: String, message: String, kind: String?, stack: String?) {
return Datadog._internal.telemetry.error(id: id, message: message, kind: kind, stack: stack)
}

internal func overrideTelemetryConfiguration(
initializationType: String? = nil,
reactNativeVersion: String? = nil,
reactVersion: String? = nil,
trackCrossPlatformLongTasks: Bool? = nil,
trackErrors: Bool? = nil,
trackInteractions: Bool? = nil,
trackLongTask: Bool? = nil,
trackNativeErrors: Bool? = nil,
trackNativeLongTasks: Bool? = nil,
trackNetworkRequests: Bool? = nil
) {
coreInstance?.telemetry.configuration(
initializationType: initializationType,
reactNativeVersion: reactNativeVersion,
reactVersion: reactVersion,
trackCrossPlatformLongTasks: trackCrossPlatformLongTasks,
trackErrors: trackErrors,
trackLongTask: trackLongTask,
trackNativeErrors: trackNativeErrors,
trackNativeLongTasks: trackNativeLongTasks,
trackNetworkRequests: trackNetworkRequests,
trackUserInteractions: trackInteractions
)
}

// Webview
private var webviewMessageEmitter: InternalExtension<WebViewTracking>.AbstractMessageEmitter?

internal func enableWebviewTracking() {
if let core = coreInstance {
webviewMessageEmitter = WebViewTracking._internal.messageEmitter(in: core)
} else {
consolePrint("Core instance was not found when initializing Webview tracking.", .critical)
}
}

internal func sendWebviewMessage(body: NSString) throws {
try self.webviewMessageEmitter?.send(body: body)
self.loggerConfiguration = loggerConfiguration
}
}

Expand Down
7 changes: 6 additions & 1 deletion packages/core/ios/Sources/DdLogsImplementation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
*/

import Foundation
import DatadogInternal
import DatadogLogs
import DatadogCore

@objc
public class DdLogsImplementation: NSObject {
Expand All @@ -20,7 +22,10 @@ public class DdLogsImplementation: NSObject {

@objc
public override convenience init() {
self.init({ DatadogSDKWrapper.shared.createLogger() }, { DatadogSDKWrapper.shared.isInitialized() })
self.init(
{ DatadogLogs.Logger.create(with: DatadogSDKWrapper.shared.loggerConfiguration, in: CoreRegistry.default) },
{ Datadog.isInitialized(instanceName: CoreRegistry.defaultInstanceName) }
)
}

@objc
Expand Down
11 changes: 0 additions & 11 deletions packages/core/ios/Sources/DdSdk.mm
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,6 @@ + (void)initFromNative {
[self telemetryError:message stack:stack kind:kind resolve:resolve reject:reject];
}

RCT_REMAP_METHOD(consumeWebviewEvent, withWebviewMessage:(NSString*)message
withResolver:(RCTPromiseResolveBlock)resolve
withRejecter:(RCTPromiseRejectBlock)reject)
{
[self consumeWebviewEvent:message resolve:resolve reject:reject];
}

RCT_REMAP_METHOD(clearAllData, withResolver:(RCTPromiseResolveBlock)resolve
withRejecter:(RCTPromiseRejectBlock)reject)
{
Expand Down Expand Up @@ -128,10 +121,6 @@ - (dispatch_queue_t)methodQueue {
return [RNQueue getSharedQueue];
}

- (void)consumeWebviewEvent:(NSString *)message resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject {
[self.ddSdkImplementation consumeWebviewEventWithMessage:message resolve:resolve reject:reject];
}

- (void)initialize:(NSDictionary *)configuration resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject {
[self.ddSdkImplementation initializeWithConfiguration:configuration resolve:resolve reject:reject];
}
Expand Down
Loading