Skip to content

Commit

Permalink
simplify config behavior implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
fractalwrench committed Nov 14, 2024
1 parent 4bac7e0 commit 35e8b52
Show file tree
Hide file tree
Showing 72 changed files with 440 additions and 473 deletions.
Original file line number Diff line number Diff line change
@@ -1,38 +1,24 @@
package io.embrace.android.embracesdk.internal.config

import io.embrace.android.embracesdk.internal.config.behavior.AnrBehavior
import io.embrace.android.embracesdk.internal.config.behavior.AnrBehaviorImpl
import io.embrace.android.embracesdk.internal.config.behavior.AppExitInfoBehavior
import io.embrace.android.embracesdk.internal.config.behavior.AppExitInfoBehaviorImpl
import io.embrace.android.embracesdk.internal.config.behavior.AutoDataCaptureBehavior
import io.embrace.android.embracesdk.internal.config.behavior.AutoDataCaptureBehaviorImpl
import io.embrace.android.embracesdk.internal.config.behavior.BackgroundActivityBehavior
import io.embrace.android.embracesdk.internal.config.behavior.BackgroundActivityBehaviorImpl
import io.embrace.android.embracesdk.internal.config.behavior.BehaviorThresholdCheck
import io.embrace.android.embracesdk.internal.config.behavior.BreadcrumbBehavior
import io.embrace.android.embracesdk.internal.config.behavior.BreadcrumbBehaviorImpl
import io.embrace.android.embracesdk.internal.config.behavior.DataCaptureEventBehavior
import io.embrace.android.embracesdk.internal.config.behavior.DataCaptureEventBehaviorImpl
import io.embrace.android.embracesdk.internal.config.behavior.LogMessageBehavior
import io.embrace.android.embracesdk.internal.config.behavior.LogMessageBehaviorImpl
import io.embrace.android.embracesdk.internal.config.behavior.NetworkBehavior
import io.embrace.android.embracesdk.internal.config.behavior.NetworkBehaviorImpl
import io.embrace.android.embracesdk.internal.config.behavior.NetworkSpanForwardingBehavior
import io.embrace.android.embracesdk.internal.config.behavior.NetworkSpanForwardingBehaviorImpl
import io.embrace.android.embracesdk.internal.config.behavior.SdkModeBehavior
import io.embrace.android.embracesdk.internal.config.behavior.SdkModeBehaviorImpl
import io.embrace.android.embracesdk.internal.config.behavior.SensitiveKeysBehavior
import io.embrace.android.embracesdk.internal.config.behavior.SensitiveKeysBehaviorImpl
import io.embrace.android.embracesdk.internal.config.behavior.SessionBehavior
import io.embrace.android.embracesdk.internal.config.behavior.SessionBehaviorImpl
import io.embrace.android.embracesdk.internal.config.behavior.WebViewVitalsBehavior
import io.embrace.android.embracesdk.internal.config.behavior.WebViewVitalsBehaviorImpl
import io.embrace.android.embracesdk.internal.config.instrumented.schema.InstrumentedConfig
import io.embrace.android.embracesdk.internal.config.remote.RemoteConfig
import io.embrace.android.embracesdk.internal.opentelemetry.OpenTelemetryConfiguration
import io.embrace.android.embracesdk.internal.payload.AppFramework
import io.embrace.android.embracesdk.internal.prefs.PreferencesService
import io.embrace.android.embracesdk.internal.utils.Provider

