Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,14 @@
package com.datadog.reactnative

import android.content.Context
import android.util.Log
import com.datadog.android.Datadog
import com.datadog.android._InternalProxy
import com.datadog.android.api.InternalLogger
import com.datadog.android.api.SdkCore
import com.datadog.android.api.feature.FeatureSdkCore
import com.datadog.android.core.InternalSdkCore
import com.datadog.android.core.configuration.Configuration
import com.datadog.android.log.Logs
import com.datadog.android.log.LogsConfiguration
import com.datadog.android.privacy.TrackingConsent
import com.datadog.android.rum.GlobalRumMonitor
import com.datadog.android.rum.Rum
import com.datadog.android.rum.RumConfiguration
import com.datadog.android.rum.RumMonitor
import com.datadog.android.trace.Trace
import com.datadog.android.trace.TraceConfiguration
import com.datadog.android.webview.WebViewTracking
import com.facebook.react.bridge.ReadableMap

Expand All @@ -49,50 +40,18 @@ object DatadogSDKWrapperStorage {
listener(ddCore)
}
}

/**
* Sets instance of core SDK to be used to initialize features.
*/
fun setSdkCore(core: InternalSdkCore?) {
this.core = core
}

/**
* Returns the core set by setSdkCore or the default core instance by default.
*/
fun getSdkCore(): SdkCore {
core?.let {
return it
}
Log.d(
DatadogSDKWrapperStorage::class.java.canonicalName,
"SdkCore was not set in DatadogSDKWrapperStorage, using default instance."
)
return Datadog.getInstance()
}
}

internal class DatadogSDKWrapper : DatadogWrapper {
override var bundleLogsWithRum = DefaultConfiguration.bundleLogsWithRum
override var bundleLogsWithTraces = DefaultConfiguration.bundleLogsWithTraces

// We use Kotlin backing field here to initialize once the telemetry proxy
// and make sure it is only after SDK is initialized.
private var telemetryProxy: _InternalProxy._TelemetryProxy? = null
get() {
if (field == null && isInitialized()) {
field = Datadog._internalProxy()._telemetry
}

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())
field = WebViewTracking._InternalWebViewProxy(Datadog.getInstance())
}

return field
Expand All @@ -108,20 +67,7 @@ internal class DatadogSDKWrapper : DatadogWrapper {
consent: TrackingConsent
) {
val core = Datadog.initialize(context, configuration, consent)
DatadogSDKWrapperStorage.setSdkCore(core as InternalSdkCore)
DatadogSDKWrapperStorage.notifyOnInitializedListeners(core)
}

override fun enableRum(configuration: RumConfiguration) {
Rum.enable(configuration, DatadogSDKWrapperStorage.getSdkCore())
}

override fun enableLogs(configuration: LogsConfiguration) {
Logs.enable(configuration, DatadogSDKWrapperStorage.getSdkCore())
}

override fun enableTrace(configuration: TraceConfiguration) {
Trace.enable(configuration, DatadogSDKWrapperStorage.getSdkCore())
DatadogSDKWrapperStorage.notifyOnInitializedListeners(core as InternalSdkCore)
}

@Deprecated("Use setUserInfo instead; the user ID is now required.")
Expand Down Expand Up @@ -160,34 +106,6 @@ internal class DatadogSDKWrapper : DatadogWrapper {
Datadog.setTrackingConsent(trackingConsent)
}

override fun sendTelemetryLog(message: String, attributes: ReadableMap, config: ReadableMap) {
val core = DatadogSDKWrapperStorage.getSdkCore() as FeatureSdkCore?
val logger = core?.internalLogger;

val additionalProperties = attributes.toMap()
val telemetryConfig = config.toMap()

logger?.log(
level = InternalLogger.Level.INFO,
target = InternalLogger.Target.TELEMETRY,
messageBuilder = { message },
onlyOnce = (telemetryConfig["onlyOnce"] as? Boolean) ?: true,
additionalProperties = additionalProperties
)
}

override fun telemetryDebug(message: String) {
telemetryProxy?.debug(message)
}

override fun telemetryError(message: String, stack: String?, kind: String?) {
telemetryProxy?.error(message, stack, kind)
}

override fun telemetryError(message: String, throwable: Throwable?) {
telemetryProxy?.error(message, throwable)
}

override fun consumeWebviewEvent(message: String) {
webViewProxy?.consumeWebviewEvent(message)
}
Expand All @@ -197,11 +115,11 @@ internal class DatadogSDKWrapper : DatadogWrapper {
}

override fun getRumMonitor(): RumMonitor {
return GlobalRumMonitor.get(DatadogSDKWrapperStorage.getSdkCore())
return GlobalRumMonitor.get(Datadog.getInstance())
}

