Skip to content

Commit

Permalink
Merge pull request #1736 from embrace-io/add-auto-term-enum
Browse files Browse the repository at this point in the history
Add auto termination enum
  • Loading branch information
fractalwrench authored Dec 6, 2024
2 parents 4631a27 + d6b5529 commit 2f8ad69
Show file tree
Hide file tree
Showing 31 changed files with 480 additions and 193 deletions.
97 changes: 60 additions & 37 deletions embrace-android-api/api/embrace-android-api.api

Large diffs are not rendered by default.

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

/**
* Defines how the span should terminate automatically
*/
public enum class AutoTerminationMode {

/**
* The span will not terminate automatically
*/
NONE,

/**
* The span will terminate when the app goes to the background
*/
ON_BACKGROUND,
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ public interface EmbraceSpan {
*/
public val parent: EmbraceSpan?

/**
* The auto termination mode for this span
*/
public val autoTerminationMode: AutoTerminationMode

/**
* Start recording of the Span. Returns true if this call triggered the start of the recording. Returns false if the Span has already
* been started or has been stopped.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package io.embrace.android.embracesdk.spans
* The actual trace won't be recorded until the SDK is started, but it's safe to use this prior to SDK initialization.
*/
public interface TracingApi {

/**
* Create an [EmbraceSpan] with the given name that will be the root span of a new trace. Returns null if the [EmbraceSpan] cannot
* be created given the current conditions of the SDK or an invalid name.
Expand All @@ -13,6 +14,7 @@ public interface TracingApi {
*/
public fun createSpan(
name: String,
autoTerminationMode: AutoTerminationMode = AutoTerminationMode.NONE,
): EmbraceSpan? = createSpan(name = name, parent = null)

/**
Expand All @@ -25,6 +27,7 @@ public interface TracingApi {
public fun createSpan(
name: String,
parent: EmbraceSpan?,
autoTerminationMode: AutoTerminationMode = AutoTerminationMode.NONE,
): EmbraceSpan?

/**
Expand All @@ -33,6 +36,7 @@ public interface TracingApi {
*/
public fun startSpan(
name: String,
autoTerminationMode: AutoTerminationMode = AutoTerminationMode.NONE,
): EmbraceSpan? = startSpan(name = name, parent = null)

/**
Expand All @@ -42,6 +46,7 @@ public interface TracingApi {
public fun startSpan(
name: String,
parent: EmbraceSpan?,
autoTerminationMode: AutoTerminationMode = AutoTerminationMode.NONE,
): EmbraceSpan? = startSpan(
name = name,
parent = parent,
Expand All @@ -56,6 +61,7 @@ public interface TracingApi {
name: String,
parent: EmbraceSpan?,
startTimeMs: Long?,
autoTerminationMode: AutoTerminationMode = AutoTerminationMode.NONE,
): EmbraceSpan?

/**
Expand All @@ -65,6 +71,7 @@ public interface TracingApi {
*/
public fun <T> recordSpan(
name: String,
autoTerminationMode: AutoTerminationMode = AutoTerminationMode.NONE,
code: () -> T,
): T = recordSpan(name = name, parent = null, attributes = null, events = null, code = code)

Expand All @@ -77,6 +84,7 @@ public interface TracingApi {
public fun <T> recordSpan(
name: String,
parent: EmbraceSpan?,
autoTerminationMode: AutoTerminationMode = AutoTerminationMode.NONE,
code: () -> T,
): T = recordSpan(name = name, parent = parent, attributes = null, events = null, code = code)

Expand All @@ -89,6 +97,7 @@ public interface TracingApi {
name: String,
attributes: Map<String, String>?,
events: List<EmbraceSpanEvent>?,
autoTerminationMode: AutoTerminationMode = AutoTerminationMode.NONE,
code: () -> T,
): T = recordSpan(name = name, parent = null, attributes = attributes, events = events, code = code)

Expand All @@ -104,6 +113,7 @@ public interface TracingApi {
parent: EmbraceSpan?,
attributes: Map<String, String>?,
events: List<EmbraceSpanEvent>?,
autoTerminationMode: AutoTerminationMode = AutoTerminationMode.NONE,
code: () -> T,
): T

Expand All @@ -115,6 +125,7 @@ public interface TracingApi {
name: String,
startTimeMs: Long,
endTimeMs: Long,
autoTerminationMode: AutoTerminationMode = AutoTerminationMode.NONE,
): Boolean =
recordCompletedSpan(
name = name,
Expand All @@ -136,6 +147,7 @@ public interface TracingApi {
startTimeMs: Long,
endTimeMs: Long,
errorCode: ErrorCode?,
autoTerminationMode: AutoTerminationMode = AutoTerminationMode.NONE,
): Boolean =
recordCompletedSpan(
name = name,
Expand All @@ -156,6 +168,7 @@ public interface TracingApi {
startTimeMs: Long,
endTimeMs: Long,
parent: EmbraceSpan?,
autoTerminationMode: AutoTerminationMode = AutoTerminationMode.NONE,
): Boolean =
recordCompletedSpan(
name = name,
Expand All @@ -178,6 +191,7 @@ public interface TracingApi {
endTimeMs: Long,
errorCode: ErrorCode?,
parent: EmbraceSpan?,
autoTerminationMode: AutoTerminationMode = AutoTerminationMode.NONE,
): Boolean = recordCompletedSpan(
name = name,
startTimeMs = startTimeMs,
Expand All @@ -199,6 +213,7 @@ public interface TracingApi {
endTimeMs: Long,
attributes: Map<String, String>?,
events: List<EmbraceSpanEvent>?,
autoTerminationMode: AutoTerminationMode = AutoTerminationMode.NONE,
): Boolean = recordCompletedSpan(
name = name,
startTimeMs = startTimeMs,
Expand All @@ -224,6 +239,7 @@ public interface TracingApi {
parent: EmbraceSpan?,
attributes: Map<String, String>?,
events: List<EmbraceSpanEvent>?,
autoTerminationMode: AutoTerminationMode = AutoTerminationMode.NONE,
): Boolean

/**
Expand Down
1 change: 1 addition & 0 deletions embrace-android-core/config/detekt/baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
<CurrentIssues>
<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;?, deliveryTracer: DeliveryTracer?, )</ID>
<ID>LongParameterList:NativeCoreModuleSupplier.kt$( initModule: InitModule, coreModule: CoreModule, payloadSourceModule: PayloadSourceModule, workerThreadModule: WorkerThreadModule, configModule: ConfigModule, storageModule: StorageModule, essentialServiceModule: EssentialServiceModule, otelModule: OpenTelemetryModule, delegateProvider: Provider&lt;JniDelegate?&gt;, sharedObjectLoaderProvider: Provider&lt;SharedObjectLoader?&gt;, symbolServiceProvider: Provider&lt;SymbolService?&gt;, )</ID>
<ID>LongParameterList:SpanService.kt$SpanService$( name: String, startTimeMs: Long, endTimeMs: Long, autoTerminationMode: AutoTerminationMode = AutoTerminationMode.NONE, parent: EmbraceSpan? = null, type: TelemetryType = EmbType.Performance.Default, internal: Boolean = true, private: Boolean = false, attributes: Map&lt;String, String&gt; = emptyMap(), events: List&lt;EmbraceSpanEvent&gt; = emptyList(), errorCode: ErrorCode? = null, )</ID>
</CurrentIssues>
</SmellBaseline>
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package io.embrace.android.embracesdk.internal.arch.datasource
import io.embrace.android.embracesdk.internal.arch.schema.SchemaType
import io.embrace.android.embracesdk.internal.spans.PersistableEmbraceSpan
import io.embrace.android.embracesdk.internal.spans.SpanService
import io.embrace.android.embracesdk.spans.AutoTerminationMode
import io.embrace.android.embracesdk.spans.EmbraceSpan

/**
Expand Down Expand Up @@ -34,11 +35,16 @@ interface SpanDataSource : DataSource<SpanService> {
): Boolean
}

fun SpanService.startSpanCapture(schemaType: SchemaType, startTimeMs: Long): PersistableEmbraceSpan? {
fun SpanService.startSpanCapture(
schemaType: SchemaType,
startTimeMs: Long,
autoTerminationMode: AutoTerminationMode = AutoTerminationMode.NONE,
): PersistableEmbraceSpan? {
return startSpan(
name = schemaType.fixedObjectName,
startTimeMs = startTimeMs,
type = schemaType.telemetryType
type = schemaType.telemetryType,
autoTerminationMode = autoTerminationMode,
)?.apply {
schemaType.attributes().forEach {
addAttribute(it.key, it.value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package io.embrace.android.embracesdk.internal.opentelemetry

import io.embrace.android.embracesdk.internal.arch.schema.TelemetryType
import io.embrace.android.embracesdk.internal.spans.EmbraceSpanBuilder
import io.embrace.android.embracesdk.spans.AutoTerminationMode
import io.embrace.android.embracesdk.spans.EmbraceSpan
import io.opentelemetry.api.trace.SpanBuilder
import io.opentelemetry.api.trace.Tracer
Expand All @@ -14,12 +15,14 @@ internal fun Tracer.embraceSpanBuilder(
type: TelemetryType,
internal: Boolean,
private: Boolean,
autoTerminationMode: AutoTerminationMode = AutoTerminationMode.NONE,
parent: EmbraceSpan? = null,
): EmbraceSpanBuilder = EmbraceSpanBuilder(
tracer = this,
name = name,
telemetryType = type,
internal = internal,
private = private,
autoTerminationMode = autoTerminationMode,
parentSpan = parent,
)
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ internal class CurrentSessionSpanImpl(
name = "session",
type = EmbType.Ux.Session,
internal = true,
private = false
private = false,
).apply {
start(startTimeMs = startTimeMs)
setSystemAttribute(SessionIncubatingAttributes.SESSION_ID, Uuid.getEmbUuid())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package io.embrace.android.embracesdk.internal.spans
import io.embrace.android.embracesdk.internal.arch.schema.FixedAttribute
import io.embrace.android.embracesdk.internal.arch.schema.PrivateSpan
import io.embrace.android.embracesdk.internal.arch.schema.TelemetryType
import io.embrace.android.embracesdk.spans.AutoTerminationMode
import io.embrace.android.embracesdk.spans.EmbraceSpan
import io.opentelemetry.api.trace.Span
import io.opentelemetry.api.trace.SpanBuilder
Expand All @@ -20,6 +21,7 @@ class EmbraceSpanBuilder(
telemetryType: TelemetryType,
val internal: Boolean,
private: Boolean,
val autoTerminationMode: AutoTerminationMode = AutoTerminationMode.NONE,
parentSpan: EmbraceSpan?,
) {
lateinit var parentContext: Context
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package io.embrace.android.embracesdk.internal.spans

import io.embrace.android.embracesdk.internal.arch.schema.TelemetryType
import io.embrace.android.embracesdk.internal.config.behavior.SensitiveKeysBehavior
import io.embrace.android.embracesdk.spans.AutoTerminationMode
import io.embrace.android.embracesdk.spans.EmbraceSpan

/**
Expand All @@ -14,6 +15,7 @@ internal interface EmbraceSpanFactory {
type: TelemetryType,
internal: Boolean,
private: Boolean,
autoTerminationMode: AutoTerminationMode = AutoTerminationMode.NONE,
parent: EmbraceSpan? = null,
): PersistableEmbraceSpan

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package io.embrace.android.embracesdk.internal.spans
import io.embrace.android.embracesdk.internal.arch.schema.TelemetryType
import io.embrace.android.embracesdk.internal.config.behavior.SensitiveKeysBehavior
import io.embrace.android.embracesdk.internal.opentelemetry.embraceSpanBuilder
import io.embrace.android.embracesdk.spans.AutoTerminationMode
import io.embrace.android.embracesdk.spans.EmbraceSpan
import io.opentelemetry.api.trace.Tracer
import io.opentelemetry.sdk.common.Clock
Expand All @@ -19,6 +20,7 @@ internal class EmbraceSpanFactoryImpl(
type: TelemetryType,
internal: Boolean,
private: Boolean,
autoTerminationMode: AutoTerminationMode,
parent: EmbraceSpan?,
): PersistableEmbraceSpan = create(
embraceSpanBuilder = tracer.embraceSpanBuilder(
Expand All @@ -27,6 +29,7 @@ internal class EmbraceSpanFactoryImpl(
internal = internal,
private = private,
parent = parent,
autoTerminationMode = autoTerminationMode
)
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import io.embrace.android.embracesdk.internal.payload.Attribute
import io.embrace.android.embracesdk.internal.payload.Span
import io.embrace.android.embracesdk.internal.payload.toNewPayload
import io.embrace.android.embracesdk.internal.utils.truncatedStacktraceText
import io.embrace.android.embracesdk.spans.AutoTerminationMode
import io.embrace.android.embracesdk.spans.EmbraceSpan
import io.embrace.android.embracesdk.spans.EmbraceSpanEvent
import io.embrace.android.embracesdk.spans.ErrorCode
Expand Down Expand Up @@ -76,6 +77,8 @@ internal class EmbraceSpanImpl(
override val isRecording: Boolean
get() = startedSpan.get()?.isRecording == true

override val autoTerminationMode: AutoTerminationMode = spanBuilder.autoTerminationMode

override fun start(startTimeMs: Long?): Boolean {
if (spanStarted()) {
return false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package io.embrace.android.embracesdk.internal.spans

import io.embrace.android.embracesdk.internal.arch.schema.TelemetryType
import io.embrace.android.embracesdk.internal.utils.Provider
import io.embrace.android.embracesdk.spans.AutoTerminationMode
import io.embrace.android.embracesdk.spans.EmbraceSpan
import io.embrace.android.embracesdk.spans.EmbraceSpanEvent
import io.embrace.android.embracesdk.spans.ErrorCode
Expand Down Expand Up @@ -44,12 +45,20 @@ internal class EmbraceSpanService(

override fun createSpan(
name: String,
autoTerminationMode: AutoTerminationMode,
parent: EmbraceSpan?,
type: TelemetryType,
internal: Boolean,
private: Boolean,
): PersistableEmbraceSpan? =
currentDelegate.createSpan(name = name, parent = parent, type = type, internal = internal, private = private)
currentDelegate.createSpan(
name = name,
autoTerminationMode = autoTerminationMode,
parent = parent,
type = type,
internal = internal,
private = private
)

override fun createSpan(embraceSpanBuilder: EmbraceSpanBuilder): PersistableEmbraceSpan? =
currentDelegate.createSpan(
Expand All @@ -58,6 +67,7 @@ internal class EmbraceSpanService(

override fun <T> recordSpan(
name: String,
autoTerminationMode: AutoTerminationMode,
parent: EmbraceSpan?,
type: TelemetryType,
internal: Boolean,
Expand All @@ -67,6 +77,7 @@ internal class EmbraceSpanService(
code: () -> T,
): T = currentDelegate.recordSpan(
name = name,
autoTerminationMode = autoTerminationMode,
parent = parent,
type = type,
internal = internal,
Expand All @@ -80,6 +91,7 @@ internal class EmbraceSpanService(
name: String,
startTimeMs: Long,
endTimeMs: Long,
autoTerminationMode: AutoTerminationMode,
parent: EmbraceSpan?,
type: TelemetryType,
internal: Boolean,
Expand All @@ -91,6 +103,7 @@ internal class EmbraceSpanService(
name = name,
startTimeMs = startTimeMs,
endTimeMs = endTimeMs,
autoTerminationMode = autoTerminationMode,
parent = parent,
type = type,
internal = internal,
Expand Down
Loading

0 comments on commit 2f8ad69

Please sign in to comment.