Skip to content

Commit

Permalink
Merge pull request #702 from DataDog/marcosaia/RUM-4912/synthetics-at…
Browse files Browse the repository at this point in the history
…tributes

[RUM-4912] Configure synthetics attributes from MainActivity intent
  • Loading branch information
marco-saia-datadog authored Oct 7, 2024
2 parents d935559 + cd86b6b commit ff1b34a
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 1 deletion.
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

0 comments on commit ff1b34a

Please sign in to comment.