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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -57,6 +58,8 @@ class DdSdkImplementation(
}
})

configureSynthetics()

initialized.set(true)

promise.resolve(null)
Expand Down Expand Up @@ -156,6 +159,17 @@ class DdSdkImplementation(
}
}

private fun configureSynthetics() {
if (DdSdkSynthetics.testId.isNullOrBlank() || DdSdkSynthetics.resultId.isNullOrBlank()) {
return
}

datadog.getRumMonitor()._getInternal()?.setSyntheticsAttribute(
DdSdkSynthetics.testId,
DdSdkSynthetics.resultId
)
}

private fun buildVitalUpdateFrequency(vitalsUpdateFrequency: String?): VitalsUpdateFrequency {
val vitalUpdateFrequencyLower = vitalsUpdateFrequency?.lowercase(Locale.US)
return when (vitalUpdateFrequencyLower) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down