Skip to content

Commit

Permalink
Merge pull request #781 from DataDog/carlosnogueira/RUM-1860/add-batc…
Browse files Browse the repository at this point in the history
…h-public-config

[RUM-1860] Expose batch processing public configuration to react-native
  • Loading branch information
marco-saia-datadog authored Feb 5, 2025
2 parents 9b5285f + db1a6e4 commit 0a877a0
Show file tree
Hide file tree
Showing 17 changed files with 249 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@

package com.datadog.reactnative

import com.datadog.android.core.configuration.BatchProcessingLevel
import com.datadog.android.trace.TracingHeaderType
import java.net.Proxy



/**
* A configuration object to initialize Datadog's features.
* @param clientToken A valid Datadog client token.
Expand Down Expand Up @@ -38,6 +41,7 @@ import java.net.Proxy
* @param bundleLogsWithRum Enables RUM correlation with logs.
* @param bundleLogsWithTraces Enables Traces correlation with logs.
* @param trackNonFatalAnrs Enables tracking of non-fatal ANRs on Android.
* @param batchProcessingLevel The preferred number of batches of data that will be sent in a single upload (can be 'LOW', 'MEDIUM' (default), 'HIGH')
*/
data class DdSdkConfiguration(
val clientToken: String,
Expand Down Expand Up @@ -66,7 +70,8 @@ data class DdSdkConfiguration(
val firstPartyHosts: Map<String, Set<TracingHeaderType>>? = null,
val bundleLogsWithRum: Boolean? = null,
val bundleLogsWithTraces: Boolean? = null,
val trackNonFatalAnrs: Boolean? = null
val trackNonFatalAnrs: Boolean? = null,
val batchProcessingLevel: String? = null
)

internal data class JSONConfigurationFile(
Expand Down Expand Up @@ -98,7 +103,8 @@ internal data class JSONDdSdkConfiguration(
val firstPartyHosts: List<JSONFirstPartyHost>? = null,
val bundleLogsWithRum: Boolean? = null,
val bundleLogsWithTraces: Boolean? = null,
val trackNonFatalAnrs: Boolean? = null
val trackNonFatalAnrs: Boolean? = null,
val batchProcessingLevel: String? = null
)

internal data class JSONProxyConfiguration(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ internal fun ReadableMap.asDdSdkConfiguration(): DdSdkConfiguration {
firstPartyHosts = getArray("firstPartyHosts")?.asFirstPartyHosts(),
bundleLogsWithRum = getBoolean("bundleLogsWithRum"),
bundleLogsWithTraces = getBoolean("bundleLogsWithTraces"),
trackNonFatalAnrs = getBooleanOrNull("trackNonFatalAnrs")
trackNonFatalAnrs = getBooleanOrNull("trackNonFatalAnrs"),
batchProcessingLevel = getString("batchProcessingLevel")
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import com.datadog.android.DatadogSite
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.core.configuration.BatchProcessingLevel
import com.datadog.android.event.EventMapper
import com.datadog.android.log.LogsConfiguration
import com.datadog.android.privacy.TrackingConsent
Expand Down Expand Up @@ -251,6 +252,7 @@ class DdSdkNativeInitialization internal constructor(
buildBatchSize(configuration.batchSize)
)


configuration.proxyConfig?.let { (proxy, authenticator) ->
configBuilder.setProxy(proxy, authenticator)
}
Expand All @@ -259,6 +261,8 @@ class DdSdkNativeInitialization internal constructor(
configBuilder.setFirstPartyHostsWithHeaderType(configuration.firstPartyHosts)
}

configBuilder.setBatchProcessingLevel(buildBatchProcessingLevel(configuration.batchProcessingLevel))

return configBuilder.build()
}

Expand Down Expand Up @@ -320,6 +324,16 @@ class DdSdkNativeInitialization internal constructor(
}
}


private fun buildBatchProcessingLevel(batchProcessingLevel: String?): BatchProcessingLevel {
return when (batchProcessingLevel?.lowercase(Locale.US)) {
"low" -> BatchProcessingLevel.LOW
"medium" -> BatchProcessingLevel.MEDIUM
"high" -> BatchProcessingLevel.HIGH
else -> BatchProcessingLevel.MEDIUM
}
}

internal fun getConfigurationFromJSONFile(): DdSdkConfiguration {
try {
val jsonString = jsonFileReader.parseAssetsJSONFile(appContext, "datadog-configuration.json")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import android.os.Looper
import android.util.Log
import android.view.Choreographer
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
Expand Down Expand Up @@ -1502,6 +1503,45 @@ internal class DdSdkTest {
}
}

@ParameterizedTest
@MethodSource("provideBatchProcessingLevel")
fun `𝕄 initialize native SDK 𝕎 initialize() {batch processing level}`(
input: String,
expectedBatchSize: BatchProcessingLevel,
@Forgery configuration: DdSdkConfiguration
) {
// Given
val bridgeConfiguration = configuration.copy(
batchProcessingLevel = input
)
val sdkConfigCaptor = argumentCaptor<Configuration>()
val rumConfigCaptor = argumentCaptor<RumConfiguration>()
val logsConfigCaptor = argumentCaptor<LogsConfiguration>()
val traceConfigCaptor = argumentCaptor<TraceConfiguration>()

// When
testedBridgeSdk.initialize(bridgeConfiguration.toReadableJavaOnlyMap(), mockPromise)

// Then
inOrder(mockDatadog) {
verify(mockDatadog).initialize(
same(mockContext),
sdkConfigCaptor.capture(),
any()
)
verify(mockDatadog).enableRum(rumConfigCaptor.capture())
verify(mockDatadog).enableTrace(traceConfigCaptor.capture())
verify(mockDatadog).enableLogs(logsConfigCaptor.capture())
}
assertThat(sdkConfigCaptor.firstValue)
.hasField("coreConfig") { coreConfig ->
coreConfig.hasFieldEqualTo(
"batchProcessingLevel",
expectedBatchSize
)
}
}

@Test
fun `𝕄 initialize native SDK 𝕎 initialize() {trackBackgroundEvents}`(
@Forgery configuration: DdSdkConfiguration,
Expand Down Expand Up @@ -2358,5 +2398,14 @@ internal class DdSdkTest {
Arguments.of("FREQUENT", UploadFrequency.FREQUENT)
)
}

@JvmStatic
fun provideBatchProcessingLevel(): Stream<Arguments?>? {
return Stream.of(
Arguments.of("LOW", BatchProcessingLevel.LOW),
Arguments.of("MEDIUM", BatchProcessingLevel.MEDIUM),
Arguments.of("HIGH", BatchProcessingLevel.HIGH)
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

package com.datadog.tools.unit

import com.datadog.android.core.configuration.BatchProcessingLevel
import com.datadog.android.core.configuration.BatchSize
import com.datadog.android.core.configuration.UploadFrequency
import com.datadog.android.rum.configuration.VitalsUpdateFrequency
Expand Down Expand Up @@ -101,6 +102,12 @@ fun DdSdkConfiguration.toReadableJavaOnlyMap(): ReadableMap {

trackNonFatalAnrs?.let { map.put("trackNonFatalAnrs", it) }

if (batchProcessingLevel != null) {
map["batchProcessingLevel"] = batchProcessingLevel
} else {
map["batchProcessingLevel"] = BatchProcessingLevel.MEDIUM.toString()
}

return map.toReadableMap()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,14 @@ class DdSdkConfigurationForgeryFactory : ForgeryFactory<DdSdkConfiguration> {
firstPartyHosts = null,
bundleLogsWithRum = forge.aBool(),
bundleLogsWithTraces = forge.aBool(),
trackNonFatalAnrs = forge.aBool()
trackNonFatalAnrs = forge.aBool(),
batchProcessingLevel = forge.aNullable {
anElementFrom(
"LOW",
"MEDIUM",
"HIGH"
)
}
)
}
}
6 changes: 5 additions & 1 deletion packages/core/ios/Sources/DdSdkConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import DatadogRUM
- bundleLogsWithTraces: Correlates logs with traces.
- appHangThreshold: The threshold for non-fatal app hangs reporting in seconds.
- trackWatchdogTerminations: Whether the SDK should track application termination by the watchdog
- batchProcessingLevel: Maximum number of batches processed sequentially without a delay
*/
@objc(DdSdkConfiguration)
public class DdSdkConfiguration: NSObject {
Expand Down Expand Up @@ -72,6 +73,7 @@ public class DdSdkConfiguration: NSObject {
public var bundleLogsWithTraces: Bool
public var appHangThreshold: Double? = nil
public var trackWatchdogTerminations: Bool
public var batchProcessingLevel: Datadog.Configuration.BatchProcessingLevel

public init(
clientToken: String,
Expand Down Expand Up @@ -102,7 +104,8 @@ public class DdSdkConfiguration: NSObject {
bundleLogsWithRum: Bool,
bundleLogsWithTraces: Bool,
appHangThreshold: Double?,
trackWatchdogTerminations: Bool
trackWatchdogTerminations: Bool,
batchProcessingLevel: Datadog.Configuration.BatchProcessingLevel
) {
self.clientToken = clientToken
self.env = env
Expand Down Expand Up @@ -133,6 +136,7 @@ public class DdSdkConfiguration: NSObject {
self.bundleLogsWithTraces = bundleLogsWithTraces
self.appHangThreshold = appHangThreshold
self.trackWatchdogTerminations = trackWatchdogTerminations
self.batchProcessingLevel = batchProcessingLevel
}
}

Expand Down
3 changes: 2 additions & 1 deletion packages/core/ios/Sources/DdSdkNativeInitialization.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ public class DdSdkNativeInitialization: NSObject {
service: configuration.serviceName as? String,
batchSize: configuration.batchSize,
uploadFrequency: configuration.uploadFrequency,
proxyConfiguration: configuration.proxyConfig
proxyConfiguration: configuration.proxyConfig,
batchProcessingLevel: configuration.batchProcessingLevel
)

if var additionalConfiguration = configuration.additionalConfig as? [String: Any] {
Expand Down
21 changes: 19 additions & 2 deletions packages/core/ios/Sources/RNDdSdkConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ extension NSDictionary {
let bundleLogsWithTraces = object(forKey: "bundleLogsWithTraces") as? Bool
let appHangThreshold = object(forKey: "appHangThreshold") as? Double
let trackWatchdogTerminations = object(forKey: "trackWatchdogTerminations") as? Bool
let batchProcessingLevel = object(forKey: "batchProcessingLevel") as? NSString

return DdSdkConfiguration(
clientToken: (clientToken != nil) ? clientToken! : String(),
Expand Down Expand Up @@ -71,7 +72,8 @@ extension NSDictionary {
bundleLogsWithRum: bundleLogsWithRum ?? DefaultConfiguration.bundleLogsWithRum,
bundleLogsWithTraces: bundleLogsWithTraces ?? DefaultConfiguration.bundleLogsWithTraces,
appHangThreshold: appHangThreshold,
trackWatchdogTerminations: trackWatchdogTerminations ?? DefaultConfiguration.trackWatchdogTerminations
trackWatchdogTerminations: trackWatchdogTerminations ?? DefaultConfiguration.trackWatchdogTerminations,
batchProcessingLevel: batchProcessingLevel.asBatchProcessingLevel()
)
}

Expand Down Expand Up @@ -238,6 +240,7 @@ extension Dictionary where Key == String, Value == AnyObject {
let bundleLogsWithTraces = configuration["bundleLogsWithTraces"] as? Bool
let appHangThreshold = configuration["appHangThreshold"] as? Double
let trackWatchdogTerminations = configuration["trackWatchdogTerminations"] as? Bool
let batchProcessingLevel = configuration["batchProcessingLevel"] as? NSString

return DdSdkConfiguration(
clientToken: clientToken ?? String(),
Expand Down Expand Up @@ -271,7 +274,8 @@ extension Dictionary where Key == String, Value == AnyObject {
bundleLogsWithRum: bundleLogsWithRum ?? DefaultConfiguration.bundleLogsWithRum,
bundleLogsWithTraces: bundleLogsWithTraces ?? DefaultConfiguration.bundleLogsWithTraces,
appHangThreshold: appHangThreshold,
trackWatchdogTerminations: trackWatchdogTerminations ?? DefaultConfiguration.trackWatchdogTerminations
trackWatchdogTerminations: trackWatchdogTerminations ?? DefaultConfiguration.trackWatchdogTerminations,
batchProcessingLevel: batchProcessingLevel.asBatchProcessingLevel()
)
}
}
Expand Down Expand Up @@ -349,4 +353,17 @@ extension NSString? {
return .us1
}
}

func asBatchProcessingLevel() -> Datadog.Configuration.BatchProcessingLevel {
switch self?.lowercased {
case "low":
return .low
case "medium":
return .medium
case "high":
return .high
default:
return .medium
}
}
}
Loading

0 comments on commit 0a877a0

Please sign in to comment.