Skip to content

Commit 1cb7d3d

Browse files
RUM-9508: pr fixes
1 parent 26b95d4 commit 1cb7d3d

File tree

10 files changed

+93
-60
lines changed

10 files changed

+93
-60
lines changed

sample/benchmark/src/main/java/com/datadog/benchmark/sample/BenchmarkApplication.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,6 @@ internal class BenchmarkApplication : Application() {
2828
benchmarkAppComponent.inject(this)
2929
}
3030
}
31+
32+
internal val Application.benchmarkAppComponent: BenchmarkAppComponent
33+
get() = (this as BenchmarkApplication).benchmarkAppComponent

sample/benchmark/src/main/java/com/datadog/benchmark/sample/DatadogFeaturesInitializer.kt

Lines changed: 49 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,34 +21,44 @@ import com.datadog.android.sessionreplay.material.MaterialExtensionSupport
2121
import com.datadog.benchmark.sample.config.BenchmarkConfig
2222
import com.datadog.benchmark.sample.config.SyntheticsRun
2323
import com.datadog.benchmark.sample.config.SyntheticsScenario
24-
import com.datadog.benchmark.sample.di.activity.BenchmarkActivityScope
2524
import com.datadog.benchmark.sample.navigation.BenchmarkNavigationPredicate
2625
import com.datadog.sample.benchmark.BuildConfig
2726
import com.datadog.sample.benchmark.R
2827
import javax.inject.Inject
28+
import javax.inject.Singleton
2929

