|
| 1 | +/* |
| 2 | + * Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. |
| 3 | + * This product includes software developed at Datadog (https://www.datadoghq.com/). |
| 4 | + * Copyright 2016-Present Datadog, Inc. |
| 5 | + */ |
| 6 | + |
| 7 | +package com.datadog.benchmark.sample |
| 8 | + |
| 9 | +import com.datadog.android.api.SdkCore |
| 10 | +import com.datadog.android.compose.enableComposeActionTracking |
| 11 | +import com.datadog.android.log.Logs |
| 12 | +import com.datadog.android.log.LogsConfiguration |
| 13 | +import com.datadog.android.rum.Rum |
| 14 | +import com.datadog.android.rum.RumConfiguration |
| 15 | +import com.datadog.android.rum.tracking.NavigationViewTrackingStrategy |
| 16 | +import com.datadog.android.sessionreplay.SessionReplay |
| 17 | +import com.datadog.android.sessionreplay.SessionReplayConfiguration |
| 18 | +import com.datadog.android.sessionreplay.SessionReplayPrivacy |
| 19 | +import com.datadog.android.sessionreplay.compose.ComposeExtensionSupport |
| 20 | +import com.datadog.android.sessionreplay.material.MaterialExtensionSupport |
| 21 | +import com.datadog.benchmark.sample.config.BenchmarkConfig |
| 22 | +import com.datadog.benchmark.sample.config.SyntheticsRun |
| 23 | +import com.datadog.benchmark.sample.config.SyntheticsScenario |
| 24 | +import com.datadog.benchmark.sample.navigation.BenchmarkNavigationPredicate |
| 25 | +import com.datadog.sample.benchmark.BuildConfig |
| 26 | +import com.datadog.sample.benchmark.R |
| 27 | +import javax.inject.Inject |
| 28 | +import javax.inject.Singleton |
| 29 | + |
| 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 |
| 36 | +@Suppress("TooManyFunctions") |
| 37 | +internal class DatadogFeaturesInitializer @Inject constructor( |
| 38 | + private val sdkCore: SdkCore |
| 39 | +) { |
| 40 | + private var isInitialized = false |
| 41 | + fun initialize(config: BenchmarkConfig) { |
| 42 | + if (isInitialized) { |
| 43 | + return |
| 44 | + } |
| 45 | + isInitialized = true |
| 46 | + |
| 47 | + if (needToEnableRum(config)) { |
| 48 | + enableRum() |
| 49 | + } |
| 50 | + |
| 51 | + if (needToEnableLogs(config)) { |
| 52 | + enableLogs() |
| 53 | + } |
| 54 | + |
| 55 | + if (needToEnableSessionReplay(config)) { |
| 56 | + enableSessionReplay() |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + private fun needToEnableSessionReplay(config: BenchmarkConfig): Boolean { |
| 61 | + return isInstrumentedRun(config) && isSessionReplayScenario(config) |
| 62 | + } |
| 63 | + |
| 64 | + @Suppress("DEPRECATION") |
| 65 | + private fun enableSessionReplay() { |
| 66 | + val sessionReplayConfig = SessionReplayConfiguration |
| 67 | + .Builder(SAMPLE_IN_ALL_SESSIONS) |
| 68 | + .setPrivacy(SessionReplayPrivacy.ALLOW) |
| 69 | + .addExtensionSupport(MaterialExtensionSupport()) |
| 70 | + .addExtensionSupport(ComposeExtensionSupport()) |
| 71 | + .build() |
| 72 | + |
| 73 | + SessionReplay.enable(sessionReplayConfig, sdkCore) |
| 74 | + } |
| 75 | + |
| 76 | + private fun needToEnableLogs(config: BenchmarkConfig): Boolean { |
| 77 | + return isInstrumentedRun(config) && isLogsScenario(config) |
| 78 | + } |
| 79 | + |
| 80 | + private fun enableLogs() { |
| 81 | + val logsConfig = LogsConfiguration.Builder().build() |
| 82 | + Logs.enable(logsConfig, sdkCore) |
| 83 | + } |
| 84 | + |
| 85 | + private fun needToEnableRum(config: BenchmarkConfig): Boolean { |
| 86 | + return when (isInstrumentedRun(config)) { |
| 87 | + true -> isSessionReplayScenario(config) || isRumScenario(config) |
| 88 | + false -> isSessionReplayScenario(config) |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + private fun createRumConfiguration(): RumConfiguration { |
| 93 | + return RumConfiguration.Builder(BuildConfig.BENCHMARK_RUM_APPLICATION_ID) |
| 94 | + .useViewTrackingStrategy( |
| 95 | + NavigationViewTrackingStrategy( |
| 96 | + R.id.nav_host_fragment, |
| 97 | + true, |
| 98 | + BenchmarkNavigationPredicate() |
| 99 | + ) |
| 100 | + ) |
| 101 | + .setTelemetrySampleRate(SAMPLE_RATE_TELEMETRY) |
| 102 | + .trackUserInteractions() |
| 103 | + .trackLongTasks(THRESHOLD_LONG_TASK_INTERVAL) |
| 104 | + .trackNonFatalAnrs(true) |
| 105 | + .setViewEventMapper { event -> |
| 106 | + event.context?.additionalProperties?.put(ATTR_IS_MAPPED, true) |
| 107 | + event |
| 108 | + } |
| 109 | + .setActionEventMapper { event -> |
| 110 | + event.context?.additionalProperties?.put(ATTR_IS_MAPPED, true) |
| 111 | + event |
| 112 | + } |
| 113 | + .setResourceEventMapper { event -> |
| 114 | + event.context?.additionalProperties?.put(ATTR_IS_MAPPED, true) |
| 115 | + event |
| 116 | + } |
| 117 | + .setErrorEventMapper { event -> |
| 118 | + event.context?.additionalProperties?.put(ATTR_IS_MAPPED, true) |
| 119 | + event |
| 120 | + } |
| 121 | + .setLongTaskEventMapper { event -> |
| 122 | + event.context?.additionalProperties?.put(ATTR_IS_MAPPED, true) |
| 123 | + event |
| 124 | + } |
| 125 | + .enableComposeActionTracking() |
| 126 | + .build() |
| 127 | + } |
| 128 | + |
| 129 | + private fun enableRum() { |
| 130 | + val rumConfig = createRumConfiguration() |
| 131 | + Rum.enable(rumConfig, sdkCore = sdkCore) |
| 132 | + } |
| 133 | + |
| 134 | + private fun isSessionReplayScenario(config: BenchmarkConfig) = when (config.scenario) { |
| 135 | + SyntheticsScenario.SessionReplayCompose, |
| 136 | + SyntheticsScenario.Upload, |
| 137 | + SyntheticsScenario.SessionReplay -> true |
| 138 | + SyntheticsScenario.Rum, |
| 139 | + SyntheticsScenario.Trace, |
| 140 | + SyntheticsScenario.LogsCustom, |
| 141 | + SyntheticsScenario.LogsHeavyTraffic, |
| 142 | + null -> false |
| 143 | + } |
| 144 | + |
| 145 | + private fun isRumScenario(config: BenchmarkConfig) = when (config.scenario) { |
| 146 | + SyntheticsScenario.Rum -> true |
| 147 | + SyntheticsScenario.SessionReplay, |
| 148 | + SyntheticsScenario.SessionReplayCompose, |
| 149 | + SyntheticsScenario.Trace, |
| 150 | + SyntheticsScenario.LogsCustom, |
| 151 | + SyntheticsScenario.LogsHeavyTraffic, |
| 152 | + SyntheticsScenario.Upload, |
| 153 | + null -> false |
| 154 | + } |
| 155 | + |
| 156 | + private fun isLogsScenario(config: BenchmarkConfig) = when (config.scenario) { |
| 157 | + SyntheticsScenario.LogsCustom, |
| 158 | + SyntheticsScenario.LogsHeavyTraffic -> true |
| 159 | + SyntheticsScenario.SessionReplay, |
| 160 | + SyntheticsScenario.SessionReplayCompose, |
| 161 | + SyntheticsScenario.Rum, |
| 162 | + SyntheticsScenario.Trace, |
| 163 | + SyntheticsScenario.Upload, |
| 164 | + null -> false |
| 165 | + } |
| 166 | + |
| 167 | + private fun isInstrumentedRun(config: BenchmarkConfig) = config.run == SyntheticsRun.Instrumented |
| 168 | + |
| 169 | + companion object { |
| 170 | + private const val SAMPLE_IN_ALL_SESSIONS = 100f |
| 171 | + private const val ATTR_IS_MAPPED = "is_mapped" |
| 172 | + private const val SAMPLE_RATE_TELEMETRY = 100f |
| 173 | + private const val THRESHOLD_LONG_TASK_INTERVAL = 250L |
| 174 | + } |
| 175 | +} |
0 commit comments