/**
* Loads configuration for the app from the Embrace API.
Expand All @@ -41,89 +27,26 @@ internal class ConfigServiceImpl(
openTelemetryCfg: OpenTelemetryConfiguration,
preferencesService: PreferencesService,
suppliedFramework: AppFramework,
configProvider: Provider<RemoteConfig?>,
thresholdCheck: BehaviorThresholdCheck = BehaviorThresholdCheck { preferencesService.deviceIdentifier },
instrumentedConfig: InstrumentedConfig,
remoteConfig: RemoteConfig?,
thresholdCheck: BehaviorThresholdCheck = BehaviorThresholdCheck { preferencesService.deviceIdentifier },
) : ConfigService {

override val backgroundActivityBehavior: BackgroundActivityBehavior =
BackgroundActivityBehaviorImpl(
thresholdCheck = thresholdCheck,
remoteSupplier = { configProvider()?.backgroundActivityConfig },
instrumentedConfig = instrumentedConfig
)

override val autoDataCaptureBehavior: AutoDataCaptureBehavior =
AutoDataCaptureBehaviorImpl(
thresholdCheck = thresholdCheck,
remoteSupplier = configProvider,
instrumentedConfig = instrumentedConfig
)

override val breadcrumbBehavior: BreadcrumbBehavior =
BreadcrumbBehaviorImpl(
thresholdCheck,
remoteSupplier = configProvider,
instrumentedConfig = instrumentedConfig
)

override val sensitiveKeysBehavior: SensitiveKeysBehavior =
SensitiveKeysBehaviorImpl(instrumentedConfig = instrumentedConfig)

override val logMessageBehavior: LogMessageBehavior =
LogMessageBehaviorImpl(
thresholdCheck,
remoteSupplier = { configProvider()?.logConfig }
)

override val anrBehavior: AnrBehavior =
AnrBehaviorImpl(
thresholdCheck,
remoteSupplier = { configProvider()?.anrConfig },
instrumentedConfig = instrumentedConfig
)

override val sessionBehavior: SessionBehavior = SessionBehaviorImpl(
thresholdCheck,
remoteSupplier = configProvider,
instrumentedConfig = instrumentedConfig
)

override val networkBehavior: NetworkBehavior =
NetworkBehaviorImpl(
thresholdCheck = thresholdCheck,
remoteSupplier = configProvider,
instrumentedConfig = instrumentedConfig
)

override val dataCaptureEventBehavior: DataCaptureEventBehavior = DataCaptureEventBehaviorImpl(
thresholdCheck = thresholdCheck,
remoteSupplier = configProvider
)

override val sdkModeBehavior: SdkModeBehavior = SdkModeBehaviorImpl(
thresholdCheck = thresholdCheck,
remoteSupplier = configProvider
)

override val appExitInfoBehavior: AppExitInfoBehavior = AppExitInfoBehaviorImpl(
thresholdCheck = thresholdCheck,
remoteSupplier = configProvider,
instrumentedConfig = instrumentedConfig
)

override val networkSpanForwardingBehavior: NetworkSpanForwardingBehavior =
NetworkSpanForwardingBehaviorImpl(
thresholdCheck = thresholdCheck,
remoteSupplier = { configProvider()?.networkSpanForwardingRemoteConfig },
instrumentedConfig = instrumentedConfig
)

override val webViewVitalsBehavior: WebViewVitalsBehavior =
WebViewVitalsBehaviorImpl(
thresholdCheck = thresholdCheck,
remoteSupplier = configProvider
)
override val backgroundActivityBehavior =
BackgroundActivityBehaviorImpl(thresholdCheck, instrumentedConfig, remoteConfig)
override val autoDataCaptureBehavior = AutoDataCaptureBehaviorImpl(thresholdCheck, instrumentedConfig, remoteConfig)
override val breadcrumbBehavior = BreadcrumbBehaviorImpl(instrumentedConfig, remoteConfig)
override val sensitiveKeysBehavior = SensitiveKeysBehaviorImpl(instrumentedConfig)
override val logMessageBehavior = LogMessageBehaviorImpl(remoteConfig)
override val anrBehavior = AnrBehaviorImpl(thresholdCheck, instrumentedConfig, remoteConfig)
override val sessionBehavior = SessionBehaviorImpl(instrumentedConfig, remoteConfig)
override val networkBehavior = NetworkBehaviorImpl(instrumentedConfig, remoteConfig)
override val dataCaptureEventBehavior = DataCaptureEventBehaviorImpl(remoteConfig)
override val sdkModeBehavior = SdkModeBehaviorImpl(thresholdCheck, remoteConfig)
override val appExitInfoBehavior = AppExitInfoBehaviorImpl(thresholdCheck, instrumentedConfig, remoteConfig)
override val webViewVitalsBehavior = WebViewVitalsBehaviorImpl(thresholdCheck, remoteConfig)
override val networkSpanForwardingBehavior =
NetworkSpanForwardingBehaviorImpl(thresholdCheck, instrumentedConfig, remoteConfig)

override val appId: String? = resolveAppId(instrumentedConfig.project.getAppId(), openTelemetryCfg)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package io.embrace.android.embracesdk.internal.config.behavior

import io.embrace.android.embracesdk.internal.config.instrumented.schema.EnabledFeatureConfig
import io.embrace.android.embracesdk.internal.config.remote.AllowedNdkSampleMethod
import io.embrace.android.embracesdk.internal.config.remote.AnrRemoteConfig
import io.embrace.android.embracesdk.internal.config.remote.Unwinder

interface AnrBehavior {
interface AnrBehavior : ConfigBehavior<EnabledFeatureConfig, AnrRemoteConfig> {

/**
* Percentage of users for which ANR stack trace capture is enabled.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
package io.embrace.android.embracesdk.internal.config.behavior

import io.embrace.android.embracesdk.internal.config.UnimplementedConfig
import io.embrace.android.embracesdk.internal.config.instrumented.schema.InstrumentedConfig
import io.embrace.android.embracesdk.internal.config.remote.AllowedNdkSampleMethod
import io.embrace.android.embracesdk.internal.config.remote.AnrRemoteConfig
import io.embrace.android.embracesdk.internal.config.remote.RemoteConfig
import io.embrace.android.embracesdk.internal.config.remote.Unwinder
import io.embrace.android.embracesdk.internal.utils.Provider

/**
* Provides the behavior that the ANR feature should follow.
*/
class AnrBehaviorImpl(
thresholdCheck: BehaviorThresholdCheck,
remoteSupplier: Provider<AnrRemoteConfig?>,
private val instrumentedConfig: InstrumentedConfig,
) : AnrBehavior, MergedConfigBehavior<UnimplementedConfig, AnrRemoteConfig>(
thresholdCheck = thresholdCheck,
remoteSupplier = remoteSupplier
) {
private val thresholdCheck: BehaviorThresholdCheck,
local: InstrumentedConfig,
remote: RemoteConfig?,
) : AnrBehavior {

private companion object {
private const val DEFAULT_ANR_PCT_ENABLED = true
Expand All @@ -34,6 +29,9 @@ class AnrBehaviorImpl(
)
}

override val local = local.enabledFeatures
override val remote = remote?.anrConfig

override fun isAnrCaptureEnabled(): Boolean {
return thresholdCheck.isBehaviorEnabled(remote?.pctEnabled)
?: DEFAULT_ANR_PCT_ENABLED
Expand Down Expand Up @@ -65,7 +63,7 @@ class AnrBehaviorImpl(

override fun isUnityAnrCaptureEnabled(): Boolean {
return thresholdCheck.isBehaviorEnabled(remote?.pctNativeThreadAnrSamplingEnabled)
?: instrumentedConfig.enabledFeatures.isUnityAnrCaptureEnabled()
?: local.isUnityAnrCaptureEnabled()
}

override fun isNativeThreadAnrSamplingOffsetEnabled(): Boolean =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package io.embrace.android.embracesdk.internal.config.behavior

interface AppExitInfoBehavior {
import io.embrace.android.embracesdk.internal.config.instrumented.schema.EnabledFeatureConfig
import io.embrace.android.embracesdk.internal.config.remote.AppExitInfoConfig

interface AppExitInfoBehavior : ConfigBehavior<EnabledFeatureConfig, AppExitInfoConfig> {

fun getTraceMaxLimit(): Int

Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
package io.embrace.android.embracesdk.internal.config.behavior

import io.embrace.android.embracesdk.internal.config.UnimplementedConfig
import io.embrace.android.embracesdk.internal.config.instrumented.schema.InstrumentedConfig
import io.embrace.android.embracesdk.internal.config.remote.RemoteConfig
import io.embrace.android.embracesdk.internal.utils.Provider

/**
* Provides the behavior that should be followed for select services that automatically
* capture data.
*/
class AppExitInfoBehaviorImpl(
thresholdCheck: BehaviorThresholdCheck,
remoteSupplier: Provider<RemoteConfig?>,
private val instrumentedConfig: InstrumentedConfig,
) : AppExitInfoBehavior, MergedConfigBehavior<UnimplementedConfig, RemoteConfig>(
thresholdCheck = thresholdCheck,
remoteSupplier = remoteSupplier
) {
private val thresholdCheck: BehaviorThresholdCheck,
local: InstrumentedConfig,
remote: RemoteConfig?,
) : AppExitInfoBehavior {

companion object {
/**
* Max size of bytes to allow capturing AppExitInfo ndk/anr traces
Expand All @@ -25,14 +21,17 @@ class AppExitInfoBehaviorImpl(
const val AEI_MAX_NUM_DEFAULT: Int = 0 // 0 means no limit
}

override val local = local.enabledFeatures
override val remote = remote?.appExitInfoConfig

override fun getTraceMaxLimit(): Int =
remote?.appExitInfoConfig?.appExitInfoTracesLimit
remote?.appExitInfoTracesLimit
?: MAX_TRACE_SIZE_BYTES

override fun isAeiCaptureEnabled(): Boolean {
return thresholdCheck.isBehaviorEnabled(remote?.appExitInfoConfig?.pctAeiCaptureEnabled)
?: instrumentedConfig.enabledFeatures.isAeiCaptureEnabled()
return thresholdCheck.isBehaviorEnabled(remote?.pctAeiCaptureEnabled)
?: local.isAeiCaptureEnabled()
}

override fun appExitInfoMaxNum(): Int = remote?.appExitInfoConfig?.aeiMaxNum ?: AEI_MAX_NUM_DEFAULT
override fun appExitInfoMaxNum(): Int = remote?.aeiMaxNum ?: AEI_MAX_NUM_DEFAULT
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package io.embrace.android.embracesdk.internal.config.behavior

interface AutoDataCaptureBehavior {
import io.embrace.android.embracesdk.internal.config.instrumented.schema.EnabledFeatureConfig
import io.embrace.android.embracesdk.internal.config.remote.RemoteConfig

interface AutoDataCaptureBehavior : ConfigBehavior<EnabledFeatureConfig, RemoteConfig> {

/**
* Returns true if [io.embrace.android.embracesdk.MemoryService] should
Expand Down
Original file line number Diff line number Diff line change
@@ -1,53 +1,48 @@
package io.embrace.android.embracesdk.internal.config.behavior

import io.embrace.android.embracesdk.internal.config.UnimplementedConfig
import io.embrace.android.embracesdk.internal.config.instrumented.schema.InstrumentedConfig
import io.embrace.android.embracesdk.internal.config.remote.RemoteConfig
import io.embrace.android.embracesdk.internal.utils.Provider

/**
* Provides the behavior that should be followed for select services that automatically
* capture data.
*/
class AutoDataCaptureBehaviorImpl(
thresholdCheck: BehaviorThresholdCheck,
remoteSupplier: Provider<RemoteConfig?>,
instrumentedConfig: InstrumentedConfig,
) : AutoDataCaptureBehavior, MergedConfigBehavior<UnimplementedConfig, RemoteConfig>(
thresholdCheck = thresholdCheck,
remoteSupplier = remoteSupplier
) {
private val thresholdCheck: BehaviorThresholdCheck,
local: InstrumentedConfig,
override val remote: RemoteConfig?,
) : AutoDataCaptureBehavior {

private companion object {
const val THERMAL_STATUS_ENABLED_DEFAULT = true
const val V2_STORAGE_ENABLED_DEFAULT = true
const val USE_OKHTTP_DEFAULT = true
}

private val cfg = instrumentedConfig.enabledFeatures
override val local = local.enabledFeatures

@Suppress("DEPRECATION")
override fun isMemoryWarningCaptureEnabled(): Boolean = cfg.isMemoryWarningCaptureEnabled()
override fun isMemoryWarningCaptureEnabled(): Boolean = local.isMemoryWarningCaptureEnabled()

override fun isThermalStatusCaptureEnabled(): Boolean {
return thresholdCheck.isBehaviorEnabled(remote?.dataConfig?.pctThermalStatusEnabled)
?: THERMAL_STATUS_ENABLED_DEFAULT
}

override fun isPowerSaveModeCaptureEnabled(): Boolean = cfg.isPowerSaveModeCaptureEnabled()
override fun isPowerSaveModeCaptureEnabled(): Boolean = local.isPowerSaveModeCaptureEnabled()
override fun isNetworkConnectivityCaptureEnabled(): Boolean =
cfg.isNetworkConnectivityCaptureEnabled()
local.isNetworkConnectivityCaptureEnabled()

override fun isAnrCaptureEnabled(): Boolean = cfg.isAnrCaptureEnabled()
override fun isJvmCrashCaptureEnabled(): Boolean = cfg.isJvmCrashCaptureEnabled()
override fun isAnrCaptureEnabled(): Boolean = local.isAnrCaptureEnabled()
override fun isJvmCrashCaptureEnabled(): Boolean = local.isJvmCrashCaptureEnabled()
override fun isComposeClickCaptureEnabled(): Boolean =
remote?.killSwitchConfig?.jetpackCompose ?: cfg.isComposeClickCaptureEnabled()
remote?.killSwitchConfig?.jetpackCompose ?: local.isComposeClickCaptureEnabled()

override fun is3rdPartySigHandlerDetectionEnabled(): Boolean =
remote?.killSwitchConfig?.sigHandlerDetection ?: cfg.is3rdPartySigHandlerDetectionEnabled()
remote?.killSwitchConfig?.sigHandlerDetection ?: local.is3rdPartySigHandlerDetectionEnabled()

override fun isNativeCrashCaptureEnabled(): Boolean = cfg.isNativeCrashCaptureEnabled()
override fun isDiskUsageCaptureEnabled(): Boolean = cfg.isDiskUsageCaptureEnabled()
override fun isNativeCrashCaptureEnabled(): Boolean = local.isNativeCrashCaptureEnabled()
override fun isDiskUsageCaptureEnabled(): Boolean = local.isDiskUsageCaptureEnabled()

private val v2StorageImpl by lazy {
thresholdCheck.isBehaviorEnabled(remote?.killSwitchConfig?.v2StoragePct) ?: V2_STORAGE_ENABLED_DEFAULT
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package io.embrace.android.embracesdk.internal.config.behavior

interface BackgroundActivityBehavior {
import io.embrace.android.embracesdk.internal.config.instrumented.schema.EnabledFeatureConfig
import io.embrace.android.embracesdk.internal.config.remote.BackgroundActivityRemoteConfig

interface BackgroundActivityBehavior : ConfigBehavior<EnabledFeatureConfig, BackgroundActivityRemoteConfig> {

/**
* Whether the feature is enabled or not.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
package io.embrace.android.embracesdk.internal.config.behavior

import io.embrace.android.embracesdk.internal.config.UnimplementedConfig
import io.embrace.android.embracesdk.internal.config.instrumented.schema.EnabledFeatureConfig
import io.embrace.android.embracesdk.internal.config.instrumented.schema.InstrumentedConfig
import io.embrace.android.embracesdk.internal.config.remote.BackgroundActivityRemoteConfig
import io.embrace.android.embracesdk.internal.utils.Provider
import io.embrace.android.embracesdk.internal.config.remote.RemoteConfig

/**
* Provides the behavior that the Background Activity feature should follow.
*/
class BackgroundActivityBehaviorImpl(
thresholdCheck: BehaviorThresholdCheck,
remoteSupplier: Provider<BackgroundActivityRemoteConfig?>,
private val instrumentedConfig: InstrumentedConfig,
) : BackgroundActivityBehavior,
MergedConfigBehavior<UnimplementedConfig, BackgroundActivityRemoteConfig>(
thresholdCheck = thresholdCheck,
remoteSupplier = remoteSupplier
) {
private val thresholdCheck: BehaviorThresholdCheck,
local: InstrumentedConfig,
remote: RemoteConfig?,
) : BackgroundActivityBehavior {

override val local: EnabledFeatureConfig = local.enabledFeatures
override val remote: BackgroundActivityRemoteConfig? = remote?.backgroundActivityConfig

override fun isBackgroundActivityCaptureEnabled(): Boolean {
return remote?.threshold?.let(thresholdCheck::isBehaviorEnabled)
?: instrumentedConfig.enabledFeatures.isBackgroundActivityCaptureEnabled()
?: local.isBackgroundActivityCaptureEnabled()
}

override fun getManualBackgroundActivityLimit(): Int = 100
Expand Down
Loading

0 comments on commit 35e8b52

Please sign in to comment.