30-
@BenchmarkActivityScope
30+
/**
31+
* The general recommendation is to initialize all the components at the Application.onCreate
32+
* to have all the observability as early as possible. However in the Benchmark app we know what features
33+
* we need only in [MainActivity.onCreate], it depends on the [SyntheticsScenario] which is derived from intent extras.
34+
*/
35+
@Singleton
3136
@Suppress("TooManyFunctions")
3237
internal class DatadogFeaturesInitializer @Inject constructor(
33-
private val sdkCore: SdkCore,
34-
private val config: BenchmarkConfig
38+
private val sdkCore: SdkCore
3539
) {
36-
fun initialize() {
37-
if (needToEnableRum()) {
40+
private var isInitialized = false
41+
fun initialize(config: BenchmarkConfig) {
42+
if (isInitialized) {
43+
return
44+
}
45+
isInitialized = true
46+
47+
if (needToEnableRum(config)) {
3848
enableRum()
3949
}
4050

41-
if (needToEnableLogs()) {
51+
if (needToEnableLogs(config)) {
4252
enableLogs()
4353
}
4454

45-
if (needToEnableSessionReplay()) {
55+
if (needToEnableSessionReplay(config)) {
4656
enableSessionReplay()
4757
}
4858
}
4959

50-
private fun needToEnableSessionReplay(): Boolean {
51-
return isInstrumentedRun() && isSessionReplayScenario()
60+
private fun needToEnableSessionReplay(config: BenchmarkConfig): Boolean {
61+
return isInstrumentedRun(config) && isSessionReplayScenario(config)
5262
}
5363

5464
@Suppress("DEPRECATION")
@@ -63,19 +73,19 @@ internal class DatadogFeaturesInitializer @Inject constructor(
6373
SessionReplay.enable(sessionReplayConfig, sdkCore)
6474
}
6575

66-
private fun needToEnableLogs(): Boolean {
67-
return isInstrumentedRun() && isLogsScenario()
76+
private fun needToEnableLogs(config: BenchmarkConfig): Boolean {
77+
return isInstrumentedRun(config) && isLogsScenario(config)
6878
}
6979

7080
private fun enableLogs() {
7181
val logsConfig = LogsConfiguration.Builder().build()
7282
Logs.enable(logsConfig, sdkCore)
7383
}
7484

75-
private fun needToEnableRum(): Boolean {
76-
return when (isInstrumentedRun()) {
77-
true -> isSessionReplayScenario() || isRumScenario()
78-
false -> isSessionReplayScenario()
85+
private fun needToEnableRum(config: BenchmarkConfig): Boolean {
86+
return when (isInstrumentedRun(config)) {
87+
true -> isSessionReplayScenario(config) || isRumScenario(config)
88+
false -> isSessionReplayScenario(config)
7989
}
8090
}
8191

@@ -121,24 +131,40 @@ internal class DatadogFeaturesInitializer @Inject constructor(
121131
Rum.enable(rumConfig, sdkCore = sdkCore)
122132
}
123133

124-
private fun isSessionReplayScenario() = when (config.scenario) {
134+
private fun isSessionReplayScenario(config: BenchmarkConfig) = when (config.scenario) {
125135
SyntheticsScenario.SessionReplayCompose,
136+
SyntheticsScenario.Upload,
126137
SyntheticsScenario.SessionReplay -> true
127-
else -> false
138+
SyntheticsScenario.Rum,
139+
SyntheticsScenario.Trace,
140+
SyntheticsScenario.LogsCustom,
141+
SyntheticsScenario.LogsHeavyTraffic,
142+
null -> false
128143
}
129144

130-
private fun isRumScenario() = when (config.scenario) {
145+
private fun isRumScenario(config: BenchmarkConfig) = when (config.scenario) {
131146
SyntheticsScenario.Rum -> true
132-
else -> false
147+
SyntheticsScenario.SessionReplay,
148+
SyntheticsScenario.SessionReplayCompose,
149+
SyntheticsScenario.Trace,
150+
SyntheticsScenario.LogsCustom,
151+
SyntheticsScenario.LogsHeavyTraffic,
152+
SyntheticsScenario.Upload,
153+
null -> false
133154
}
134155

135-
private fun isLogsScenario() = when (config.scenario) {
156+
private fun isLogsScenario(config: BenchmarkConfig) = when (config.scenario) {
136157
SyntheticsScenario.LogsCustom,
137158
SyntheticsScenario.LogsHeavyTraffic -> true
138-
else -> false
159+
SyntheticsScenario.SessionReplay,
160+
SyntheticsScenario.SessionReplayCompose,
161+
SyntheticsScenario.Rum,
162+
SyntheticsScenario.Trace,
163+
SyntheticsScenario.Upload,
164+
null -> false
139165
}
140166

141-
private fun isInstrumentedRun() = config.run == SyntheticsRun.Instrumented
167+
private fun isInstrumentedRun(config: BenchmarkConfig) = config.run == SyntheticsRun.Instrumented
142168

143169
companion object {
144170
private const val SAMPLE_IN_ALL_SESSIONS = 100f

sample/benchmark/src/main/java/com/datadog/benchmark/sample/MainActivity.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
package com.datadog.benchmark.sample
88

9+
import android.app.Activity
910
import android.os.Bundle
1011
import androidx.activity.compose.setContent
1112
import androidx.appcompat.app.AppCompatActivity
@@ -43,7 +44,7 @@ class MainActivity : AppCompatActivity() {
4344
config = BenchmarkConfig.resolveSyntheticsBundle(intent.extras)
4445

4546
benchmarkActivityComponent = DaggerBenchmarkActivityComponent.factory().create(
46-
deps = (application as BenchmarkApplication).benchmarkAppComponent,
47+
deps = application.benchmarkAppComponent,
4748
config = config,
4849
mainActivity = this
4950
)
@@ -59,7 +60,7 @@ class MainActivity : AppCompatActivity() {
5960
setContentView(R.layout.activity_main)
6061
}
6162

62-
datadogFeaturesInitializer.initialize()
63+
datadogFeaturesInitializer.initialize(config)
6364
}
6465

6566
override fun onStart() {
@@ -79,3 +80,6 @@ class MainActivity : AppCompatActivity() {
7980
}
8081
}
8182
}
83+
84+
internal val Activity.benchmarkActivityComponent: BenchmarkActivityComponent
85+
get() = (this as MainActivity).benchmarkActivityComponent

sample/benchmark/src/main/java/com/datadog/benchmark/sample/di/activity/BenchmarkActivityComponent.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
package com.datadog.benchmark.sample.di.activity
88

99
import com.datadog.android.api.SdkCore
10+
import com.datadog.benchmark.sample.DatadogFeaturesInitializer
1011
import com.datadog.benchmark.sample.MainActivity
1112
import com.datadog.benchmark.sample.config.BenchmarkConfig
1213
import com.datadog.benchmark.sample.ui.logscustom.LogsFragment
@@ -21,6 +22,7 @@ internal annotation class BenchmarkActivityScope
2122

2223
internal interface BenchmarkActivityComponentDependencies {
2324
val sdkCore: SdkCore
25+
val datadogFeaturesInitializer: DatadogFeaturesInitializer
2426
}
2527

2628
@Component(

sample/benchmark/src/main/java/com/datadog/benchmark/sample/navigation/FragmentsNavigationManagerImpl.kt

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,25 +47,24 @@ private fun NavController.createNavGraph(scenario: SyntheticsScenario?): NavGrap
4747

4848
private fun createStartDestination(scenario: SyntheticsScenario?): String {
4949
return when (scenario) {
50-
SyntheticsScenario.SessionReplay -> SESSION_REPLAY_METERIAL_FRAGMENT_KEY
51-
SyntheticsScenario.SessionReplayCompose -> TODO()
52-
SyntheticsScenario.Rum -> TODO()
53-
SyntheticsScenario.Trace -> TODO()
50+
SyntheticsScenario.SessionReplay, SyntheticsScenario.Upload -> SESSION_REPLAY_METERIAL_FRAGMENT_KEY
51+
SyntheticsScenario.SessionReplayCompose -> error("Using fragments for SessionReplayCompose scenario")
52+
SyntheticsScenario.Rum -> TODO("RUM-9510")
53+
SyntheticsScenario.Trace -> TODO("RUM-9509")
5454
null, SyntheticsScenario.LogsCustom -> LOGS_FRAGMENT_KEY
55-
SyntheticsScenario.Upload -> TODO()
56-
SyntheticsScenario.LogsHeavyTraffic -> TODO()
55+
SyntheticsScenario.LogsHeavyTraffic -> TODO("RUM-9508")
5756
}
5857
}
5958

6059
private fun NavGraphBuilder.navGraph(scenario: SyntheticsScenario?) {
6160
return when (scenario) {
62-
SyntheticsScenario.SessionReplay -> navGraphSessionReplay()
61+
SyntheticsScenario.SessionReplay,
62+
SyntheticsScenario.Upload -> navGraphSessionReplay()
6363
SyntheticsScenario.SessionReplayCompose -> error("Using fragments for SessionReplayCompose scenario")
64-
SyntheticsScenario.Rum -> TODO()
65-
SyntheticsScenario.Trace -> TODO()
64+
SyntheticsScenario.Rum -> TODO("RUM-9510")
65+
SyntheticsScenario.Trace -> TODO("RUM-9509")
6666
null, SyntheticsScenario.LogsCustom -> navGraphLogs()
67-
SyntheticsScenario.Upload -> TODO()
68-
SyntheticsScenario.LogsHeavyTraffic -> TODO()
67+
SyntheticsScenario.LogsHeavyTraffic -> TODO("RUM-9508")
6968
}
7069
}
7170

sample/benchmark/src/main/java/com/datadog/benchmark/sample/ui/logscustom/LogPayloadUtils.kt

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,49 +19,49 @@ internal fun LogPayloadSize.createLogAttributes(): Map<String, Any?> {
1919
}
2020

2121
private val MEDIUM_ATTRIBUTES_PAYLOAD = mapOf(
22-
"user" to mapOf(
22+
"benchmark_user" to mapOf(
2323
"id" to UUID.randomUUID().toString(),
2424
"name" to "John Doe",
2525
"email" to "[email protected]"
2626
),
27-
"device" to mapOf(
28-
"type" to "iPhone",
29-
"os" to "iOS 17.0"
27+
"benchmark_device" to mapOf(
28+
"type" to "Android Phone",
29+
"os" to "Android 15"
3030
),
31-
"log_type" to "user_event"
31+
"benchmark_log_type" to "user_event"
3232
)
3333

3434
private val LARGE_ATTRIBUTES_PAYLOAD = mapOf(
35-
"log_type" to "user_event",
36-
"session" to mapOf(
35+
"benchmark_log_type" to "user_event",
36+
"benchmark_session" to mapOf(
3737
"id" to UUID.randomUUID().toString(),
3838
"startTime" to "2024-02-27T12:00:00Z",
3939
"duration" to "2450"
4040
),
41-
"user" to mapOf(
41+
"benchmark_user" to mapOf(
4242
"id" to "a1b2c3d4-e5f6-7g8h-9i0j-k1l2m3n4o5p6",
4343
"name" to "John Doe",
4444
"email" to "[email protected]"
4545
),
46-
"location" to mapOf(
46+
"benchmark_location" to mapOf(
4747
"city" to "San Francisco",
4848
"country" to "USA"
4949
),
50-
"device" to mapOf(
51-
"model" to "iPhone 15 Pro",
52-
"os" to "iOS 17.2",
50+
"benchmark_device" to mapOf(
51+
"model" to "Google Pixel 8",
52+
"os" to "Android 15",
5353
"battery" to "80%"
5454
),
55-
"network" to mapOf(
55+
"benchmark_network" to mapOf(
5656
"type" to "WiFi",
5757
"carrier" to "Verizon"
5858
),
59-
"errorStack" to mapOf(
59+
"benchmark_errorStack" to mapOf(
6060
"stackTrace" to "Error at module XYZ -> function ABC",
6161
"crashType" to "NullPointerException"
6262
)
6363
)
6464

6565
private val SMALL_ATTRIBUTES_PAYLOAD = mapOf(
66-
"log_type" to "simple"
66+
"benchmark_log_type" to "simple"
6767
)

sample/benchmark/src/main/java/com/datadog/benchmark/sample/ui/logscustom/LogsFragment.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import androidx.compose.ui.platform.ComposeView
1818
import androidx.fragment.app.Fragment
1919
import androidx.fragment.app.viewModels
2020
import androidx.lifecycle.ViewModelProvider
21-
import com.datadog.benchmark.sample.MainActivity
21+
import com.datadog.benchmark.sample.benchmarkActivityComponent
2222
import com.datadog.benchmark.sample.di.activity.ViewModel
2323
import javax.inject.Inject
2424

@@ -29,7 +29,7 @@ internal class LogsFragment : Fragment() {
2929
internal lateinit var viewModelFactory: ViewModelProvider.Factory
3030

3131
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
32-
(requireActivity() as MainActivity).benchmarkActivityComponent.inject(this)
32+
requireActivity().benchmarkActivityComponent.inject(this)
3333

3434
val viewModel by viewModels<LogsScreenViewModel> { viewModelFactory }
3535

sample/benchmark/src/main/java/com/datadog/benchmark/sample/ui/sessionreplay/SessionReplayAppcompatFragment.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import androidx.fragment.app.Fragment
2323
import com.bumptech.glide.Glide
2424
import com.bumptech.glide.request.target.CustomTarget
2525
import com.bumptech.glide.request.transition.Transition
26-
import com.datadog.benchmark.sample.MainActivity
26+
import com.datadog.benchmark.sample.benchmarkActivityComponent
2727
import com.datadog.benchmark.sample.navigation.FragmentsNavigationManager
2828
import com.datadog.sample.benchmark.R
2929
import com.google.android.material.datepicker.MaterialDatePicker
@@ -41,7 +41,7 @@ internal class SessionReplayAppcompatFragment : Fragment() {
4141
container: ViewGroup?,
4242
savedInstanceState: Bundle?
4343
): View? {
44-
(requireActivity() as MainActivity).benchmarkActivityComponent.inject(this)
44+
requireActivity().benchmarkActivityComponent.inject(this)
4545

4646
return inflater.inflate(R.layout.fragment_session_replay_compat, container, false).apply {
4747
findViewById<CheckedTextView>(R.id.checked_text_view).apply {

sample/benchmark/src/main/java/com/datadog/benchmark/sample/ui/sessionreplay/SessionReplayMaterialFragment.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import androidx.fragment.app.Fragment
2323
import com.bumptech.glide.Glide
2424
import com.bumptech.glide.request.target.CustomTarget
2525
import com.bumptech.glide.request.transition.Transition
26-
import com.datadog.benchmark.sample.MainActivity
26+
import com.datadog.benchmark.sample.benchmarkActivityComponent
2727
import com.datadog.benchmark.sample.navigation.FragmentsNavigationManager
2828
import com.datadog.sample.benchmark.R
2929
import com.google.android.material.datepicker.MaterialDatePicker
@@ -40,7 +40,7 @@ internal class SessionReplayMaterialFragment : Fragment() {
4040
container: ViewGroup?,
4141
savedInstanceState: Bundle?
4242
): View? {
43-
(requireActivity() as MainActivity).benchmarkActivityComponent.inject(this)
43+
requireActivity().benchmarkActivityComponent.inject(this)
4444

4545
return inflater.inflate(R.layout.fragment_session_replay_material, container, false).apply {
4646
findViewById<CheckedTextView>(R.id.checked_text_view).apply {

sample/benchmark/src/test/java/com/datadog/benchmark/sample/config/DatadogFeaturesInitializerTest.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,8 @@ class DatadogFeaturesInitializerTest {
101101
Mockito.mockStatic(Logs::class.java).use { logsStatic ->
102102
// When
103103
DatadogFeaturesInitializer(
104-
sdkCore = mockSdkCore,
105-
config = config
106-
).initialize()
104+
sdkCore = mockSdkCore
105+
).initialize(config)
107106

108107
// Then
109108
thenBlock(MockedStatics(sessionReplayStatic, rumStatic, logsStatic))

0 commit comments

Comments
 (0)