Skip to content

Commit

Permalink
allow auto termination of spans
Browse files Browse the repository at this point in the history
  • Loading branch information
fractalwrench committed Dec 6, 2024
1 parent 370b045 commit a1b0918
Show file tree
Hide file tree
Showing 12 changed files with 221 additions and 53 deletions.
38 changes: 16 additions & 22 deletions embrace-android-api/api/embrace-android-api.api
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,11 @@ public abstract interface class io/embrace/android/embracesdk/internal/api/SdkAp

public final class io/embrace/android/embracesdk/internal/api/SdkApi$DefaultImpls {
public static fun createSpan (Lio/embrace/android/embracesdk/internal/api/SdkApi;Ljava/lang/String;Lio/embrace/android/embracesdk/spans/AutoTerminationMode;)Lio/embrace/android/embracesdk/spans/EmbraceSpan;
public static fun recordCompletedSpan (Lio/embrace/android/embracesdk/internal/api/SdkApi;Ljava/lang/String;JJLio/embrace/android/embracesdk/spans/AutoTerminationMode;)Z
public static fun recordCompletedSpan (Lio/embrace/android/embracesdk/internal/api/SdkApi;Ljava/lang/String;JJLio/embrace/android/embracesdk/spans/EmbraceSpan;Lio/embrace/android/embracesdk/spans/AutoTerminationMode;)Z
public static fun recordCompletedSpan (Lio/embrace/android/embracesdk/internal/api/SdkApi;Ljava/lang/String;JJLio/embrace/android/embracesdk/spans/ErrorCode;Lio/embrace/android/embracesdk/spans/AutoTerminationMode;)Z
public static fun recordCompletedSpan (Lio/embrace/android/embracesdk/internal/api/SdkApi;Ljava/lang/String;JJLio/embrace/android/embracesdk/spans/ErrorCode;Lio/embrace/android/embracesdk/spans/EmbraceSpan;Lio/embrace/android/embracesdk/spans/AutoTerminationMode;)Z
public static fun recordCompletedSpan (Lio/embrace/android/embracesdk/internal/api/SdkApi;Ljava/lang/String;JJLjava/util/Map;Ljava/util/List;Lio/embrace/android/embracesdk/spans/AutoTerminationMode;)Z
public static fun recordCompletedSpan (Lio/embrace/android/embracesdk/internal/api/SdkApi;Ljava/lang/String;JJ)Z
public static fun recordCompletedSpan (Lio/embrace/android/embracesdk/internal/api/SdkApi;Ljava/lang/String;JJLio/embrace/android/embracesdk/spans/EmbraceSpan;)Z
public static fun recordCompletedSpan (Lio/embrace/android/embracesdk/internal/api/SdkApi;Ljava/lang/String;JJLio/embrace/android/embracesdk/spans/ErrorCode;)Z
public static fun recordCompletedSpan (Lio/embrace/android/embracesdk/internal/api/SdkApi;Ljava/lang/String;JJLio/embrace/android/embracesdk/spans/ErrorCode;Lio/embrace/android/embracesdk/spans/EmbraceSpan;)Z
public static fun recordCompletedSpan (Lio/embrace/android/embracesdk/internal/api/SdkApi;Ljava/lang/String;JJLjava/util/Map;Ljava/util/List;)Z
public static fun recordSpan (Lio/embrace/android/embracesdk/internal/api/SdkApi;Ljava/lang/String;Lio/embrace/android/embracesdk/spans/AutoTerminationMode;Lkotlin/jvm/functions/Function0;)Ljava/lang/Object;
public static fun recordSpan (Lio/embrace/android/embracesdk/internal/api/SdkApi;Ljava/lang/String;Lio/embrace/android/embracesdk/spans/EmbraceSpan;Lio/embrace/android/embracesdk/spans/AutoTerminationMode;Lkotlin/jvm/functions/Function0;)Ljava/lang/Object;
public static fun recordSpan (Lio/embrace/android/embracesdk/internal/api/SdkApi;Ljava/lang/String;Ljava/util/Map;Ljava/util/List;Lio/embrace/android/embracesdk/spans/AutoTerminationMode;Lkotlin/jvm/functions/Function0;)Ljava/lang/Object;
Expand Down Expand Up @@ -289,12 +289,12 @@ public abstract interface class io/embrace/android/embracesdk/spans/TracingApi {
public abstract fun createSpan (Ljava/lang/String;Lio/embrace/android/embracesdk/spans/AutoTerminationMode;)Lio/embrace/android/embracesdk/spans/EmbraceSpan;
public abstract fun createSpan (Ljava/lang/String;Lio/embrace/android/embracesdk/spans/EmbraceSpan;Lio/embrace/android/embracesdk/spans/AutoTerminationMode;)Lio/embrace/android/embracesdk/spans/EmbraceSpan;
public abstract fun getSpan (Ljava/lang/String;)Lio/embrace/android/embracesdk/spans/EmbraceSpan;
public abstract fun recordCompletedSpan (Ljava/lang/String;JJLio/embrace/android/embracesdk/spans/AutoTerminationMode;)Z
public abstract fun recordCompletedSpan (Ljava/lang/String;JJLio/embrace/android/embracesdk/spans/EmbraceSpan;Lio/embrace/android/embracesdk/spans/AutoTerminationMode;)Z
public abstract fun recordCompletedSpan (Ljava/lang/String;JJLio/embrace/android/embracesdk/spans/ErrorCode;Lio/embrace/android/embracesdk/spans/AutoTerminationMode;)Z
public abstract fun recordCompletedSpan (Ljava/lang/String;JJLio/embrace/android/embracesdk/spans/ErrorCode;Lio/embrace/android/embracesdk/spans/EmbraceSpan;Lio/embrace/android/embracesdk/spans/AutoTerminationMode;)Z
public abstract fun recordCompletedSpan (Ljava/lang/String;JJLio/embrace/android/embracesdk/spans/ErrorCode;Lio/embrace/android/embracesdk/spans/EmbraceSpan;Ljava/util/Map;Ljava/util/List;Lio/embrace/android/embracesdk/spans/AutoTerminationMode;)Z
public abstract fun recordCompletedSpan (Ljava/lang/String;JJLjava/util/Map;Ljava/util/List;Lio/embrace/android/embracesdk/spans/AutoTerminationMode;)Z
public abstract fun recordCompletedSpan (Ljava/lang/String;JJ)Z
public abstract fun recordCompletedSpan (Ljava/lang/String;JJLio/embrace/android/embracesdk/spans/EmbraceSpan;)Z
public abstract fun recordCompletedSpan (Ljava/lang/String;JJLio/embrace/android/embracesdk/spans/ErrorCode;)Z
public abstract fun recordCompletedSpan (Ljava/lang/String;JJLio/embrace/android/embracesdk/spans/ErrorCode;Lio/embrace/android/embracesdk/spans/EmbraceSpan;)Z
public abstract fun recordCompletedSpan (Ljava/lang/String;JJLio/embrace/android/embracesdk/spans/ErrorCode;Lio/embrace/android/embracesdk/spans/EmbraceSpan;Ljava/util/Map;Ljava/util/List;)Z
public abstract fun recordCompletedSpan (Ljava/lang/String;JJLjava/util/Map;Ljava/util/List;)Z
public abstract fun recordSpan (Ljava/lang/String;Lio/embrace/android/embracesdk/spans/AutoTerminationMode;Lkotlin/jvm/functions/Function0;)Ljava/lang/Object;
public abstract fun recordSpan (Ljava/lang/String;Lio/embrace/android/embracesdk/spans/EmbraceSpan;Lio/embrace/android/embracesdk/spans/AutoTerminationMode;Lkotlin/jvm/functions/Function0;)Ljava/lang/Object;
public abstract fun recordSpan (Ljava/lang/String;Lio/embrace/android/embracesdk/spans/EmbraceSpan;Ljava/util/Map;Ljava/util/List;Lio/embrace/android/embracesdk/spans/AutoTerminationMode;Lkotlin/jvm/functions/Function0;)Ljava/lang/Object;
Expand All @@ -308,17 +308,11 @@ public final class io/embrace/android/embracesdk/spans/TracingApi$DefaultImpls {
public static fun createSpan (Lio/embrace/android/embracesdk/spans/TracingApi;Ljava/lang/String;Lio/embrace/android/embracesdk/spans/AutoTerminationMode;)Lio/embrace/android/embracesdk/spans/EmbraceSpan;
public static synthetic fun createSpan$default (Lio/embrace/android/embracesdk/spans/TracingApi;Ljava/lang/String;Lio/embrace/android/embracesdk/spans/AutoTerminationMode;ILjava/lang/Object;)Lio/embrace/android/embracesdk/spans/EmbraceSpan;
public static synthetic fun createSpan$default (Lio/embrace/android/embracesdk/spans/TracingApi;Ljava/lang/String;Lio/embrace/android/embracesdk/spans/EmbraceSpan;Lio/embrace/android/embracesdk/spans/AutoTerminationMode;ILjava/lang/Object;)Lio/embrace/android/embracesdk/spans/EmbraceSpan;
public static fun recordCompletedSpan (Lio/embrace/android/embracesdk/spans/TracingApi;Ljava/lang/String;JJLio/embrace/android/embracesdk/spans/AutoTerminationMode;)Z
public static fun recordCompletedSpan (Lio/embrace/android/embracesdk/spans/TracingApi;Ljava/lang/String;JJLio/embrace/android/embracesdk/spans/EmbraceSpan;Lio/embrace/android/embracesdk/spans/AutoTerminationMode;)Z
public static fun recordCompletedSpan (Lio/embrace/android/embracesdk/spans/TracingApi;Ljava/lang/String;JJLio/embrace/android/embracesdk/spans/ErrorCode;Lio/embrace/android/embracesdk/spans/AutoTerminationMode;)Z
public static fun recordCompletedSpan (Lio/embrace/android/embracesdk/spans/TracingApi;Ljava/lang/String;JJLio/embrace/android/embracesdk/spans/ErrorCode;Lio/embrace/android/embracesdk/spans/EmbraceSpan;Lio/embrace/android/embracesdk/spans/AutoTerminationMode;)Z
public static fun recordCompletedSpan (Lio/embrace/android/embracesdk/spans/TracingApi;Ljava/lang/String;JJLjava/util/Map;Ljava/util/List;Lio/embrace/android/embracesdk/spans/AutoTerminationMode;)Z
public static synthetic fun recordCompletedSpan$default (Lio/embrace/android/embracesdk/spans/TracingApi;Ljava/lang/String;JJLio/embrace/android/embracesdk/spans/AutoTerminationMode;ILjava/lang/Object;)Z
public static synthetic fun recordCompletedSpan$default (Lio/embrace/android/embracesdk/spans/TracingApi;Ljava/lang/String;JJLio/embrace/android/embracesdk/spans/EmbraceSpan;Lio/embrace/android/embracesdk/spans/AutoTerminationMode;ILjava/lang/Object;)Z
public static synthetic fun recordCompletedSpan$default (Lio/embrace/android/embracesdk/spans/TracingApi;Ljava/lang/String;JJLio/embrace/android/embracesdk/spans/ErrorCode;Lio/embrace/android/embracesdk/spans/AutoTerminationMode;ILjava/lang/Object;)Z
public static synthetic fun recordCompletedSpan$default (Lio/embrace/android/embracesdk/spans/TracingApi;Ljava/lang/String;JJLio/embrace/android/embracesdk/spans/ErrorCode;Lio/embrace/android/embracesdk/spans/EmbraceSpan;Lio/embrace/android/embracesdk/spans/AutoTerminationMode;ILjava/lang/Object;)Z
public static synthetic fun recordCompletedSpan$default (Lio/embrace/android/embracesdk/spans/TracingApi;Ljava/lang/String;JJLio/embrace/android/embracesdk/spans/ErrorCode;Lio/embrace/android/embracesdk/spans/EmbraceSpan;Ljava/util/Map;Ljava/util/List;Lio/embrace/android/embracesdk/spans/AutoTerminationMode;ILjava/lang/Object;)Z
public static synthetic fun recordCompletedSpan$default (Lio/embrace/android/embracesdk/spans/TracingApi;Ljava/lang/String;JJLjava/util/Map;Ljava/util/List;Lio/embrace/android/embracesdk/spans/AutoTerminationMode;ILjava/lang/Object;)Z
public static fun recordCompletedSpan (Lio/embrace/android/embracesdk/spans/TracingApi;Ljava/lang/String;JJ)Z
public static fun recordCompletedSpan (Lio/embrace/android/embracesdk/spans/TracingApi;Ljava/lang/String;JJLio/embrace/android/embracesdk/spans/EmbraceSpan;)Z
public static fun recordCompletedSpan (Lio/embrace/android/embracesdk/spans/TracingApi;Ljava/lang/String;JJLio/embrace/android/embracesdk/spans/ErrorCode;)Z
public static fun recordCompletedSpan (Lio/embrace/android/embracesdk/spans/TracingApi;Ljava/lang/String;JJLio/embrace/android/embracesdk/spans/ErrorCode;Lio/embrace/android/embracesdk/spans/EmbraceSpan;)Z
public static fun recordCompletedSpan (Lio/embrace/android/embracesdk/spans/TracingApi;Ljava/lang/String;JJLjava/util/Map;Ljava/util/List;)Z
public static fun recordSpan (Lio/embrace/android/embracesdk/spans/TracingApi;Ljava/lang/String;Lio/embrace/android/embracesdk/spans/AutoTerminationMode;Lkotlin/jvm/functions/Function0;)Ljava/lang/Object;
public static fun recordSpan (Lio/embrace/android/embracesdk/spans/TracingApi;Ljava/lang/String;Lio/embrace/android/embracesdk/spans/EmbraceSpan;Lio/embrace/android/embracesdk/spans/AutoTerminationMode;Lkotlin/jvm/functions/Function0;)Ljava/lang/Object;
public static fun recordSpan (Lio/embrace/android/embracesdk/spans/TracingApi;Ljava/lang/String;Ljava/util/Map;Ljava/util/List;Lio/embrace/android/embracesdk/spans/AutoTerminationMode;Lkotlin/jvm/functions/Function0;)Ljava/lang/Object;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public interface TracingApi {
public fun createSpan(
name: String,
autoTerminationMode: AutoTerminationMode = AutoTerminationMode.NONE,
): EmbraceSpan? = createSpan(name = name, parent = null)
): EmbraceSpan? = createSpan(name = name, parent = null, autoTerminationMode = autoTerminationMode)

/**
* Create an [EmbraceSpan] with the given name and parent. Passing in a parent that is null result in a new trace with this
Expand All @@ -37,7 +37,7 @@ public interface TracingApi {
public fun startSpan(
name: String,
autoTerminationMode: AutoTerminationMode = AutoTerminationMode.NONE,
): EmbraceSpan? = startSpan(name = name, parent = null)
): EmbraceSpan? = startSpan(name = name, parent = null, autoTerminationMode = autoTerminationMode)

/**
* Create, start, and return a new [EmbraceSpan] with the given name and parent. Returns null if the [EmbraceSpan] cannot be created
Expand All @@ -50,7 +50,8 @@ public interface TracingApi {
): EmbraceSpan? = startSpan(
name = name,
parent = parent,
startTimeMs = null
startTimeMs = null,
autoTerminationMode = autoTerminationMode,
)

/**
Expand All @@ -73,7 +74,14 @@ public interface TracingApi {
name: String,
autoTerminationMode: AutoTerminationMode = AutoTerminationMode.NONE,
code: () -> T,
): T = recordSpan(name = name, parent = null, attributes = null, events = null, code = code)
): T = recordSpan(
name = name,
parent = null,
attributes = null,
events = null,
code = code,
autoTerminationMode = autoTerminationMode,
)

/**
* Execute the given block of code and record a new span around it with the given parent. Passing in a parent that is null will result
Expand All @@ -86,7 +94,14 @@ public interface TracingApi {
parent: EmbraceSpan?,
autoTerminationMode: AutoTerminationMode = AutoTerminationMode.NONE,
code: () -> T,
): T = recordSpan(name = name, parent = parent, attributes = null, events = null, code = code)
): T = recordSpan(
name = name,
parent = parent,
attributes = null,
events = null,
code = code,
autoTerminationMode = autoTerminationMode,
)

/**
* Execute the given block of code and record a new trace around it with optional attributes and list of [EmbraceSpanEvent]. If the span
Expand All @@ -99,7 +114,14 @@ public interface TracingApi {
events: List<EmbraceSpanEvent>?,
autoTerminationMode: AutoTerminationMode = AutoTerminationMode.NONE,
code: () -> T,
): T = recordSpan(name = name, parent = null, attributes = attributes, events = events, code = code)
): T = recordSpan(
name = name,
parent = null,
attributes = attributes,
events = events,
code = code,
autoTerminationMode = autoTerminationMode,

Check warning on line 123 in embrace-android-api/src/main/kotlin/io/embrace/android/embracesdk/spans/TracingApi.kt

View check run for this annotation

Codecov / codecov/patch

embrace-android-api/src/main/kotlin/io/embrace/android/embracesdk/spans/TracingApi.kt#L117-L123

Added lines #L117 - L123 were not covered by tests
)

/**
* Execute the given block of code and record a new span around it with the given parent with optional attributes and list
Expand All @@ -125,7 +147,6 @@ public interface TracingApi {
name: String,
startTimeMs: Long,
endTimeMs: Long,
autoTerminationMode: AutoTerminationMode = AutoTerminationMode.NONE,
): Boolean =
recordCompletedSpan(
name = name,
Expand All @@ -147,7 +168,6 @@ public interface TracingApi {
startTimeMs: Long,
endTimeMs: Long,
errorCode: ErrorCode?,
autoTerminationMode: AutoTerminationMode = AutoTerminationMode.NONE,
): Boolean =
recordCompletedSpan(
name = name,
Expand All @@ -168,7 +188,6 @@ public interface TracingApi {
startTimeMs: Long,
endTimeMs: Long,
parent: EmbraceSpan?,
autoTerminationMode: AutoTerminationMode = AutoTerminationMode.NONE,
): Boolean =
recordCompletedSpan(
name = name,
Expand All @@ -191,7 +210,6 @@ public interface TracingApi {
endTimeMs: Long,
errorCode: ErrorCode?,
parent: EmbraceSpan?,
autoTerminationMode: AutoTerminationMode = AutoTerminationMode.NONE,
): Boolean = recordCompletedSpan(
name = name,
startTimeMs = startTimeMs,
Expand All @@ -213,7 +231,6 @@ public interface TracingApi {
endTimeMs: Long,
attributes: Map<String, String>?,
events: List<EmbraceSpanEvent>?,
autoTerminationMode: AutoTerminationMode = AutoTerminationMode.NONE,
): Boolean = recordCompletedSpan(
name = name,
startTimeMs = startTimeMs,
Expand All @@ -239,7 +256,6 @@ public interface TracingApi {
parent: EmbraceSpan?,
attributes: Map<String, String>?,
events: List<EmbraceSpanEvent>?,
autoTerminationMode: AutoTerminationMode = AutoTerminationMode.NONE,
): Boolean

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package io.embrace.android.embracesdk.internal.envelope.session

import io.embrace.android.embracesdk.internal.arch.schema.AppTerminationCause
import io.embrace.android.embracesdk.internal.arch.schema.EmbType
import io.embrace.android.embracesdk.internal.clock.Clock
import io.embrace.android.embracesdk.internal.logging.EmbLogger
import io.embrace.android.embracesdk.internal.payload.SessionPayload
import io.embrace.android.embracesdk.internal.payload.Span
import io.embrace.android.embracesdk.internal.payload.toNewPayload
import io.embrace.android.embracesdk.internal.session.captureDataSafely
import io.embrace.android.embracesdk.internal.session.lifecycle.ProcessStateService
import io.embrace.android.embracesdk.internal.session.orchestrator.SessionSnapshotType
import io.embrace.android.embracesdk.internal.spans.CurrentSessionSpan
import io.embrace.android.embracesdk.internal.spans.EmbraceSpanData
Expand All @@ -19,6 +21,8 @@ internal class SessionPayloadSourceImpl(
private val currentSessionSpan: CurrentSessionSpan,
private val spanRepository: SpanRepository,
private val otelPayloadMapper: OtelPayloadMapper,
private val processStateService: ProcessStateService,
private val clock: Clock,
private val logger: EmbLogger,
) : SessionPayloadSource {

Expand All @@ -31,6 +35,10 @@ internal class SessionPayloadSourceImpl(
val isCacheAttempt = endType == SessionSnapshotType.PERIODIC_CACHE
val includeSnapshots = endType != SessionSnapshotType.JVM_CRASH

if (!endType.forceQuit && processStateService.isInBackground) {
spanRepository.autoTerminateSpans(clock.now())
}

// Snapshots should only be included if the process is expected to last beyond the current session
val snapshots: List<Span>? = if (includeSnapshots) {
retrieveSpanSnapshots(isCacheAttempt)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ internal class PayloadSourceModuleImpl(
otelModule.currentSessionSpan,
otelModule.spanRepository,
otelPayloadMapperProvider(),
essentialServiceModule.processStateService,
initModule.clock,
initModule.logger
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ class EmbraceTracer(
parent: EmbraceSpan?,
attributes: Map<String, String>?,
events: List<EmbraceSpanEvent>?,
autoTerminationMode: AutoTerminationMode,
): Boolean = spanService.recordCompletedSpan(
name = name,
startTimeMs = startTimeMs.normalizeTimestampAsMillis(),
Expand All @@ -77,7 +76,6 @@ class EmbraceTracer(
private = false,
attributes = attributes ?: emptyMap(),
events = events ?: emptyList(),
autoTerminationMode = autoTerminationMode,
errorCode = errorCode
)

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.EmbType
import io.embrace.android.embracesdk.internal.utils.lockAndRun
import io.embrace.android.embracesdk.spans.AutoTerminationMode
import io.embrace.android.embracesdk.spans.EmbraceSpan
import io.embrace.android.embracesdk.spans.ErrorCode
import java.util.concurrent.ConcurrentHashMap
Expand Down Expand Up @@ -100,5 +101,19 @@ class SpanRepository {
spanUpdateNotifier?.invoke()
}

/**
* Automatically terminates root spans
*/
fun autoTerminateSpans(now: Long) { // TODO: move out of collection
// TODO: pass consistent endTimeMs
activeSpans.forEach { entry ->
val span = entry.value
if (span.parent == null && entry.value.autoTerminationMode == AutoTerminationMode.ON_BACKGROUND) {
span.stop(endTimeMs = now)
// TODO: obtain the span children
}
}
}

private fun notTracked(spanId: String): Boolean = activeSpans[spanId] == null && completedSpans[spanId] == null
}
Loading

0 comments on commit a1b0918

Please sign in to comment.