override fun clearAllData() {
return Datadog.clearAllData(DatadogSDKWrapperStorage.getSdkCore())
return Datadog.clearAllData(Datadog.getInstance())
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import com.facebook.react.bridge.ReadableMap
import java.lang.IllegalArgumentException

/**
* Wrapper around [Datadog].
* Wrapper around [com.datadog.android.Datadog].
*/
@Suppress("ComplexInterface", "TooManyFunctions")
interface DatadogWrapper {
Expand Down Expand Up @@ -49,10 +49,8 @@ interface DatadogWrapper {
/**
* Initializes the Datadog SDK.
* @param context your application context
* @param credentials your organization credentials
* @param configuration the configuration for the SDK library
* @param trackingConsent as the initial state of the tracking consent flag.
* @see [Credentials]
* @param consent as the initial state of the tracking consent flag.
* @see [Configuration]
* @see [TrackingConsent]
* @throws IllegalArgumentException if the env name is using illegal characters and your
Expand All @@ -64,33 +62,6 @@ interface DatadogWrapper {
consent: TrackingConsent
)

/**
* Enables the RUM feature of the SDK.
*
* @param configuration the configuration for the RUM feature
*/
fun enableRum(
configuration: RumConfiguration
)

/**
* Enables the Logs feature of the SDK.
*
* @param configuration the configuration for the Logs feature
*/
fun enableLogs(
configuration: LogsConfiguration
)

/**
* Enables the Trace feature of the SDK.
*
* @param configuration the configuration for the Trace feature
*/
fun enableTrace(
configuration: TraceConfiguration
)

/**
* Sets the user information.
*
Expand Down Expand Up @@ -126,7 +97,7 @@ interface DatadogWrapper {

/**
* Sets the user information.
* @param extraUserInfo: The additional information. (To set the id, name or email please user setUserInfo).
* @param extraInfo: The additional information. (To set the id, name or email please user setUserInfo).
*/
fun addUserExtraInfo(
extraInfo: Map<String, Any?>
Expand All @@ -144,27 +115,6 @@ interface DatadogWrapper {
*/
fun setTrackingConsent(trackingConsent: TrackingConsent)


/**
* Sends telemetry event with attributes.
*/
fun sendTelemetryLog(message: String, attributes: ReadableMap, config: ReadableMap)

/**
* Sends telemetry debug event.
*/
fun telemetryDebug(message: String)

/**
* Sends telemetry error.
*/
fun telemetryError(message: String, stack: String?, kind: String?)

/**
* Sends telemetry error.
*/
fun telemetryError(message: String, throwable: Throwable?)

/**
* Sends Webview events.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package com.datadog.reactnative

import android.util.Log as AndroidLog
import com.datadog.android.Datadog
import com.datadog.android.log.Logger
import com.facebook.react.bridge.Promise
import com.facebook.react.bridge.ReadableMap
Expand All @@ -22,7 +23,7 @@ class DdLogsImplementation(
val bundleLogsWithRum = datadog.bundleLogsWithRum
val bundleLogsWithTraces = datadog.bundleLogsWithTraces

logger ?: Logger.Builder(DatadogSDKWrapperStorage.getSdkCore())
logger ?: Logger.Builder(Datadog.getInstance())
.setLogcatLogsEnabled(true)
.setBundleWithRumEnabled(bundleLogsWithRum)
.setBundleWithTraceEnabled(bundleLogsWithTraces)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import java.util.concurrent.atomic.AtomicBoolean
class DdSdkImplementation(
private val reactContext: ReactApplicationContext,
private val datadog: DatadogWrapper = DatadogSDKWrapper(),
private val ddTelemetry: DdTelemetry = DdTelemetry(),
private val uiThreadExecutor: UiThreadExecutor = ReactUiThreadExecutor()
) {
internal val appContext: Context = reactContext.applicationContext
Expand All @@ -39,7 +40,7 @@ class DdSdkImplementation(
fun initialize(configuration: ReadableMap, promise: Promise) {
val ddSdkConfiguration = configuration.asDdSdkConfiguration()

val nativeInitialization = DdSdkNativeInitialization(appContext, datadog)
val nativeInitialization = DdSdkNativeInitialization(appContext, datadog, ddTelemetry)
nativeInitialization.initialize(ddSdkConfiguration)

this.frameRateProvider = createFrameRateProvider(ddSdkConfiguration)
Expand Down Expand Up @@ -145,7 +146,7 @@ class DdSdkImplementation(
* @param config Configuration object, can take 'onlyOnce: Boolean'
*/
fun sendTelemetryLog(message: String, attributes: ReadableMap, config: ReadableMap, promise: Promise) {
datadog.sendTelemetryLog(message, attributes, config)
ddTelemetry.sendTelemetryLog(message, attributes, config)
promise.resolve(null)
}

Expand All @@ -154,7 +155,7 @@ class DdSdkImplementation(
* @param message Debug message.
*/
fun telemetryDebug(message: String, promise: Promise) {
datadog.telemetryDebug(message)
ddTelemetry.telemetryDebug(message)
promise.resolve(null)
}

Expand All @@ -165,7 +166,7 @@ class DdSdkImplementation(
* @param kind Error kind.
*/
fun telemetryError(message: String, stack: String, kind: String, promise: Promise) {
datadog.telemetryError(message, stack, kind)
ddTelemetry.telemetryError(message, stack, kind)
promise.resolve(null)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,17 @@ package com.datadog.reactnative
import android.content.Context
import android.content.pm.PackageManager
import android.util.Log
import com.datadog.android.Datadog
import com.datadog.android.DatadogSite
import com.datadog.android.core.configuration.BatchProcessingLevel
import com.datadog.android.core.configuration.BatchSize
import com.datadog.android.core.configuration.Configuration
import com.datadog.android.core.configuration.UploadFrequency
import com.datadog.android.event.EventMapper
import com.datadog.android.log.Logs
import com.datadog.android.log.LogsConfiguration
import com.datadog.android.privacy.TrackingConsent
import com.datadog.android.rum.Rum
import com.datadog.android.rum.RumConfiguration
import com.datadog.android.rum._RumInternalProxy
import com.datadog.android.rum.configuration.VitalsUpdateFrequency
Expand All @@ -25,6 +28,7 @@ import com.datadog.android.rum.model.ActionEvent
import com.datadog.android.rum.model.ResourceEvent
import com.datadog.android.rum.tracking.ActivityViewTrackingStrategy
import com.datadog.android.telemetry.model.TelemetryConfigurationEvent
import com.datadog.android.trace.Trace
import com.datadog.android.trace.TraceConfiguration
import com.google.gson.Gson
import java.util.Locale
Expand All @@ -37,6 +41,7 @@ import kotlin.time.Duration.Companion.seconds
class DdSdkNativeInitialization internal constructor(
private val appContext: Context,
private val datadog: DatadogWrapper = DatadogSDKWrapper(),
private val ddTelemetry: DdTelemetry = DdTelemetry(),
private val jsonFileReader: JSONFileReader = JSONFileReader()
) {
internal fun initialize(ddSdkConfiguration: DdSdkConfiguration) {
Expand All @@ -59,11 +64,9 @@ class DdSdkNativeInitialization internal constructor(

datadog.initialize(appContext, sdkConfiguration, trackingConsent)

datadog.enableRum(rumConfiguration)

datadog.enableTrace(traceConfiguration)

datadog.enableLogs(logsConfiguration)
Rum.enable(rumConfiguration, Datadog.getInstance())
Logs.enable(logsConfiguration, Datadog.getInstance())
Trace.enable(traceConfiguration, Datadog.getInstance())
}

private fun configureRumAndTracesForLogs(configuration: DdSdkConfiguration) {
Expand Down Expand Up @@ -95,7 +98,7 @@ class DdSdkNativeInitialization internal constructor(
try {
appContext.packageManager.getPackageInfo(packageName, 0)
} catch (e: PackageManager.NameNotFoundException) {
datadog.telemetryError(e.message ?: DdSdkImplementation.PACKAGE_INFO_NOT_FOUND_ERROR_MESSAGE, e)
ddTelemetry.telemetryError(e.message ?: DdSdkImplementation.PACKAGE_INFO_NOT_FOUND_ERROR_MESSAGE, e)
return DdSdkImplementation.DEFAULT_APP_VERSION
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ import com.facebook.react.module.model.ReactModuleInfoProvider
*/
class DdSdkReactNativePackage : TurboReactPackage() {
private val sdkWrapper = DatadogSDKWrapper()
private val ddTelemetry = DdTelemetry()
override fun getModule(name: String, reactContext: ReactApplicationContext): NativeModule? {
return when (name) {
DdSdkImplementation.NAME -> DdSdk(reactContext, sdkWrapper)
DdSdkImplementation.NAME -> DdSdk(reactContext, sdkWrapper, ddTelemetry)
DdRumImplementation.NAME -> DdRum(reactContext, sdkWrapper)
DdTraceImplementation.NAME -> DdTrace(reactContext)
DdLogsImplementation.NAME -> DdLogs(reactContext, sdkWrapper)
Expand Down
Loading