Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RUM-4912] Configure synthetics attributes from MainActivity intent #702

Open
wants to merge 1 commit into
base: marcosaia/RUM-5918/update-dd-sdks
Choose a base branch
from

Conversation

marco-saia-datadog
Copy link
Contributor

@marco-saia-datadog marco-saia-datadog commented Jul 18, 2024

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

@marco-saia-datadog marco-saia-datadog marked this pull request as ready for review July 18, 2024 12:21
@marco-saia-datadog marco-saia-datadog requested a review from a team as a code owner July 18, 2024 12:21
@marco-saia-datadog marco-saia-datadog force-pushed the marcosaia/RUM-4912/synthetics-attributes branch from b3bb207 to f953909 Compare July 18, 2024 12:49
Comment on lines 170 to 182
// 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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not quite clear why we are doing this. Synthetics starts the application with intent having necessary attributes and we read them in DdSdk. And then, with any view tracking strategy registered we should be able to read it.

The only reason why it wouldn't be read is having configuration.nativeViewTracking != true. Is this a "hack" for that case?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is that you are supposed to be calling useViewTrackingStrategy in MainApplication, while on React Native, by the time the initialization is called, the MainActivity has already been created.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rather then try to do changed in API of Android SDK, so that injection is available from cross-platform

Copy link
Contributor Author

@marco-saia-datadog marco-saia-datadog Jul 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be difficult to achieve, as the method would have to be available before the initialisation of the SDK.

It might be good to have a method that we can call when we register to the lifecycle on DdSdk init, to which we can pass the MainActivity intent so that the Android SDK can internally handle the synthetics attributes setting, or any other operation that has to be performed on the MainActivity.

In my opinion, for the purpose of this PR, it wouldn't be too bad to expose either the setSyntheticsAttribute method or the AdvancedRumMonitor class, so that we can at least make sure that the session type is reported correctly for synthetics.

However, I am open to other ideas, and if we manage to make the View Tracking Strategies work on the MainActivity on RN, I would still expose a basic ViewTrackingStrategy that does not do anything aside from consuming the intent extras for synthetics, so that we can replace the current default NoOpViewTrackingStrategy.

@marco-saia-datadog marco-saia-datadog force-pushed the marcosaia/RUM-4912/synthetics-attributes branch from f953909 to 88bf7ba Compare September 6, 2024 12:41
@marco-saia-datadog marco-saia-datadog force-pushed the marcosaia/RUM-4912/synthetics-attributes branch from 88bf7ba to fb7ac6c Compare September 6, 2024 14:15
@marco-saia-datadog marco-saia-datadog changed the base branch from develop to marcosaia/RUM-5918/update-dd-sdks September 6, 2024 14:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants