Skip to content

Commit

Permalink
Added handleSyntheticsAttribute in RumInternalProxy
Browse files Browse the repository at this point in the history
  • Loading branch information
marco-saia-datadog committed Jul 23, 2024
1 parent b36d867 commit ef32fea
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 15 deletions.
1 change: 1 addition & 0 deletions features/dd-sdk-android-rum/api/apiSurface
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ interface com.datadog.android.rum.RumSessionListener
class com.datadog.android.rum._RumInternalProxy
fun addLongTask(Long, String)
fun updatePerformanceMetric(RumPerformanceMetric, Double)
fun handleSyntheticsAttribute(String?, String?)
companion object
fun setTelemetryConfigurationEventMapper(RumConfiguration.Builder, com.datadog.android.event.EventMapper<com.datadog.android.telemetry.model.TelemetryConfigurationEvent>): RumConfiguration.Builder
fun setAdditionalConfiguration(RumConfiguration.Builder, Map<String, Any>): RumConfiguration.Builder
Expand Down
1 change: 1 addition & 0 deletions features/dd-sdk-android-rum/api/dd-sdk-android-rum.api
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ public abstract interface class com/datadog/android/rum/RumSessionListener {
public final class com/datadog/android/rum/_RumInternalProxy {
public static final field Companion Lcom/datadog/android/rum/_RumInternalProxy$Companion;
public final fun addLongTask (JLjava/lang/String;)V
public final fun handleSyntheticsAttribute (Ljava/lang/String;Ljava/lang/String;)V
public final fun updatePerformanceMetric (Lcom/datadog/android/rum/RumPerformanceMetric;D)V
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

package com.datadog.android.rum

import android.content.Intent
import com.datadog.android.event.EventMapper
import com.datadog.android.lint.InternalApi
import com.datadog.android.rum.internal.monitor.AdvancedRumMonitor
Expand All @@ -31,6 +32,7 @@ import com.datadog.android.telemetry.model.TelemetryConfigurationEvent
"VariableNaming"
)
class _RumInternalProxy internal constructor(private val rumMonitor: AdvancedRumMonitor) {
private var handledSyntheticsAttribute = false

fun addLongTask(durationNs: Long, target: String) {
rumMonitor.addLongTask(durationNs, target)
Expand All @@ -40,6 +42,28 @@ class _RumInternalProxy internal constructor(private val rumMonitor: AdvancedRum
rumMonitor.updatePerformanceMetric(metric, value)
}

@Suppress("MemberVisibilityCanBePrivate")
fun handleSyntheticsAttribute(testId: String?, resultId: String?) {
if (this.handledSyntheticsAttribute) {
return
}

this.handledSyntheticsAttribute = true
if (testId.isNullOrBlank() || resultId.isNullOrBlank()) {
return
}

rumMonitor.setSyntheticsAttribute(testId, resultId)
}

internal fun handleSyntheticsAttribute(intent: Intent) {
@Suppress("TooGenericExceptionCaught")
val extras = try { intent.extras } catch (_: Exception) { null }
val testId = extras?.getString("_dd.synthetics.test_id")
val resultId = extras?.getString("_dd.synthetics.result_id")
this.handleSyntheticsAttribute(testId, resultId)
}

companion object {

@Suppress("FunctionMaxLength")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import com.datadog.android.api.SdkCore
import com.datadog.android.api.feature.FeatureSdkCore
import com.datadog.android.rum.GlobalRumMonitor
import com.datadog.android.rum.internal.RumFeature
import com.datadog.android.rum.internal.monitor.AdvancedRumMonitor

/**
* The ActivityLifecycleTrackingStrategy as an [Application.ActivityLifecycleCallbacks]
Expand Down Expand Up @@ -92,17 +91,10 @@ abstract class ActivityLifecycleTrackingStrategy :

@MainThread
override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
val intent = activity.intent
val extras = intent.safeExtras
val testId = extras?.getString("_dd.synthetics.test_id")
val resultId = extras?.getString("_dd.synthetics.result_id")
if (!testId.isNullOrBlank() && !resultId.isNullOrBlank()) {
(GlobalRumMonitor.get(sdkCore) as? AdvancedRumMonitor)
?.setSyntheticsAttribute(
testId,
resultId
)
}
GlobalRumMonitor
.get(sdkCore)
._getInternal()
?.handleSyntheticsAttribute(activity.intent)
}

@MainThread
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import org.mockito.kotlin.doAnswer
import org.mockito.kotlin.doReturn
import org.mockito.kotlin.doThrow
import org.mockito.kotlin.mock
import org.mockito.kotlin.never
import org.mockito.kotlin.verify
import org.mockito.kotlin.verifyNoInteractions
import org.mockito.kotlin.whenever
Expand Down Expand Up @@ -166,7 +167,7 @@ internal abstract class ActivityLifecycleTrackingStrategyTest<T> : ObjectTest<T>

// verify
val mockRumMonitor = rumMonitor.mockInstance as AdvancedRumMonitor
verifyNoInteractions(mockRumMonitor)
verify(mockRumMonitor, never()).setSyntheticsAttribute(any(), any())
}

@Test
Expand All @@ -184,7 +185,7 @@ internal abstract class ActivityLifecycleTrackingStrategyTest<T> : ObjectTest<T>

// verify
val mockRumMonitor = rumMonitor.mockInstance as AdvancedRumMonitor
verifyNoInteractions(mockRumMonitor)
verify(mockRumMonitor, never()).setSyntheticsAttribute(any(), any())
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,33 @@ package com.datadog.android.rum.utils.config
import com.datadog.android.core.InternalSdkCore
import com.datadog.android.rum.GlobalRumMonitor
import com.datadog.android.rum.RumMonitor
import com.datadog.android.rum._RumInternalProxy
import com.datadog.android.rum.internal.monitor.AdvancedRumMonitor
import com.datadog.tools.unit.extensions.config.MockTestConfiguration
import fr.xgouchet.elmyr.Forge
import org.mockito.kotlin.mock
import org.mockito.kotlin.whenever

@Suppress("TestFunctionName")
internal abstract class InternalAdvancedRumMonitor : AdvancedRumMonitor {
override fun _getInternal(): _RumInternalProxy? {
return null
}
}

internal class GlobalRumMonitorTestConfiguration :
MockTestConfiguration<RumMonitor>(AdvancedRumMonitor::class.java) {
MockTestConfiguration<RumMonitor>(InternalAdvancedRumMonitor::class.java) {

lateinit var mockSdkCore: InternalSdkCore

override fun setUp(forge: Forge) {
super.setUp(forge)
mockSdkCore = mock()

(mockInstance as? InternalAdvancedRumMonitor)?.let {
whenever(it._getInternal()).thenReturn(_RumInternalProxy(mockInstance as AdvancedRumMonitor))
}

GlobalRumMonitor.registerIfAbsent(mockInstance, mockSdkCore)
}

Expand Down

0 comments on commit ef32fea

Please sign in to comment.