Skip to content

Commit 4af6a3d

Browse files
authored
Fix error span status for successful requests in Ktor (#12161)
1 parent 98bf183 commit 4af6a3d

File tree

3 files changed

+11
-3
lines changed

3 files changed

+11
-3
lines changed

instrumentation/ktor/ktor-2.0/library/src/main/kotlin/io/opentelemetry/instrumentation/ktor/v2_0/client/KtorClientTracing.kt

+5-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,11 @@ class KtorClientTracing internal constructor(
9898
scope.launch {
9999
val job = it.call.coroutineContext.job
100100
job.join()
101-
val cause = job.getCancellationException()
101+
val cause = if (!job.isCancelled) {
102+
null
103+
} else {
104+
kotlin.runCatching { job.getCancellationException() }.getOrNull()
105+
}
102106

103107
plugin.endSpan(openTelemetryContext, it.call, cause)
104108
}

instrumentation/ktor/ktor-2.0/testing/src/main/kotlin/io/opentelemetry/instrumentation/ktor/v2_0/client/AbstractKtorHttpClientTest.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import io.opentelemetry.instrumentation.testing.junit.http.HttpClientTestOptions
1919
import io.opentelemetry.instrumentation.testing.junit.http.HttpClientTestOptions.DEFAULT_HTTP_ATTRIBUTES
2020
import io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat
2121
import io.opentelemetry.sdk.testing.assertj.TraceAssert
22+
import io.opentelemetry.sdk.trace.data.StatusData
2223
import io.opentelemetry.semconv.NetworkAttributes
2324
import kotlinx.coroutines.*
2425
import org.junit.jupiter.api.Test
@@ -89,7 +90,7 @@ abstract class AbstractKtorHttpClientTest : AbstractHttpClientTest<HttpRequestBu
8990
testing.waitAndAssertTraces(
9091
Consumer { trace: TraceAssert ->
9192
val span = trace.getSpan(0)
92-
assertThat(span).hasKind(SpanKind.CLIENT)
93+
assertThat(span).hasKind(SpanKind.CLIENT).hasStatus(StatusData.unset())
9394
assertThat(span.endEpochNanos - span.startEpochNanos >= 1_000_000_000)
9495
.describedAs("Span duration should be at least 1000ms")
9596
.isTrue()

testing-common/src/main/java/io/opentelemetry/instrumentation/testing/junit/http/AbstractHttpClientTest.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,10 @@ void successfulGetRequest(String path) throws Exception {
118118
testing.waitAndAssertTraces(
119119
trace ->
120120
trace.hasSpansSatisfyingExactly(
121-
span -> assertClientSpan(span, uri, method, responseCode, null).hasNoParent(),
121+
span ->
122+
assertClientSpan(span, uri, method, responseCode, null)
123+
.hasNoParent()
124+
.hasStatus(StatusData.unset()),
122125
span -> assertServerSpan(span).hasParent(trace.getSpan(0))));
123126
}
124127

0 commit comments

Comments
 (0)