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

Insert OpenTelemetry phase before Setup #13239

Merged
merged 5 commits into from
Feb 11, 2025
Merged
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
@@ -33,26 +33,32 @@ object KtorServerTelemetryUtil {
val tracer = KtorServerTracer(instrumenter)
val startPhase = PipelinePhase("OpenTelemetry")

application.insertPhaseBefore(ApplicationCallPipeline.Monitoring, startPhase)
application.insertPhaseBefore(ApplicationCallPipeline.Setup, startPhase)
application.intercept(startPhase) {
val context = tracer.start(call)

if (context != null) {
call.attributes.put(contextKey, context)
withContext(context.asContextElement()) {
try {
proceed()
} catch (err: Throwable) {
// Stash error for reporting later since need ktor to finish setting up the response
call.attributes.put(errorKey, err)
throw err
}
proceed()
}
} else {
proceed()
}
}

val errorPhase = PipelinePhase("OpenTelemetryError")
application.insertPhaseBefore(ApplicationCallPipeline.Monitoring, errorPhase)
application.intercept(errorPhase) {
try {
proceed()
} catch (err: Throwable) {
// Stash error for reporting later since need ktor to finish setting up the response
call.attributes.put(errorKey, err)
throw err
}
}

val postSendPhase = PipelinePhase("OpenTelemetryPostSend")
application.sendPipeline.insertPhaseAfter(ApplicationSendPipeline.After, postSendPhase)
application.sendPipeline.intercept(postSendPhase) {
3 changes: 2 additions & 1 deletion instrumentation/ktor/ktor-3.0/library/README.md
Original file line number Diff line number Diff line change
@@ -32,7 +32,8 @@ implementation("io.opentelemetry.instrumentation:opentelemetry-ktor-3.0:OPENTELE

## Initializing server instrumentation

Initialize instrumentation by installing the `KtorServerTelemetry` feature.
Initialize instrumentation by installing the `KtorServerTelemetry` feature. Make sure that no other
logging plugin is installed before this.
You must set the `OpenTelemetry` to use with the feature.

```kotlin
Original file line number Diff line number Diff line change
@@ -5,9 +5,13 @@

package io.opentelemetry.instrumentation.ktor.v3_0

import io.ktor.http.HttpStatusCode
import io.ktor.server.application.*
import io.ktor.server.application.hooks.CallFailed
import io.ktor.server.response.respondText
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension
import io.opentelemetry.instrumentation.testing.junit.http.HttpServerInstrumentationExtension
import io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint
import org.junit.jupiter.api.extension.RegisterExtension

class KtorHttpServerTest : AbstractKtorHttpServerTest() {
@@ -27,6 +31,12 @@ class KtorHttpServerTest : AbstractKtorHttpServerTest() {
capturedRequestHeaders(TEST_REQUEST_HEADER)
capturedResponseHeaders(TEST_RESPONSE_HEADER)
}

install(createRouteScopedPlugin("Failure handler, that can mask exceptions if exception handling is in the wrong phase", ServerEndpoint.EXCEPTION.path, {}) {
on(CallFailed) { call, cause ->
call.respondText("failure: ${cause.message}", status = HttpStatusCode.InternalServerError)
}
})
}
}
}