Skip to content

Commit

Permalink
refactor: hide embrace impl behind interface
Browse files Browse the repository at this point in the history
  • Loading branch information
fractalwrench committed Jul 9, 2024
1 parent 2b2b448 commit 1064de2
Show file tree
Hide file tree
Showing 18 changed files with 85 additions and 158 deletions.
3 changes: 2 additions & 1 deletion embrace-android-sdk/api/embrace-android-sdk.api
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public final class io/embrace/android/embracesdk/BuildConfig {
public fun <init> ()V
}

public final class io/embrace/android/embracesdk/Embrace : io/embrace/android/embracesdk/internal/api/EmbraceAndroidApi, io/embrace/android/embracesdk/internal/api/EmbraceApi, io/embrace/android/embracesdk/internal/api/InternalInterfaceApi, io/embrace/android/embracesdk/internal/api/LogsApi, io/embrace/android/embracesdk/internal/api/MomentsApi, io/embrace/android/embracesdk/internal/api/NetworkRequestApi, io/embrace/android/embracesdk/internal/api/OTelApi, io/embrace/android/embracesdk/internal/api/SdkStateApi, io/embrace/android/embracesdk/internal/api/SessionApi, io/embrace/android/embracesdk/internal/api/UserApi, io/embrace/android/embracesdk/spans/TracingApi {
public final class io/embrace/android/embracesdk/Embrace : io/embrace/android/embracesdk/internal/api/SdkApi {
public fun addBreadcrumb (Ljava/lang/String;)V
public fun addLogRecordExporter (Lio/opentelemetry/sdk/logs/export/LogRecordExporter;)V
public fun addSessionProperty (Ljava/lang/String;Ljava/lang/String;Z)Z
Expand Down Expand Up @@ -59,6 +59,7 @@ public final class io/embrace/android/embracesdk/Embrace : io/embrace/android/em
public fun logMessage (Ljava/lang/String;Lio/embrace/android/embracesdk/Severity;Ljava/util/Map;)V
public fun logPushNotification (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/Integer;Ljava/lang/Integer;Ljava/lang/Boolean;Ljava/lang/Boolean;)V
public fun logWarning (Ljava/lang/String;)V
public fun logWebView (Ljava/lang/String;)V
public fun recordCompletedSpan (Ljava/lang/String;JJ)Z
public fun recordCompletedSpan (Ljava/lang/String;JJLio/embrace/android/embracesdk/spans/EmbraceSpan;)Z
public fun recordCompletedSpan (Ljava/lang/String;JJLio/embrace/android/embracesdk/spans/ErrorCode;)Z
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,13 @@
import java.util.List;
import java.util.Map;

import io.embrace.android.embracesdk.annotation.InternalApi;
import io.embrace.android.embracesdk.internal.EmbraceInternalInterface;
import io.embrace.android.embracesdk.internal.Systrace;
import io.embrace.android.embracesdk.internal.api.EmbraceAndroidApi;
import io.embrace.android.embracesdk.internal.api.EmbraceApi;
import io.embrace.android.embracesdk.internal.api.InternalInterfaceApi;
import io.embrace.android.embracesdk.internal.api.LogsApi;
import io.embrace.android.embracesdk.internal.api.MomentsApi;
import io.embrace.android.embracesdk.internal.api.NetworkRequestApi;
import io.embrace.android.embracesdk.internal.api.OTelApi;
import io.embrace.android.embracesdk.internal.api.SdkStateApi;
import io.embrace.android.embracesdk.internal.api.SessionApi;
import io.embrace.android.embracesdk.internal.api.UserApi;
import io.embrace.android.embracesdk.internal.api.SdkApi;
import io.embrace.android.embracesdk.network.EmbraceNetworkRequest;
import io.embrace.android.embracesdk.spans.EmbraceSpan;
import io.embrace.android.embracesdk.spans.EmbraceSpanEvent;
import io.embrace.android.embracesdk.spans.ErrorCode;
import io.embrace.android.embracesdk.spans.TracingApi;
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.sdk.logs.export.LogRecordExporter;
import io.opentelemetry.sdk.trace.export.SpanExporter;
Expand All @@ -40,18 +29,7 @@
*/
@SuppressLint("EmbracePublicApiPackageRule")
@SuppressWarnings("unused")
public final class Embrace implements
LogsApi,
MomentsApi,
NetworkRequestApi,
SessionApi,
UserApi,
TracingApi,
EmbraceApi,
EmbraceAndroidApi,
SdkStateApi,
OTelApi,
InternalInterfaceApi {
public final class Embrace implements SdkApi {

/**
* Singleton instance of the Embrace SDK.
Expand Down Expand Up @@ -641,6 +619,13 @@ public void trackWebViewPerformance(@NonNull String tag, @NonNull String message
}
}

@Override
public void logWebView(@Nullable String url) {
if (verifyNonNullParameters("logWebView", url)) {
impl.logWebView(url);

Check warning on line 625 in embrace-android-sdk/src/main/java/io/embrace/android/embracesdk/Embrace.java

View check run for this annotation

Codecov / codecov/patch

embrace-android-sdk/src/main/java/io/embrace/android/embracesdk/Embrace.java#L625

Added line #L625 was not covered by tests
}
}

@Nullable
@Override
public String getCurrentSessionId() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import io.embrace.android.embracesdk.internal.api.LogsApi
import io.embrace.android.embracesdk.internal.api.MomentsApi
import io.embrace.android.embracesdk.internal.api.NetworkRequestApi
import io.embrace.android.embracesdk.internal.api.OTelApi
import io.embrace.android.embracesdk.internal.api.SdkApi
import io.embrace.android.embracesdk.internal.api.SdkStateApi
import io.embrace.android.embracesdk.internal.api.SessionApi
import io.embrace.android.embracesdk.internal.api.UserApi
Expand Down Expand Up @@ -64,15 +65,16 @@ internal class EmbraceImpl @JvmOverloads constructor(
private val breadcrumbApiDelegate: BreadcrumbApiDelegate = BreadcrumbApiDelegate(bootstrapper, sdkCallChecker),
private val webviewApiDelegate: InternalWebViewApiDelegate =
InternalWebViewApiDelegate(bootstrapper, sdkCallChecker),
) : UserApi by userApiDelegate,
SessionApi by sessionApiDelegate,
NetworkRequestApi by networkRequestApiDelegate,
) : SdkApi,
LogsApi by logsApiDelegate,
MomentsApi by momentsApiDelegate,
NetworkRequestApi by networkRequestApiDelegate,
SessionApi by sessionApiDelegate,
UserApi by userApiDelegate,
TracingApi by bootstrapper.openTelemetryModule.embraceTracer,
ViewTrackingApi by viewTrackingApiDelegate,
SdkStateApi by sdkStateApiDelegate,
OTelApi by otelApiDelegate,
ViewTrackingApi by viewTrackingApiDelegate,
BreadcrumbApi by breadcrumbApiDelegate,
InternalWebViewApi by webviewApiDelegate,
InternalInterfaceApi {
Expand Down Expand Up @@ -106,6 +108,16 @@ internal class EmbraceImpl @JvmOverloads constructor(
private val nativeThreadSampler by embraceImplInject { bootstrapper.nativeModule.nativeThreadSamplerService }
private val nativeThreadSamplerInstaller by embraceImplInject { bootstrapper.nativeModule.nativeThreadSamplerInstaller }

override fun start(context: Context) = start(context, Embrace.AppFramework.NATIVE) { null }

Check warning on line 111 in embrace-android-sdk/src/main/java/io/embrace/android/embracesdk/EmbraceImpl.kt

View check run for this annotation

Codecov / codecov/patch

embrace-android-sdk/src/main/java/io/embrace/android/embracesdk/EmbraceImpl.kt#L111

Added line #L111 was not covered by tests
override fun start(context: Context, appFramework: Embrace.AppFramework) =
start(context, appFramework) { null }

Check warning on line 113 in embrace-android-sdk/src/main/java/io/embrace/android/embracesdk/EmbraceImpl.kt

View check run for this annotation

Codecov / codecov/patch

embrace-android-sdk/src/main/java/io/embrace/android/embracesdk/EmbraceImpl.kt#L113

Added line #L113 was not covered by tests

override fun start(context: Context, isDevMode: Boolean) =
start(context, Embrace.AppFramework.NATIVE) { null }

Check warning on line 116 in embrace-android-sdk/src/main/java/io/embrace/android/embracesdk/EmbraceImpl.kt

View check run for this annotation

Codecov / codecov/patch

embrace-android-sdk/src/main/java/io/embrace/android/embracesdk/EmbraceImpl.kt#L116

Added line #L116 was not covered by tests

override fun start(context: Context, isDevMode: Boolean, appFramework: Embrace.AppFramework) =
start(context, appFramework) { null }

Check warning on line 119 in embrace-android-sdk/src/main/java/io/embrace/android/embracesdk/EmbraceImpl.kt

View check run for this annotation

Codecov / codecov/patch

embrace-android-sdk/src/main/java/io/embrace/android/embracesdk/EmbraceImpl.kt#L119

Added line #L119 was not covered by tests

/**
* Starts instrumentation of the Android application using the Embrace SDK. This should be
* called during creation of the application, as early as possible.
Expand Down Expand Up @@ -281,7 +293,7 @@ internal class EmbraceImpl @JvmOverloads constructor(
*/
override val internalInterface get(): EmbraceInternalInterface {
val internalInterface = embraceInternalInterface
return if (isStarted() && internalInterface != null) {
return if (isStarted && internalInterface != null) {
internalInterface
} else {
uninitializedSdkInternalInterface
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import kotlin.reflect.KProperty
*/
internal inline fun <reified T> EmbraceImpl.embraceImplInject(
noinline provider: Provider<T>
): ReadOnlyProperty<Any?, T?> = EmbraceImplFieldDelegate(::isStarted, provider)
): ReadOnlyProperty<Any?, T?> = EmbraceImplFieldDelegate({ this.isStarted }, provider)

internal inline fun <reified T> embraceImplInject(
checker: SdkCallChecker,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,6 @@ internal interface EmbraceAndroidApi {
appFramework: Embrace.AppFramework
)

/**
* Whether or not the SDK has been started.
*
* @return true if the SDK is started, false otherwise
*/
val isStarted: Boolean

/**
* Records that a view 'started'. You should call this when your app starts displaying an
* activity, a fragment, a screen, or any custom UI element, and you want to capture a
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.embrace.android.embracesdk.internal.api

import io.embrace.android.embracesdk.spans.TracingApi

internal interface SdkApi :
LogsApi,
MomentsApi,
NetworkRequestApi,
SessionApi,
UserApi,
TracingApi,
EmbraceAndroidApi,
SdkStateApi,
OTelApi,
InternalInterfaceApi,
BreadcrumbApi,
InternalWebViewApi
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ internal interface SdkStateApi {
*
* @return true if the SDK is started, false otherwise
*/
fun isStarted(): Boolean
val isStarted: Boolean

/**
* Sets a custom app ID that overrides the one specified at build time. Must be called before
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ internal class FlutterInternalInterfaceImpl(
) : EmbraceInternalInterface by impl, FlutterInternalInterface {

override fun setEmbraceFlutterSdkVersion(version: String?) {
if (embrace.isStarted()) {
if (embrace.isStarted) {
if (version != null) {
hostedSdkVersionInfo.hostedSdkVersion = version
}
Expand All @@ -26,7 +26,7 @@ internal class FlutterInternalInterfaceImpl(
}

override fun setDartVersion(version: String?) {
if (embrace.isStarted()) {
if (embrace.isStarted) {
if (version != null) {
hostedSdkVersionInfo.hostedPlatformVersion = version
}
Expand Down Expand Up @@ -63,7 +63,7 @@ internal class FlutterInternalInterfaceImpl(
library: String?,
exceptionType: LogExceptionType
) {
if (embrace.isStarted()) {
if (embrace.isStarted) {
embrace.logMessage(
EventType.ERROR_LOG,
"Dart error",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ internal class ReactNativeInternalInterfaceImpl(
type: String?,
stacktrace: String?
) {
if (embrace.isStarted()) {
if (embrace.isStarted) {
val exception = JsException(name, message, type, stacktrace)
crashService.logUnhandledJsException(exception)
} else {
Expand All @@ -41,7 +41,7 @@ internal class ReactNativeInternalInterfaceImpl(
properties: Map<String, Any>,
stacktrace: String?
) {
if (embrace.isStarted()) {
if (embrace.isStarted) {
embrace.logMessage(
EventType.ERROR_LOG,
message,
Expand All @@ -58,7 +58,7 @@ internal class ReactNativeInternalInterfaceImpl(
}

override fun setJavaScriptPatchNumber(number: String?) {
if (embrace.isStarted()) {
if (embrace.isStarted) {
if (number == null) {
logger.logError("JavaScript patch number must not be null")
return
Expand All @@ -74,15 +74,15 @@ internal class ReactNativeInternalInterfaceImpl(
}

override fun setReactNativeSdkVersion(version: String?) {
if (embrace.isStarted()) {
if (embrace.isStarted) {
hostedSdkVersionInfo.hostedSdkVersion = version
} else {
logger.logSdkNotInitialized("set React Native SDK version")
}
}

override fun setReactNativeVersionNumber(version: String?) {
if (embrace.isStarted()) {
if (embrace.isStarted) {
if (version == null) {
logger.logError("ReactNative version must not be null")
return
Expand All @@ -106,7 +106,7 @@ internal class ReactNativeInternalInterfaceImpl(
}

private fun setJavaScriptBundleUrl(context: Context, url: String, didUpdate: Boolean? = null) {
if (embrace.isStarted()) {
if (embrace.isStarted) {
metadataService.setReactNativeBundleId(context, url, didUpdate)
} else {
logger.logSdkNotInitialized("set JavaScript bundle URL")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ internal class SdkStateApiDelegate(
*
* @return true if the SDK is started, false otherwise
*/
override fun isStarted(): Boolean = sdkCallChecker.started.get()
override val isStarted: Boolean
get() = sdkCallChecker.started.get()

/**
* Sets a custom app ID that overrides the one specified at build time. Must be called before
Expand All @@ -41,7 +42,7 @@ internal class SdkStateApiDelegate(
* @return true if the app ID could be set, false otherwise.
*/
override fun setAppId(appId: String): Boolean {
if (isStarted()) {
if (isStarted) {
logger.logError("You must set the custom app ID before the SDK is started.", null)
return false
}
Expand Down Expand Up @@ -83,7 +84,7 @@ internal class SdkStateApiDelegate(
return null
}

override fun getLastRunEndState(): Embrace.LastRunEndState = if (isStarted() && crashVerifier != null) {
override fun getLastRunEndState(): Embrace.LastRunEndState = if (isStarted && crashVerifier != null) {
if (crashVerifier?.didLastRunCrash() == true) {
Embrace.LastRunEndState.CRASH
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ internal class UnityInternalInterfaceImpl(
buildGuid: String?,
unitySdkVersion: String?
) {
if (embrace.isStarted()) {
if (embrace.isStarted) {
if (unityVersion == null || buildGuid == null) {
val sdkVersionMessage = unitySdkVersion ?: "null or previous than 1.7.5"
logger.logError(
Expand Down Expand Up @@ -56,7 +56,7 @@ internal class UnityInternalInterfaceImpl(
stacktrace: String?,
exceptionType: LogExceptionType
) {
if (embrace.isStarted()) {
if (embrace.isStarted) {
embrace.logMessage(
EventType.ERROR_LOG,
"Unity exception",
Expand Down
Loading

0 comments on commit 1064de2

Please sign in to comment.