From 16e0b3402fa6198cc7089b8c8c1035f3ca624d55 Mon Sep 17 00:00:00 2001 From: Marco Saia Date: Wed, 17 Jul 2024 17:19:35 +0200 Subject: [PATCH 1/2] Add synthetics attributes from MainActivity intent --- .../reactnative/DdSdkImplementation.kt | 18 +++++++++++++++++- .../datadog/reactnative/DdSdkSynthetics.kt | 12 ++++++++++++ .../kotlin/com/datadog/reactnative/DdSdk.kt | 19 +++++++++++++++++++ .../kotlin/com/datadog/reactnative/DdSdk.kt | 19 +++++++++++++++++++ 4 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 packages/core/android/src/main/kotlin/com/datadog/reactnative/DdSdkSynthetics.kt diff --git a/packages/core/android/src/main/kotlin/com/datadog/reactnative/DdSdkImplementation.kt b/packages/core/android/src/main/kotlin/com/datadog/reactnative/DdSdkImplementation.kt index fc30d13ab..7220c6c76 100644 --- a/packages/core/android/src/main/kotlin/com/datadog/reactnative/DdSdkImplementation.kt +++ b/packages/core/android/src/main/kotlin/com/datadog/reactnative/DdSdkImplementation.kt @@ -9,8 +9,9 @@ package com.datadog.reactnative import android.content.Context import android.util.Log import com.datadog.android.privacy.TrackingConsent -import com.datadog.android.rum.configuration.VitalsUpdateFrequency +import com.datadog.android.rum.GlobalRumMonitor import com.datadog.android.rum.RumPerformanceMetric +import com.datadog.android.rum.configuration.VitalsUpdateFrequency import com.facebook.react.bridge.LifecycleEventListener import com.facebook.react.bridge.Promise import com.facebook.react.bridge.ReactApplicationContext @@ -57,6 +58,8 @@ class DdSdkImplementation( } }) + configureSynthetics() + initialized.set(true) promise.resolve(null) @@ -156,6 +159,19 @@ class DdSdkImplementation( } } + private fun configureSynthetics() { + if (DdSdkSynthetics.testId.isNullOrBlank() || DdSdkSynthetics.resultId.isNullOrBlank()) { + return + } + + val core = DatadogSDKWrapperStorage.getSdkCore() + val monitor = GlobalRumMonitor.get(core) + monitor._getInternal()?.setSyntheticsAttribute( + DdSdkSynthetics.testId, + DdSdkSynthetics.resultId + ) + } + private fun buildVitalUpdateFrequency(vitalsUpdateFrequency: String?): VitalsUpdateFrequency { val vitalUpdateFrequencyLower = vitalsUpdateFrequency?.lowercase(Locale.US) return when (vitalUpdateFrequencyLower) { diff --git a/packages/core/android/src/main/kotlin/com/datadog/reactnative/DdSdkSynthetics.kt b/packages/core/android/src/main/kotlin/com/datadog/reactnative/DdSdkSynthetics.kt new file mode 100644 index 000000000..8ff756d83 --- /dev/null +++ b/packages/core/android/src/main/kotlin/com/datadog/reactnative/DdSdkSynthetics.kt @@ -0,0 +1,12 @@ +/* + * Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. + * This product includes software developed at Datadog (https://www.datadoghq.com/). + * Copyright 2016-Present Datadog, Inc. + */ + +package com.datadog.reactnative + +internal object DdSdkSynthetics { + var testId: String? = null + var resultId: String? = null +} \ No newline at end of file diff --git a/packages/core/android/src/newarch/kotlin/com/datadog/reactnative/DdSdk.kt b/packages/core/android/src/newarch/kotlin/com/datadog/reactnative/DdSdk.kt index c004814a3..3680fea30 100644 --- a/packages/core/android/src/newarch/kotlin/com/datadog/reactnative/DdSdk.kt +++ b/packages/core/android/src/newarch/kotlin/com/datadog/reactnative/DdSdk.kt @@ -6,6 +6,8 @@ package com.datadog.reactnative +import android.app.Activity +import com.facebook.react.bridge.LifecycleEventListener import com.facebook.react.bridge.Promise import com.facebook.react.bridge.ReactApplicationContext import com.facebook.react.bridge.ReactMethod @@ -21,6 +23,23 @@ class DdSdk( override fun getName(): String = DdSdkImplementation.NAME + init { + reactContext.addLifecycleEventListener(object : LifecycleEventListener { + override fun onHostResume() { + val currentActivity: Activity? = currentActivity + if (currentActivity != null) { + val intent = currentActivity.intent + val extras = intent.extras + DdSdkSynthetics.testId = extras?.getString("_dd.synthetics.test_id") + DdSdkSynthetics.resultId = extras?.getString("_dd.synthetics.result_id") + } + } + + override fun onHostPause() {} + override fun onHostDestroy() {} + }) + } + /** * Initializes Datadog's features. * @param configuration The configuration to use. diff --git a/packages/core/android/src/oldarch/kotlin/com/datadog/reactnative/DdSdk.kt b/packages/core/android/src/oldarch/kotlin/com/datadog/reactnative/DdSdk.kt index c5e422d6e..47fc76ea3 100644 --- a/packages/core/android/src/oldarch/kotlin/com/datadog/reactnative/DdSdk.kt +++ b/packages/core/android/src/oldarch/kotlin/com/datadog/reactnative/DdSdk.kt @@ -6,6 +6,8 @@ package com.datadog.reactnative +import android.app.Activity +import com.facebook.react.bridge.LifecycleEventListener import com.facebook.react.bridge.Promise import com.facebook.react.bridge.ReactApplicationContext import com.facebook.react.bridge.ReactContextBaseJavaModule @@ -22,6 +24,23 @@ class DdSdk( override fun getName(): String = DdSdkImplementation.NAME + init { + reactContext.addLifecycleEventListener(object : LifecycleEventListener { + override fun onHostResume() { + val currentActivity: Activity? = currentActivity + if (currentActivity != null) { + val intent = currentActivity.intent + val extras = intent.extras + DdSdkSynthetics.testId = extras?.getString("_dd.synthetics.test_id") + DdSdkSynthetics.resultId = extras?.getString("_dd.synthetics.result_id") + } + } + + override fun onHostPause() {} + override fun onHostDestroy() {} + }) + } + /** * Initializes Datadog's features. * @param configuration The configuration to use. From cd86b6ba4ec6c56571a96156f8eb46c76ac2c71b Mon Sep 17 00:00:00 2001 From: Marco Saia Date: Mon, 7 Oct 2024 14:41:02 +0200 Subject: [PATCH 2/2] Android: added unit tests for synthetics attributes --- .../datadog/reactnative/DdSdkImplementation.kt | 4 +--- .../kotlin/com/datadog/reactnative/DdSdkTest.kt | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/packages/core/android/src/main/kotlin/com/datadog/reactnative/DdSdkImplementation.kt b/packages/core/android/src/main/kotlin/com/datadog/reactnative/DdSdkImplementation.kt index 7220c6c76..8f5020080 100644 --- a/packages/core/android/src/main/kotlin/com/datadog/reactnative/DdSdkImplementation.kt +++ b/packages/core/android/src/main/kotlin/com/datadog/reactnative/DdSdkImplementation.kt @@ -164,9 +164,7 @@ class DdSdkImplementation( return } - val core = DatadogSDKWrapperStorage.getSdkCore() - val monitor = GlobalRumMonitor.get(core) - monitor._getInternal()?.setSyntheticsAttribute( + datadog.getRumMonitor()._getInternal()?.setSyntheticsAttribute( DdSdkSynthetics.testId, DdSdkSynthetics.resultId ) diff --git a/packages/core/android/src/test/kotlin/com/datadog/reactnative/DdSdkTest.kt b/packages/core/android/src/test/kotlin/com/datadog/reactnative/DdSdkTest.kt index 46bffe029..071b9ee7c 100644 --- a/packages/core/android/src/test/kotlin/com/datadog/reactnative/DdSdkTest.kt +++ b/packages/core/android/src/test/kotlin/com/datadog/reactnative/DdSdkTest.kt @@ -2293,6 +2293,23 @@ internal class DdSdkTest { .hasFieldEqualTo("customEndpointUrl", customTraceEndpoint) } + @Test + fun `𝕄 initialize native SDK 𝕎 initialize() {synthethics attributes}`() { + // Given + fakeConfiguration = fakeConfiguration.copy(nativeCrashReportEnabled = false, site = null) + DdSdkSynthetics.testId = "unit-test-test-id" + DdSdkSynthetics.resultId = "unit-test-result-id" + + // When + testedBridgeSdk.initialize(fakeConfiguration.toReadableJavaOnlyMap(), mockPromise) + + // Then + verify(mockRumInternalProxy).setSyntheticsAttribute( + "unit-test-test-id", + "unit-test-result-id" + ) + } + @Test fun `𝕄 clear all data 𝕎 clearAllData()`() { // When