Skip to content

Commit

Permalink
Merge pull request #1737 from embrace-io/propagate-auto-term-enum
Browse files Browse the repository at this point in the history
Propagate auto termination enum to span service
  • Loading branch information
fractalwrench authored Dec 3, 2024
2 parents 7bd8034 + 3ecf4cf commit 370b045
Show file tree
Hide file tree
Showing 26 changed files with 303 additions and 79 deletions.
1 change: 1 addition & 0 deletions embrace-android-api/api/embrace-android-api.api
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ public abstract interface class io/embrace/android/embracesdk/spans/EmbraceSpan
public abstract fun addEvent (Ljava/lang/String;)Z
public abstract fun addEvent (Ljava/lang/String;Ljava/lang/Long;)Z
public abstract fun addEvent (Ljava/lang/String;Ljava/lang/Long;Ljava/util/Map;)Z
public abstract fun getAutoTerminationMode ()Lio/embrace/android/embracesdk/spans/AutoTerminationMode;
public abstract fun getParent ()Lio/embrace/android/embracesdk/spans/EmbraceSpan;
public abstract fun getSpanContext ()Lio/opentelemetry/api/trace/SpanContext;
public abstract fun getSpanId ()Ljava/lang/String;
Expand Down
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
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 @@ -19,6 +19,7 @@ import io.embrace.android.embracesdk.internal.spans.EmbraceSpanLimits.MAX_TOTAL_
import io.embrace.android.embracesdk.internal.spans.EmbraceSpanLimits.isAttributeValid
import io.embrace.android.embracesdk.internal.spans.EmbraceSpanLimits.isNameValid
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 @@ -77,6 +78,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
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class EmbraceTracer(
parent = parent,
internal = false,
private = false,
autoTerminationMode = autoTerminationMode
)

override fun startSpan(
Expand All @@ -37,6 +38,7 @@ class EmbraceTracer(
startTimeMs = startTimeMs?.normalizeTimestampAsMillis(),
internal = false,
private = false,
autoTerminationMode = autoTerminationMode
)

override fun <T> recordSpan(
Expand All @@ -53,6 +55,7 @@ class EmbraceTracer(
private = false,
attributes = attributes ?: emptyMap(),
events = events ?: emptyList(),
autoTerminationMode = autoTerminationMode,
code = code
)

Expand All @@ -74,6 +77,7 @@ 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 @@ -3,6 +3,7 @@ package io.embrace.android.embracesdk.internal.spans
import io.embrace.android.embracesdk.internal.Initializable
import io.embrace.android.embracesdk.internal.arch.schema.EmbType
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.embrace.android.embracesdk.spans.EmbraceSpanEvent
import io.embrace.android.embracesdk.spans.ErrorCode
Expand All @@ -18,6 +19,7 @@ interface SpanService : Initializable {
*/
fun createSpan(
name: String,
autoTerminationMode: AutoTerminationMode = AutoTerminationMode.NONE,
parent: EmbraceSpan? = null,
type: TelemetryType = EmbType.Performance.Default,
internal: Boolean = true,
Expand All @@ -35,6 +37,7 @@ interface SpanService : Initializable {
*/
fun startSpan(
name: String,
autoTerminationMode: AutoTerminationMode = AutoTerminationMode.NONE,
parent: EmbraceSpan? = null,
startTimeMs: Long? = null,
type: TelemetryType = EmbType.Performance.Default,
Expand All @@ -43,6 +46,7 @@ interface SpanService : Initializable {
): PersistableEmbraceSpan? {
createSpan(
name = name,
autoTerminationMode = autoTerminationMode,
parent = parent,
type = type,
internal = internal,
Expand All @@ -62,6 +66,7 @@ interface SpanService : Initializable {
*/
fun <T> recordSpan(
name: String,
autoTerminationMode: AutoTerminationMode = AutoTerminationMode.NONE,
parent: EmbraceSpan? = null,
type: TelemetryType = EmbType.Performance.Default,
internal: Boolean = true,
Expand All @@ -79,6 +84,7 @@ interface SpanService : Initializable {
name: String,
startTimeMs: Long,
endTimeMs: Long,
autoTerminationMode: AutoTerminationMode = AutoTerminationMode.NONE,
parent: EmbraceSpan? = null,
type: TelemetryType = EmbType.Performance.Default,
internal: Boolean = true,
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.clock.nanosToMillis
import io.embrace.android.embracesdk.internal.spans.EmbraceSpanLimits.isNameValid
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 All @@ -29,6 +30,7 @@ internal class SpanServiceImpl(

override fun createSpan(
name: String,
autoTerminationMode: AutoTerminationMode,
parent: EmbraceSpan?,
type: TelemetryType,
internal: Boolean,
Expand All @@ -40,7 +42,8 @@ internal class SpanServiceImpl(
type = type,
internal = internal,
private = private,
parent = parent
parent = parent,
autoTerminationMode = autoTerminationMode,
)
} else {
null
Expand All @@ -60,6 +63,7 @@ internal class SpanServiceImpl(

override fun <T> recordSpan(
name: String,
autoTerminationMode: AutoTerminationMode,
parent: EmbraceSpan?,
type: TelemetryType,
internal: Boolean,
Expand All @@ -69,7 +73,14 @@ internal class SpanServiceImpl(
code: () -> T,
): T {
val returnValue: T
val span = createSpan(name = name, parent = parent, type = type, internal = internal, private = private)
val span = createSpan(
name = name,
autoTerminationMode = autoTerminationMode,
parent = parent,
type = type,
internal = internal,
private = private
)
try {
val started = span?.start() ?: false
if (started) {
Expand Down Expand Up @@ -98,6 +109,7 @@ internal class SpanServiceImpl(
name: String,
startTimeMs: Long,
endTimeMs: Long,
autoTerminationMode: AutoTerminationMode,
parent: EmbraceSpan?,
type: TelemetryType,
internal: Boolean,
Expand All @@ -116,7 +128,8 @@ internal class SpanServiceImpl(
type = type,
internal = internal,
private = private,
parent = parent
parent = parent,
autoTerminationMode = autoTerminationMode,
)
if (newSpan.start(startTimeMs)) {
attributes.forEach {
Expand Down
Loading

0 comments on commit 370b045

Please sign in to comment.