-
Notifications
You must be signed in to change notification settings - Fork 967
Fix ending span in Ktor plugin #11726
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
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,15 +10,21 @@ import io.ktor.client.engine.cio.* | |
import io.ktor.client.plugins.* | ||
import io.ktor.client.request.* | ||
import io.ktor.http.* | ||
import io.opentelemetry.api.trace.SpanKind | ||
import io.opentelemetry.context.Context | ||
import io.opentelemetry.extension.kotlin.asContextElement | ||
import io.opentelemetry.instrumentation.testing.junit.http.AbstractHttpClientTest | ||
import io.opentelemetry.instrumentation.testing.junit.http.HttpClientResult | ||
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 | ||
import io.opentelemetry.sdk.testing.assertj.TraceAssert | ||
import io.opentelemetry.semconv.NetworkAttributes | ||
import kotlinx.coroutines.* | ||
import org.assertj.core.api.Assertions | ||
import org.junit.jupiter.api.Test | ||
import java.net.URI | ||
import java.util.function.Consumer | ||
|
||
abstract class AbstractKtorHttpClientTest : AbstractHttpClientTest<HttpRequestBuilder>() { | ||
|
||
|
@@ -71,4 +77,24 @@ abstract class AbstractKtorHttpClientTest : AbstractHttpClientTest<HttpRequestBu | |
} | ||
} | ||
} | ||
|
||
@Test | ||
fun checkSpanEndsAfterBodyReceived() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it make sense to move this to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure because according to the documentation, it's okay to have both behaviors: HTTP client spans SHOULD end sometime after the HTTP response headers are fully read (or when they fail to be read). This may or may not include reading the response body. (https://opentelemetry.io/docs/specs/semconv/http/http-spans/#http-client-span-duration) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @marychatte can you confirm that the change conforms with this part of the spec also? thanks
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @trask, there will be no later from There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thanks, what happens if a user reads the response headers and then decides not to read (ignore) the body? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @trask In Ktor, we read the raw body in memory before it, and even if the user does not decide to call |
||
val method = "GET" | ||
val path = "/long-request" | ||
val uri = resolveAddress(path) | ||
val responseCode = doRequest(method, uri) | ||
|
||
Assertions.assertThat(responseCode).isEqualTo(200) | ||
|
||
testing.waitAndAssertTraces( | ||
Consumer { trace: TraceAssert -> | ||
val span = trace.getSpan(0) | ||
OpenTelemetryAssertions.assertThat(span).hasKind(SpanKind.CLIENT) | ||
Assertions.assertThat(span.endEpochNanos - span.startEpochNanos >= 1000000) | ||
trask marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.describedAs("Span duration should be at least 1000ms") | ||
.isTrue() | ||
} | ||
) | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.