Skip to content

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

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

Open
jobinlawrance opened this issue Dec 20, 2024 · 3 comments
Labels
contribution welcome Request makes sense, maintainers probably won't have time, contribution would be welcome enhancement New feature or request

Comments

@jobinlawrance
Copy link

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

@jobinlawrance jobinlawrance added bug Something isn't working needs triage New issue that requires triage labels Dec 20, 2024
@laurit
Copy link
Contributor

laurit commented Dec 20, 2024

We'd welcome a contribution for this.

@laurit laurit added contribution welcome Request makes sense, maintainers probably won't have time, contribution would be welcome enhancement New feature or request and removed bug Something isn't working needs triage New issue that requires triage labels Dec 20, 2024
@davidmrz
Copy link

davidmrz commented Apr 14, 2025

Hi, I was exploring this repository and came across this issue that is open for contributions. I gave it a look, but wanted to understand deeper the issue reported.

I was able to reproduce it. No spans are registered for the client when calling via websockets (which RPC uses under the hood). However, the spans are registered for the server (not linked to the client). Would the expectation be that the trace includes both, client and server spans, or is it something else?

@jobinlawrance @laurit

@laurit
Copy link
Contributor

laurit commented Apr 15, 2025

Would the expectation be that the trace includes both, client and server spans, or is it something else?

yes, both client and server spans should be created and appear in the same trace.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contribution welcome Request makes sense, maintainers probably won't have time, contribution would be welcome enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants