dd-sdk-reactnative
dd-sdk-reactnative copied to clipboard
[RUM-4912] Configure synthetics attributes from MainActivity intent
What does this PR do?
Retrieves synthetics attributes from the MainActivity intent and configures RUM, so that the session will be detected, treated and reported as a synthetics test.
Context
In Android SDK, synthetics attributes (result_id and test_id) are retrieved from the intent extras in ActivityLifecycleTrackingStrategy.
ActivityLifecycleTrackingStrategy is a superclass of different View Tracking strategies that can be enabled through RumConfigurationBuilder by using .useViewTrackingStrategy.
In React Native SDK, we don't enable native views tracking unless it is explicitly specified by the users in the SDK configuration and - more importantly - we don't initialise the SDK in application scope, and the intent extras are lost for this reason.
Proposed Solution
We can register to the lifecycle events using the application context when the RN module is initialized, using context.addLifecycleEventListener, retrieve the intent extras, and use them later during the SDK initialization.
Problem
We currently do not have a way to set the synthetics attributes, because the way its done on Android is through AdvancedRumMonitor, which is an internal class and it is not visible from RN.
Code from ActivityLifecycleTrackingStrategy.kt:
(GlobalRumMonitor.get(sdkCore) as? AdvancedRumMonitor)
?.setSyntheticsAttribute(
testId,
resultId
)
Workaround
This PR introduces a workaround to still take advantage of the Android code, and demonstrate that we can in fact intercept and set the intent extras using the lifecycle event listener.
The workaround consists in manually calling the ActivityLifecycleTrackingStrategy methods with a dummy activity that contains the required intent extras:
// 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)
Conclusion
I believe we have to expose a method from Android SDK that allows cross platform frameworks to set the synthetics attributes, and get rid of this workaround.
Review checklist (to be filled by reviewers)
- [ ] Feature or bugfix MUST have appropriate tests
- [ ] Make sure you discussed the feature or bugfix with the maintaining team in an Issue
- [ ] Make sure each commit and the PR mention the Issue number (cf the CONTRIBUTING doc)
- [ ] If this PR is auto-generated, please make sure also to manually update the code related to the change