Skip to content

Commit 6e2d404

Browse files
committed
Replace Span.makeCurrent() with Span.asContextElement()
1 parent 5323882 commit 6e2d404

File tree

7 files changed

+16
-5
lines changed

7 files changed

+16
-5
lines changed

gradle/libs.versions.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ okhttp4 = { module = "com.squareup.okhttp3:okhttp", version.ref = "okhttp4-versi
5757
okhttp-coroutines = { module = "com.squareup.okhttp3:okhttp-coroutines", version.ref = "okhttp-version" }
5858
opentelemetry-api = { module = "io.opentelemetry:opentelemetry-api", version.ref = "otel-version" }
5959
opentelemetry-sdk-testing = {module = "io.opentelemetry:opentelemetry-sdk-testing", version.ref = "otel-version" }
60+
opentelemetry-kotlin-extension = { module = "io.opentelemetry:opentelemetry-extension-kotlin", version.ref = "otel-version" }
6061
slf4j-api = { module = "org.slf4j:slf4j-api", version.ref = "slf4j-version" }
6162
slf4j-api-v1x = { module = "org.slf4j:slf4j-api", version.ref = "slf4j-v1x-version" }
6263
slf4j-simple = { module = "org.slf4j:slf4j-simple", version.ref = "slf4j-version" }

runtime/observability/telemetry-api/api/telemetry-api.api

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,7 @@ public final class aws/smithy/kotlin/runtime/telemetry/metrics/UpDownCounter$Def
364364

365365
public abstract class aws/smithy/kotlin/runtime/telemetry/trace/AbstractTraceSpan : aws/smithy/kotlin/runtime/telemetry/trace/TraceSpan {
366366
public fun <init> ()V
367+
public fun asContextElement ()Lkotlin/coroutines/CoroutineContext;
367368
public fun close ()V
368369
public fun emitEvent (Ljava/lang/String;Laws/smithy/kotlin/runtime/collections/Attributes;)V
369370
public fun getSpanContext ()Laws/smithy/kotlin/runtime/telemetry/trace/SpanContext;
@@ -424,6 +425,7 @@ public final class aws/smithy/kotlin/runtime/telemetry/trace/SpanStatus : java/l
424425

425426
public abstract interface class aws/smithy/kotlin/runtime/telemetry/trace/TraceSpan : aws/smithy/kotlin/runtime/telemetry/context/Scope {
426427
public static final field Companion Laws/smithy/kotlin/runtime/telemetry/trace/TraceSpan$Companion;
428+
public abstract fun asContextElement ()Lkotlin/coroutines/CoroutineContext;
427429
public abstract fun close ()V
428430
public abstract fun emitEvent (Ljava/lang/String;Laws/smithy/kotlin/runtime/collections/Attributes;)V
429431
public abstract fun getSpanContext ()Laws/smithy/kotlin/runtime/telemetry/trace/SpanContext;

runtime/observability/telemetry-api/common/src/aws/smithy/kotlin/runtime/telemetry/trace/AbstractTraceSpan.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ package aws.smithy.kotlin.runtime.telemetry.trace
66

77
import aws.smithy.kotlin.runtime.collections.AttributeKey
88
import aws.smithy.kotlin.runtime.collections.Attributes
9+
import kotlin.coroutines.CoroutineContext
10+
import kotlin.coroutines.EmptyCoroutineContext
911

1012
/**
1113
* An abstract implementation of a trace span. By default, this class uses no-op implementations for all members unless
@@ -18,4 +20,5 @@ public abstract class AbstractTraceSpan : TraceSpan {
1820
override operator fun <T : Any> set(key: AttributeKey<T>, value: T) { }
1921
override fun mergeAttributes(attributes: Attributes) { }
2022
override fun close() { }
23+
override fun asContextElement(): CoroutineContext = EmptyCoroutineContext
2124
}

runtime/observability/telemetry-api/common/src/aws/smithy/kotlin/runtime/telemetry/trace/CoroutineContextTraceExt.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public suspend inline fun <R> withSpan(
7171
// or else traces may be disconnected from their parent
7272
val updatedCtx = coroutineContext[TelemetryProviderContext]?.provider?.contextManager?.current()
7373
val telemetryCtxElement = (updatedCtx?.let { TelemetryContextElement(it) } ?: coroutineContext[TelemetryContextElement]) ?: EmptyCoroutineContext
74-
withContext(context + TraceSpanContext(span) + telemetryCtxElement) {
74+
withContext(context + TraceSpanContext(span) + telemetryCtxElement + span.asContextElement()) {
7575
block(span)
7676
}
7777
} catch (ex: Exception) {

runtime/observability/telemetry-api/common/src/aws/smithy/kotlin/runtime/telemetry/trace/TraceSpan.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import aws.smithy.kotlin.runtime.collections.AttributeKey
99
import aws.smithy.kotlin.runtime.collections.Attributes
1010
import aws.smithy.kotlin.runtime.collections.emptyAttributes
1111
import aws.smithy.kotlin.runtime.telemetry.context.Scope
12+
import kotlin.coroutines.CoroutineContext
1213

1314
/**
1415
* Represents a single operation/task within a trace. Each trace contains a root span and
@@ -27,6 +28,8 @@ public interface TraceSpan : Scope {
2728
*/
2829
public val spanContext: SpanContext
2930

31+
public fun asContextElement(): CoroutineContext
32+
3033
/**
3134
* Set an attribute on the span
3235
* @param key the attribute key to use

runtime/observability/telemetry-provider-otel/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ kotlin {
1818
jvmMain {
1919
dependencies {
2020
api(libs.opentelemetry.api)
21+
api(libs.opentelemetry.kotlin.extension)
2122
}
2223
}
2324
all {

runtime/observability/telemetry-provider-otel/jvm/src/aws/smithy/kotlin/runtime/telemetry/otel/OtelTracerProvider.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import aws.smithy.kotlin.runtime.collections.Attributes
1010
import aws.smithy.kotlin.runtime.telemetry.context.Context
1111
import aws.smithy.kotlin.runtime.telemetry.trace.*
1212
import io.opentelemetry.api.OpenTelemetry
13+
import io.opentelemetry.extension.kotlin.asContextElement
14+
import kotlin.coroutines.CoroutineContext
1315
import io.opentelemetry.api.trace.Span as OtelSpan
1416
import io.opentelemetry.api.trace.SpanContext as OtelSpanContext
1517
import io.opentelemetry.api.trace.SpanKind as OtelSpanKind
@@ -62,11 +64,11 @@ private class OtelSpanContextImpl(private val otelSpanContext: OtelSpanContext)
6264
internal class OtelTraceSpanImpl(
6365
private val otelSpan: OtelSpan,
6466
) : TraceSpan {
65-
66-
private val spanScope = otelSpan.makeCurrent()
67-
6867
override val spanContext: SpanContext
6968
get() = OtelSpanContextImpl(otelSpan.spanContext)
69+
70+
override fun asContextElement(): CoroutineContext = otelSpan.asContextElement()
71+
7072
override fun <T : Any> set(key: AttributeKey<T>, value: T) {
7173
key.otelAttrKeyOrNull(value)?.let { otelKey ->
7274
otelSpan.setAttribute(otelKey, value)
@@ -90,7 +92,6 @@ internal class OtelTraceSpanImpl(
9092

9193
override fun close() {
9294
otelSpan.end()
93-
spanScope.close()
9495
}
9596
}
9697

0 commit comments

Comments
 (0)