Skip to content

Commit

Permalink
Merge pull request #1641 from embrace-io/integration/config-hardening
Browse files Browse the repository at this point in the history
Config service hardening
  • Loading branch information
fractalwrench authored Nov 18, 2024
2 parents cc98016 + 59ccf25 commit a3d471a
Show file tree
Hide file tree
Showing 217 changed files with 2,428 additions and 2,520 deletions.
1 change: 1 addition & 0 deletions embrace-android-core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ dependencies {
compileOnly(libs.opentelemetry.semconv.incubating)
implementation(libs.lifecycle.runtime)
implementation(libs.lifecycle.process)
implementation(libs.okhttp)

testImplementation(platform(libs.opentelemetry.bom))
testImplementation(libs.opentelemetry.api)
Expand Down
2 changes: 1 addition & 1 deletion embrace-android-core/config/detekt/baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
<SmellBaseline>
<ManuallySuppressedIssues></ManuallySuppressedIssues>
<CurrentIssues>
<ID>LongParameterList:DeliveryModuleSupplier.kt$( configModule: ConfigModule, initModule: InitModule, otelModule: OpenTelemetryModule, workerThreadModule: WorkerThreadModule, coreModule: CoreModule, storageModule: StorageModule, essentialServiceModule: EssentialServiceModule, payloadStorageServiceProvider: Provider&lt;PayloadStorageService?&gt;, cacheStorageServiceProvider: Provider&lt;PayloadStorageService?&gt;, requestExecutionServiceProvider: Provider&lt;RequestExecutionService?&gt;, deliveryServiceProvider: Provider&lt;DeliveryService?&gt;, )</ID>
<ID>LongParameterList:DeliveryModuleSupplier.kt$( configModule: ConfigModule, initModule: InitModule, otelModule: OpenTelemetryModule, workerThreadModule: WorkerThreadModule, coreModule: CoreModule, storageModule: StorageModule, essentialServiceModule: EssentialServiceModule, androidServicesModule: AndroidServicesModule, payloadStorageServiceProvider: Provider&lt;PayloadStorageService&gt;?, cacheStorageServiceProvider: Provider&lt;PayloadStorageService&gt;?, requestExecutionServiceProvider: Provider&lt;RequestExecutionService&gt;?, deliveryServiceProvider: Provider&lt;DeliveryService&gt;?, )</ID>
</CurrentIssues>
</SmellBaseline>
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.embrace.android.embracesdk.internal.arch

import io.embrace.android.embracesdk.internal.arch.datasource.DataSourceState
import io.embrace.android.embracesdk.internal.config.ConfigService
import io.embrace.android.embracesdk.internal.logging.EmbLogger
import io.embrace.android.embracesdk.internal.logging.InternalErrorType
import io.embrace.android.embracesdk.internal.worker.BackgroundWorker
Expand All @@ -12,17 +11,10 @@ import java.util.concurrent.CopyOnWriteArrayList
* place to coordinate everything in one place.
*/
class DataCaptureOrchestrator(
configService: ConfigService,
private val worker: BackgroundWorker,
private val logger: EmbLogger,
) : EmbraceFeatureRegistry {

init {
configService.addListener {
onConfigChange()
}
}

private val dataSourceStates = CopyOnWriteArrayList<DataSourceState<*>>()

var currentSessionType: SessionType? = null
Expand All @@ -38,18 +30,6 @@ class DataCaptureOrchestrator(
}
}

private fun onConfigChange() {
dataSourceStates.forEach { state ->
try {
state.dispatchStateChange {
state.onConfigChange()
}
} catch (exc: Throwable) {
logger.trackInternalError(InternalErrorType.CFG_CHANGE_DATA_CAPTURE_FAIL, exc)
}
}
}

/**
* Callback that is invoked when the session type changes.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,6 @@ class DataSourceState<T : DataSource<*>>(
}
}

/**
* Callback that is invoked when the config layer experiences a change.
*/
fun onConfigChange() {
updateDataSource()
}

private fun updateDataSource() {
val enabled =
currentSessionType != null && currentSessionType != disabledSessionType && configGate()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,60 +2,49 @@ package io.embrace.android.embracesdk.internal.buildinfo

import android.content.res.Resources
import io.embrace.android.embracesdk.internal.AndroidResourcesService
import io.embrace.android.embracesdk.internal.config.instrumented.ProjectConfig
import io.embrace.android.embracesdk.internal.config.instrumented.schema.InstrumentedConfig

internal class BuildInfoServiceImpl(
resources: AndroidResourcesService,
packageName: String,
private val instrumentedConfig: InstrumentedConfig,
private val resources: AndroidResourcesService,
private val packageName: String,
) : BuildInfoService {

companion object {
private const val BUILD_INFO_RN_BUNDLE_ID: String = "emb_rn_bundle_id"
private const val RES_TYPE_STRING = "string"
}

private val info by lazy {
fromResources(resources, packageName)
BuildInfo(
instrumentedConfig.project.getBuildId(),
instrumentedConfig.project.getBuildType(),
instrumentedConfig.project.getBuildFlavor(),
getBuildResource(resources, packageName, BUILD_INFO_RN_BUNDLE_ID),
)
}

override fun getBuildInfo(): BuildInfo = info

companion object {
private const val BUILD_INFO_RN_BUNDLE_ID: String = "emb_rn_bundle_id"
private const val RES_TYPE_STRING = "string"

/**
* Loads the build information from resources provided by the config file packaged within the application by Gradle at
* build-time.
*
* @return the build information
*/
@JvmStatic
fun fromResources(resources: AndroidResourcesService, packageName: String): BuildInfo {
val buildInfo = BuildInfo(
ProjectConfig.getBuildId(),
ProjectConfig.getBuildType(),
ProjectConfig.getBuildFlavor(),
getBuildResource(resources, packageName, BUILD_INFO_RN_BUNDLE_ID),
/**
* Given a build property name and a build property type, retrieves the embrace build resource value.
*/
fun getBuildResource(
resources: AndroidResourcesService,
packageName: String,
buildProperty: String,
): String? {
return try {
val resourceId =
resources.getIdentifier(buildProperty, RES_TYPE_STRING, packageName)
resources.getString(resourceId)
} catch (ex: NullPointerException) {
throw IllegalArgumentException(
"No resource found for $buildProperty property. Failed to create build info.",
ex
)
return buildInfo
}

/**
* Given a build property name and a build property type, retrieves the embrace build resource value.
*/
fun getBuildResource(
resources: AndroidResourcesService,
packageName: String,
buildProperty: String,
): String? {
return try {
val resourceId =
resources.getIdentifier(buildProperty, RES_TYPE_STRING, packageName)
resources.getString(resourceId)
} catch (ex: NullPointerException) {
throw IllegalArgumentException(
"No resource found for $buildProperty property. Failed to create build info.",
ex
)
} catch (ex: Resources.NotFoundException) {
null
}
} catch (ex: Resources.NotFoundException) {
null
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ internal class ApiRequestMapper(
) {

private val apiUrlBuilders = Endpoint.values().associateWith {
urlBuilder.getEmbraceUrlWithSuffix(it.version, it.path)
urlBuilder.resolveUrl(it)
}

private fun Endpoint.asEmbraceUrl(): String = checkNotNull(apiUrlBuilders[this])
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package io.embrace.android.embracesdk.internal.comms.api

import io.embrace.android.embracesdk.internal.capture.connectivity.NetworkConnectivityListener
import io.embrace.android.embracesdk.internal.config.RemoteConfigSource
import io.embrace.android.embracesdk.internal.injection.SerializationAction
import io.embrace.android.embracesdk.internal.payload.Envelope
import io.embrace.android.embracesdk.internal.payload.LogPayload
import java.util.concurrent.Future

interface ApiService : RemoteConfigSource, NetworkConnectivityListener {
interface ApiService : NetworkConnectivityListener {

/**
* Sends a list of OTel Logs to the API.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,22 @@ package io.embrace.android.embracesdk.internal.comms.api
interface ApiUrlBuilder {

/**
* Returns the url used to fetch the config.
* The App ID that will be used in API requests.
*/
fun getConfigUrl(): String
val appId: String

/**
* The Device ID that will be used in API requests.
*/
val deviceId: String

/**
* Base URL for the data endpoint
*/
val baseDataUrl: String

/**
* Returns the url used to send data to Embrace.
*/
fun getEmbraceUrlWithSuffix(apiVersion: String, suffix: String): String
fun resolveUrl(endpoint: Endpoint): String
}
Loading

0 comments on commit a3d471a

Please sign in to comment.