Skip to content

Commit

Permalink
refactor: move arch classes to core package
Browse files Browse the repository at this point in the history
  • Loading branch information
fractalwrench committed Jul 16, 2024
1 parent 029717f commit b8d9570
Show file tree
Hide file tree
Showing 32 changed files with 131 additions and 402 deletions.
2 changes: 2 additions & 0 deletions embrace-android-core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ dependencies {
implementation(project(":embrace-android-payload"))
compileOnly(platform(libs.opentelemetry.bom))
compileOnly(libs.opentelemetry.api)
compileOnly(libs.opentelemetry.semconv)
compileOnly(libs.opentelemetry.semconv.incubating)
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package io.embrace.android.embracesdk.internal.arch.destination

import io.embrace.android.embracesdk.annotation.InternalApi
import io.embrace.android.embracesdk.internal.arch.schema.EmbType
import io.embrace.android.embracesdk.internal.arch.schema.SchemaType
import io.embrace.android.embracesdk.spans.EmbraceSpanEvent

/**
* Declares functions for writing an [EmbraceSpanEvent] or attributes to the current session span.
*/
@InternalApi
public interface SessionSpanWriter {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ private const val EMBRACE_ATTRIBUTE_NAME_PREFIX = "emb."
*/
private const val EMBRACE_PRIVATE_ATTRIBUTE_NAME_PREFIX = "emb.private."

/**
* Prefix added to all Embrace attribute keys that represent session properties that are set via the SDK
*/
private const val EMBRACE_SESSION_PROPERTY_NAME_PREFIX = "emb.properties."

/**
* Return the appropriate internal Embrace attribute name given the current string
*/
Expand All @@ -21,3 +26,5 @@ public fun String.toEmbraceAttributeName(isPrivate: Boolean = false): String {
}
return prefix + this
}

public fun String.toSessionPropertyAttributeName(): String = EMBRACE_SESSION_PROPERTY_NAME_PREFIX + this
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
package io.embrace.android.embracesdk.internal.arch.schema

import io.embrace.android.embracesdk.annotation.InternalApi
import io.embrace.android.embracesdk.internal.clock.millisToNanos
import io.embrace.android.embracesdk.internal.payload.AppExitInfoData
import io.embrace.android.embracesdk.internal.payload.NetworkCapturedCall
import io.embrace.android.embracesdk.internal.spans.toSessionPropertyAttributeName
import io.embrace.android.embracesdk.internal.utils.NetworkUtils.getValidTraceId
import io.embrace.android.embracesdk.internal.utils.NetworkUtils.stripUrl
import io.embrace.android.embracesdk.internal.utils.toNonNullMap
import io.embrace.android.embracesdk.network.EmbraceNetworkRequest
import io.opentelemetry.semconv.ErrorAttributes
import io.opentelemetry.semconv.HttpAttributes
import io.opentelemetry.semconv.incubating.ExceptionIncubatingAttributes
import io.opentelemetry.semconv.incubating.HttpIncubatingAttributes

/**
* The collections of attribute schemas used by the associated telemetry types.
Expand All @@ -21,7 +13,6 @@ import io.opentelemetry.semconv.incubating.HttpIncubatingAttributes
* telemetry data object if the same, fixed name is used for every instance.
*/

@InternalApi
public sealed class SchemaType(
public val telemetryType: TelemetryType,
public val fixedObjectName: String = "",
Expand Down Expand Up @@ -201,7 +192,7 @@ public sealed class SchemaType(
override val schemaAttributes: Map<String, String> = emptyMap<String, String>()
}

internal class AeiLog(message: AppExitInfoData) : SchemaType(EmbType.System.Exit) {
public class AeiLog(message: AppExitInfoData) : SchemaType(EmbType.System.Exit) {
override val schemaAttributes: Map<String, String> = mapOf(
"aei_session_id" to message.sessionId,
"session_id_error" to message.sessionIdError,
Expand All @@ -216,18 +207,10 @@ public sealed class SchemaType(
).toNonNullMap()
}

public class NetworkRequest(networkRequest: EmbraceNetworkRequest) : SchemaType(EmbType.Performance.Network) {
override val schemaAttributes: Map<String, String> = mapOf(
"url.full" to stripUrl(networkRequest.url),
HttpAttributes.HTTP_REQUEST_METHOD.key to networkRequest.httpMethod,
HttpAttributes.HTTP_RESPONSE_STATUS_CODE.key to networkRequest.responseCode,
HttpIncubatingAttributes.HTTP_REQUEST_BODY_SIZE.key to networkRequest.bytesSent,
HttpIncubatingAttributes.HTTP_RESPONSE_BODY_SIZE.key to networkRequest.bytesReceived,
ErrorAttributes.ERROR_TYPE.key to networkRequest.errorType,
"error.message" to networkRequest.errorMessage,
"emb.w3c_traceparent" to networkRequest.w3cTraceparent,
"emb.trace_id" to getValidTraceId(networkRequest.traceId),
).toNonNullMap().mapValues { it.value.toString() }
public class NetworkRequest(networkRequestAttrs: Map<String, String>) : SchemaType(EmbType.Performance.Network) {
// EmbraceNetworkRequest needs to stay in embrace-android-sdk module as it's a public API,
// so pass a map of attributes directly.
override val schemaAttributes: Map<String, String> = networkRequestAttrs
}

public class Log(attributes: TelemetryAttributes) : SchemaType(EmbType.System.Log) {
Expand Down Expand Up @@ -270,7 +253,7 @@ public sealed class SchemaType(
override val schemaAttributes: Map<String, String> = emptyMap<String, String>()
}

internal class NetworkCapturedRequest(networkCapturedCall: NetworkCapturedCall) : SchemaType(
public class NetworkCapturedRequest(networkCapturedCall: NetworkCapturedCall) : SchemaType(
telemetryType = EmbType.System.NetworkCapturedRequest
) {
override val schemaAttributes: Map<String, String> = mapOf(
Expand All @@ -297,7 +280,7 @@ public sealed class SchemaType(
).toNonNullMap()
}

internal class NetworkStatus(
public class NetworkStatus(
networkStatus: io.embrace.android.embracesdk.internal.comms.delivery.NetworkStatus
) : SchemaType(
telemetryType = EmbType.System.NetworkStatus,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package io.embrace.android.embracesdk.internal.arch.schema

public object SendImmediately : FixedAttribute {
override val key: EmbraceAttributeKey = EmbraceAttributeKey(id = "send_immediately")
override val value: String = "true"
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package io.embrace.android.embracesdk.internal.arch.schema

import io.embrace.android.embracesdk.annotation.InternalApi
import io.embrace.android.embracesdk.internal.config.ConfigService
import io.embrace.android.embracesdk.internal.spans.toSessionPropertyAttributeName
import io.opentelemetry.api.common.AttributeKey

/**
* Object that aggregates various attributes and returns a [Map] that represents the values at the current state
*/
@InternalApi
public class TelemetryAttributes(
private val configService: ConfigService,
private val sessionPropertiesProvider: () -> Map<String, String>? = { null },
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package io.embrace.android.embracesdk.internal.clock

import io.embrace.android.embracesdk.annotation.InternalApi
import java.util.concurrent.TimeUnit

@InternalApi
public fun interface Clock {

/**
Expand All @@ -20,18 +18,18 @@ public fun interface Clock {
/**
* Turns a number that specifies a millisecond value to nanoseconds
*/
internal fun Long.millisToNanos(): Long = TimeUnit.MILLISECONDS.toNanos(this)
public fun Long.millisToNanos(): Long = TimeUnit.MILLISECONDS.toNanos(this)

/**
* Turns a number that specifies a nanosecond value to milliseconds
*/
internal fun Long.nanosToMillis(): Long = TimeUnit.NANOSECONDS.toMillis(this)
public fun Long.nanosToMillis(): Long = TimeUnit.NANOSECONDS.toMillis(this)

/**
* Any epoch timestamp that we detect to be unreasonable to be interpreted as milliseconds, we assume it's an unintended use of nanoseconds
* based on an old API version or assumption from OpenTelemetry conventions
*/
internal fun Long.normalizeTimestampAsMillis(): Long =
public fun Long.normalizeTimestampAsMillis(): Long =
if (this < MAX_MS_CUTOFF) {
this
} else {
Expand All @@ -41,4 +39,4 @@ internal fun Long.normalizeTimestampAsMillis(): Long =
/**
* Equivalent to the epoch time of January 1, 5000 12:00:00 AM GMT
*/
internal const val MAX_MS_CUTOFF = 95_617_584_000_000L
public const val MAX_MS_CUTOFF: Long = 95_617_584_000_000L
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.embrace.android.embracesdk.internal.comms.delivery

internal enum class NetworkStatus(val value: String) {
public enum class NetworkStatus(public val value: String) {
NOT_REACHABLE("none"),
WIFI("wifi"),
WAN("wan"),
Expand All @@ -10,6 +10,6 @@ internal enum class NetworkStatus(val value: String) {
* Returns true if the device is connected to a network.
* [UNKNOWN] is considered reachable so we attempt to send network calls.
*/
val isReachable: Boolean
public val isReachable: Boolean
get() = this != NOT_REACHABLE
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.embrace.android.embracesdk.internal.config

import io.embrace.android.embracesdk.annotation.InternalApi
import io.embrace.android.embracesdk.internal.config.behavior.AnrBehavior
import io.embrace.android.embracesdk.internal.config.behavior.AppExitInfoBehavior
import io.embrace.android.embracesdk.internal.config.behavior.AutoDataCaptureBehavior
Expand All @@ -15,7 +14,6 @@ import io.embrace.android.embracesdk.internal.config.behavior.SdkModeBehavior
import io.embrace.android.embracesdk.internal.config.behavior.SessionBehavior
import io.embrace.android.embracesdk.internal.config.behavior.StartupBehavior
import io.embrace.android.embracesdk.internal.config.behavior.WebViewVitalsBehavior
import io.embrace.android.embracesdk.internal.config.remote.RemoteConfig
import io.embrace.android.embracesdk.internal.payload.AppFramework
import java.io.Closeable

Expand All @@ -24,7 +22,6 @@ import java.io.Closeable
*
* Configuration is configured for the user's app, and exposed via the API.
*/
@InternalApi
public interface ConfigService : Closeable {

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

import io.embrace.android.embracesdk.annotation.InternalApi
import io.embrace.android.embracesdk.internal.config.remote.AllowedNdkSampleMethod
import io.embrace.android.embracesdk.internal.config.remote.Unwinder
import java.util.regex.Pattern

@InternalApi
public interface AnrBehavior {

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

import io.embrace.android.embracesdk.annotation.InternalApi
import io.embrace.android.embracesdk.internal.config.remote.NetworkCaptureRuleRemoteConfig

@InternalApi
public interface NetworkBehavior {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ package io.embrace.android.embracesdk.internal.utils
* performs the necessary casts to ensure Kotlin's type system is happy.
*/
@Suppress("UNCHECKED_CAST")
internal fun <K, V> Map<K, V?>.toNonNullMap(): Map<K, V> {
public fun <K, V> Map<K, V?>.toNonNullMap(): Map<K, V> {
return filter { it.value != null } as Map<K, V>
}
2 changes: 2 additions & 0 deletions embrace-android-okhttp3/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ android {

dependencies {
compileOnly(libs.okhttp)
compileOnly(project(":embrace-android-core"))
compileOnly(project(":embrace-android-sdk"))
testImplementation(project(":embrace-android-core"))
testImplementation(project(":embrace-android-sdk"))
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ package io.embrace.android.embracesdk.internal.config.remote

import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass
import io.embrace.android.embracesdk.annotation.InternalApi

@JsonClass(generateAdapter = true)
@InternalApi
public class AllowedNdkSampleMethod(
@Json(name = "c") public val clz: String? = null,
@Json(name = "m") public val method: String? = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package io.embrace.android.embracesdk.internal.config.remote

import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass
import io.embrace.android.embracesdk.annotation.InternalApi

private const val NETWORK_BODY_RULE_DEFAULT_MAX_COUNT = 5
private const val NETWORK_BODY_RULE_DEFAULT_MAX_SIZE_BYTES = 102400L
Expand All @@ -11,7 +10,6 @@ private const val NETWORK_BODY_RULE_DEFAULT_MAX_SIZE_BYTES = 102400L
* Criteria to determine if a network body call should be captured or not.
*/
@JsonClass(generateAdapter = true)
@InternalApi
public data class NetworkCaptureRuleRemoteConfig(

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package io.embrace.android.embracesdk.internal.config.remote

public enum class Unwinder(public val code: Int) {
LIBUNWIND(0),
LIBUNWINDSTACK(1)
}
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
package io.embrace.android.embracesdk.internal.payload

internal data class AppExitInfoData(
internal val sessionId: String?,
public data class AppExitInfoData(
public val sessionId: String?,

internal val sessionIdError: String?,
public val sessionIdError: String?,

// the importance of the process that it used to have before the death.
internal val importance: Int?,
public val importance: Int?,

// Last proportional set size of the memory that the process had used in Bytes.
internal val pss: Long?,
public val pss: Long?,

internal val reason: Int?,
public val reason: Int?,

// Last resident set size of the memory that the process had used in Bytes.
internal val rss: Long?,
public val rss: Long?,

// The exit status argument of exit() if the application calls it,
// or the signal number if the application is signaled.
internal val status: Int?,
public val status: Int?,

internal val timestamp: Long?,
public val timestamp: Long?,

// file with ANR/CRASH traces compressed as string
internal val trace: String?,
public val trace: String?,

internal val description: String?,
public val description: String?,

// Error or Exception if the traces couldn't be collected
internal val traceStatus: String?
public val traceStatus: String?
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package io.embrace.android.embracesdk.internal.payload

import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass

/**
* The frameworks in use by the app. Previous name: a.f
*
* Values: NATIVE,REACT_NATIVE,UNITY,FLUTTER
*/
@JsonClass(generateAdapter = false)
public enum class AppFramework(public val value: Int) {
@Json(name = "1")
NATIVE(1),

@Json(name = "2")
REACT_NATIVE(2),

@Json(name = "3")
UNITY(3),

@Json(name = "4")
FLUTTER(4);

public companion object {

public fun fromInt(type: Int): AppFramework? = values().associateBy(AppFramework::value)[type]

public fun fromString(type: String?): AppFramework? = when (type) {
"react_native" -> REACT_NATIVE
"unity" -> UNITY
"flutter" -> FLUTTER
"native" -> NATIVE
else -> null
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import com.squareup.moshi.JsonClass
import java.util.UUID

@JsonClass(generateAdapter = true)
internal data class NetworkCapturedCall(
public data class NetworkCapturedCall(
/**
* The duration of the network request in milliseconds.
*/
Expand Down
Loading

0 comments on commit b8d9570

Please sign in to comment.