Skip to content

Commit

Permalink
W.I.P: Add synthetics attributes from MainActivity intent
Browse files Browse the repository at this point in the history
  • Loading branch information
marco-saia-datadog committed Jul 18, 2024
1 parent 6d7b1a9 commit b3bb207
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@

package com.datadog.reactnative

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.util.Log
import com.datadog.android.privacy.TrackingConsent
import com.datadog.android.rum.RumErrorSource
import com.datadog.android.rum.configuration.VitalsUpdateFrequency
import com.datadog.android.rum.RumPerformanceMetric
import com.datadog.android.rum.tracking.ActivityLifecycleTrackingStrategy
import com.facebook.react.bridge.LifecycleEventListener
import com.facebook.react.bridge.Promise
import com.facebook.react.bridge.ReactApplicationContext
Expand Down Expand Up @@ -57,6 +61,8 @@ class DdSdkImplementation(
}
})

configureSynthetics()

initialized.set(true)

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

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

// 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)
}

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

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

0 comments on commit b3bb207

Please sign in to comment.