Skip to content

Commit

Permalink
Fix error span status for successful requests in Ktor (#12161)
Browse files Browse the repository at this point in the history
  • Loading branch information
marychatte authored Sep 3, 2024
1 parent 98bf183 commit 4af6a3d
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,11 @@ class KtorClientTracing internal constructor(
scope.launch {
val job = it.call.coroutineContext.job
job.join()
val cause = job.getCancellationException()
val cause = if (!job.isCancelled) {
null
} else {
kotlin.runCatching { job.getCancellationException() }.getOrNull()
}

plugin.endSpan(openTelemetryContext, it.call, cause)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import io.opentelemetry.instrumentation.testing.junit.http.HttpClientTestOptions
import io.opentelemetry.instrumentation.testing.junit.http.HttpClientTestOptions.DEFAULT_HTTP_ATTRIBUTES
import io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat
import io.opentelemetry.sdk.testing.assertj.TraceAssert
import io.opentelemetry.sdk.trace.data.StatusData
import io.opentelemetry.semconv.NetworkAttributes
import kotlinx.coroutines.*
import org.junit.jupiter.api.Test
Expand Down Expand Up @@ -89,7 +90,7 @@ abstract class AbstractKtorHttpClientTest : AbstractHttpClientTest<HttpRequestBu
testing.waitAndAssertTraces(
Consumer { trace: TraceAssert ->
val span = trace.getSpan(0)
assertThat(span).hasKind(SpanKind.CLIENT)
assertThat(span).hasKind(SpanKind.CLIENT).hasStatus(StatusData.unset())
assertThat(span.endEpochNanos - span.startEpochNanos >= 1_000_000_000)
.describedAs("Span duration should be at least 1000ms")
.isTrue()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,10 @@ void successfulGetRequest(String path) throws Exception {
testing.waitAndAssertTraces(
trace ->
trace.hasSpansSatisfyingExactly(
span -> assertClientSpan(span, uri, method, responseCode, null).hasNoParent(),
span ->
assertClientSpan(span, uri, method, responseCode, null)
.hasNoParent()
.hasStatus(StatusData.unset()),
span -> assertServerSpan(span).hasParent(trace.getSpan(0))));
}

Expand Down

0 comments on commit 4af6a3d

Please sign in to comment.