Skip to content

Commit

Permalink
Ktor2 http client uses low level instrumentation (#12530)
Browse files Browse the repository at this point in the history
  • Loading branch information
laurit authored Oct 29, 2024
1 parent 40640a3 commit 86ee95c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import io.opentelemetry.context.Context
import io.opentelemetry.context.propagation.ContextPropagators
import io.opentelemetry.extension.kotlin.asContextElement
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter
import io.opentelemetry.instrumentation.api.semconv.http.HttpClientRequestResendCount
import kotlinx.coroutines.InternalCoroutinesApi
import kotlinx.coroutines.job
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -63,6 +64,14 @@ class KtorClientTracing internal constructor(
}

private fun installSpanCreation(plugin: KtorClientTracing, scope: HttpClient) {
val initializeRequestPhase = PipelinePhase("OpenTelemetryInitializeRequest")
scope.requestPipeline.insertPhaseAfter(HttpRequestPipeline.State, initializeRequestPhase)

scope.requestPipeline.intercept(initializeRequestPhase) {
val openTelemetryContext = HttpClientRequestResendCount.initialize(Context.current())
withContext(openTelemetryContext.asContextElement()) { proceed() }
}

val createSpanPhase = PipelinePhase("OpenTelemetryCreateSpan")
scope.sendPipeline.insertPhaseAfter(HttpSendPipeline.State, createSpanPhase)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,8 @@ abstract class AbstractKtorHttpClientTest : AbstractHttpClientTest<HttpRequestBu
override fun configure(optionsBuilder: HttpClientTestOptions.Builder) {
with(optionsBuilder) {
disableTestReadTimeout()
// this instrumentation creates a span per each physical request
// related issue https://github.com/open-telemetry/opentelemetry-java-instrumentation/issues/5722
disableTestRedirects()
markAsLowLevelInstrumentation()
setMaxRedirects(20)
spanEndsAfterBody()

setHttpAttributes { DEFAULT_HTTP_ATTRIBUTES - setOf(NetworkAttributes.NETWORK_PROTOCOL_VERSION) }
Expand Down

0 comments on commit 86ee95c

Please sign in to comment.