Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error span status for successful requests in Ktor #12161

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading