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-5566] Added setSyntheticsAttribute in RumInternalProxy #2133

Merged
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
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 setSyntheticsAttribute(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 setSyntheticsAttribute (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) {
@Volatile 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 setSyntheticsAttribute(testId: String?, resultId: String?) {
if (this.handledSyntheticsAttribute) {
return
}

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

rumMonitor.setSyntheticsAttribute(testId, resultId)
}

internal fun setSyntheticsAttributeFromIntent(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.setSyntheticsAttribute(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,16 +91,11 @@ 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
)
if (::sdkCore.isInitialized) {
marco-saia-datadog marked this conversation as resolved.
Show resolved Hide resolved
GlobalRumMonitor
.get(sdkCore)
._getInternal()
?.setSyntheticsAttributeFromIntent(activity.intent)
}
}

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 {
marco-saia-datadog marked this conversation as resolved.
Show resolved Hide resolved
override fun _getInternal(): _RumInternalProxy? {
return null
}
}
marco-saia-datadog marked this conversation as resolved.
Show resolved Hide resolved

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 {
marco-saia-datadog marked this conversation as resolved.
Show resolved Hide resolved
whenever(it._getInternal()).thenReturn(_RumInternalProxy(mockInstance as AdvancedRumMonitor))
}

GlobalRumMonitor.registerIfAbsent(mockInstance, mockSdkCore)
}

Expand Down