Skip to content

Commit

Permalink
Workaround binary breaking change
Browse files Browse the repository at this point in the history
  • Loading branch information
e5l committed Oct 31, 2024
1 parent 5f51e77 commit 53ba03a
Showing 1 changed file with 22 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

package io.opentelemetry.instrumentation.ktor.v2_0.server

import io.ktor.events.EventDefinition
import io.ktor.http.*
import io.ktor.serialization.*
import io.ktor.server.application.*
import io.ktor.server.request.*
import io.ktor.server.response.*
Expand Down Expand Up @@ -260,7 +260,26 @@ val KtorServerTracing = createRouteScopedPlugin("OpenTelemetry", ::KtorServerTra
}
}

application.environment.monitor.subscribe(RoutingCallStarted) { call ->
HttpServerRoute.update(Context.current(), HttpServerRouteSource.SERVER, { _, arg -> arg.route.parent.toString() }, call)
val callStartedEvent = runCatching {
RoutingCallStarted // Ktor 2.0
}.getOrElse {
// Ktor 3.0
val routingRoot = Class.forName("io.ktor.server.routing.RoutingRoot")
val callStartedGetter = routingRoot.getDeclaredMethod("access\$getRoutingCallStarted\$cp")
callStartedGetter.invoke(null) as EventDefinition<ApplicationCall>
}

application.environment.monitor.subscribe(callStartedEvent) { call ->
HttpServerRoute.update(Context.current(), HttpServerRouteSource.SERVER, { _, arg ->
runCatching {
// Ktor 2.0
(call as RoutingApplicationCall).route.parent.toString()
}.getOrElse {
// Ktor 3.0
val route = arg::class.java.getDeclaredMethod("getRoute").invoke(arg)
val parent = route::class.java.getDeclaredMethod("getParent").invoke(route)
parent.toString()
}
}, call)
}
}

0 comments on commit 53ba03a

Please sign in to comment.