From 88bf7ba6f85ddcca59d9c062c30057ba0ce4ad7d Mon Sep 17 00:00:00 2001 From: Marco Saia Date: Wed, 17 Jul 2024 17:19:35 +0200 Subject: [PATCH] W.I.P: Add synthetics attributes from MainActivity intent --- .../reactnative/DdSdkImplementation.kt | 26 +++++++++++++++++++ .../datadog/reactnative/DdSdkSynthetics.kt | 12 +++++++++ .../kotlin/com/datadog/reactnative/DdSdk.kt | 19 ++++++++++++++ .../kotlin/com/datadog/reactnative/DdSdk.kt | 19 ++++++++++++++ 4 files changed, 76 insertions(+) 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..47542ef5e 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 @@ -6,11 +6,15 @@ package com.datadog.reactnative +import android.app.Activity import android.content.Context +import android.content.Intent import android.util.Log import com.datadog.android.privacy.TrackingConsent +import com.datadog.android.rum.RumErrorSource import com.datadog.android.rum.configuration.VitalsUpdateFrequency import com.datadog.android.rum.RumPerformanceMetric +import com.datadog.android.rum.tracking.ActivityLifecycleTrackingStrategy import com.facebook.react.bridge.LifecycleEventListener import com.facebook.react.bridge.Promise import com.facebook.react.bridge.ReactApplicationContext @@ -57,6 +61,8 @@ class DdSdkImplementation( } }) + configureSynthetics() + initialized.set(true) promise.resolve(null) @@ -156,6 +162,26 @@ class DdSdkImplementation( } } + private fun configureSynthetics() { + if (DdSdkSynthetics.testId.isNullOrBlank() || DdSdkSynthetics.resultId.isNullOrBlank()) { + return + } + + // Workaround for `AdvancedRumMonitor` and `setSyntheticsAttribute` not being visible. + val intent = Intent().apply { + putExtra("_dd.synthetics.test_id", DdSdkSynthetics.testId) + putExtra("_dd.synthetics.result_id", DdSdkSynthetics.resultId) + } + + val proxyActivity = Activity() + proxyActivity.intent = intent + + val lifecycleTracking = object : ActivityLifecycleTrackingStrategy() {} + val core = DatadogSDKWrapperStorage.getSdkCore() + lifecycleTracking.register(core, appContext) + lifecycleTracking.onActivityCreated(proxyActivity, null) + } + 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.