Skip to content

Ktor plugin does not capture kotlinx rpc (and perhaps even websocket) traces. #12939

Open
@jobinlawrance

Description

Describe the bug

When adding opentelemetry plugin to ktor server, only the http request are captured in the traces. KotlinX RPC (krpc) is added to Ktor via a plugin.

Unlike gRPC, kotlinx RPC does not use Http 2 and instead relies on websockets as is evident from the logs and it's plugin implementation which also leads me to believe that websocket traces are missing as well.

// It's installed on the server like 

install(RPC)

// internal implementation

public val RPC: ApplicationPlugin<RPCConfigBuilder.Server> = createApplicationPlugin(
    name = "RPC",
    createConfiguration = { RPCConfigBuilder.Server() },
) {
    application.install(WebSockets)           // < -----------------------------  Websockets
    application.attributes.put(RPCServerPluginAttributesKey, pluginConfig)
}

// And on the client

installRPC {
            waitForServices = true
}

// implementation

public fun HttpClientConfig<*>.installRPC(
    configure: RPCConfigBuilder.Client.() -> Unit = {}
) {
    install(WebSockets)                      // < -----------------------------  Websockets
    install(RPC, configure)
}

Steps to reproduce

In order to add OpenTelemetry plugin to Ktor server and client i've followed the offical ktor examples and implemented here

Expected behavior

The Ktor server has a bi-directional stream like

routing {
        rpc("/stream") {
            rpcConfig {
                serialization {
                    json{
                        ignoreUnknownKeys = true
                    }
                }
            }
        
            registerService<DeviceStream> { ctx -> DeviceStreamImpl(ctx) }
        }
    }

and a http GET request like

routing {
        get("/") {
            call.respondText("Hello World!")
        }
    }

Actual behavior

Only the http requests are automatically captured on the server

image image

In client if the initial RPC call fails I do get the trace for it but otherwise there are no traces related to rpc & websockets

image

Javaagent or library instrumentation version

opentelemetry-ktor-3.0 v2.10.0-alpha

Environment

JDK:
Temurin 21
OS:
MacOS Sequoia version 15.1

Additional context

No response

Metadata

Assignees

No one assigned

    Labels

    contribution welcomeRequest makes sense, maintainers probably won't have time, contribution would be welcomeenhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions