Skip to content

Commit 53ba03a

Browse files
committed
Workaround binary breaking change
1 parent 5f51e77 commit 53ba03a

File tree

1 file changed

+22
-3
lines changed
  • instrumentation/ktor/ktor-2.0/library/src/main/kotlin/io/opentelemetry/instrumentation/ktor/v2_0/server

1 file changed

+22
-3
lines changed

instrumentation/ktor/ktor-2.0/library/src/main/kotlin/io/opentelemetry/instrumentation/ktor/v2_0/server/KtorServerTracing.kt

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

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

8+
import io.ktor.events.EventDefinition
89
import io.ktor.http.*
9-
import io.ktor.serialization.*
1010
import io.ktor.server.application.*
1111
import io.ktor.server.request.*
1212
import io.ktor.server.response.*
@@ -260,7 +260,26 @@ val KtorServerTracing = createRouteScopedPlugin("OpenTelemetry", ::KtorServerTra
260260
}
261261
}
262262

263-
application.environment.monitor.subscribe(RoutingCallStarted) { call ->
264-
HttpServerRoute.update(Context.current(), HttpServerRouteSource.SERVER, { _, arg -> arg.route.parent.toString() }, call)
263+
val callStartedEvent = runCatching {
264+
RoutingCallStarted // Ktor 2.0
265+
}.getOrElse {
266+
// Ktor 3.0
267+
val routingRoot = Class.forName("io.ktor.server.routing.RoutingRoot")
268+
val callStartedGetter = routingRoot.getDeclaredMethod("access\$getRoutingCallStarted\$cp")
269+
callStartedGetter.invoke(null) as EventDefinition<ApplicationCall>
270+
}
271+
272+
application.environment.monitor.subscribe(callStartedEvent) { call ->
273+
HttpServerRoute.update(Context.current(), HttpServerRouteSource.SERVER, { _, arg ->
274+
runCatching {
275+
// Ktor 2.0
276+
(call as RoutingApplicationCall).route.parent.toString()
277+
}.getOrElse {
278+
// Ktor 3.0
279+
val route = arg::class.java.getDeclaredMethod("getRoute").invoke(arg)
280+
val parent = route::class.java.getDeclaredMethod("getParent").invoke(route)
281+
parent.toString()
282+
}
283+
}, call)
265284
}
266285
}

0 commit comments

Comments
 (0)