dd-trace-java icon indicating copy to clipboard operation
dd-trace-java copied to clipboard

Span in Kotlin `withTimeout` has wrong parent

Open yonigibbs opened this issue 2 months ago • 3 comments

Tracer Version(s)

1.54.0

Java Version(s)

21.0.7

JVM Vendor

Other (please specify in comments)

Bug Report

Hi! I have the following code in a Kotlin Ktor server application, when handling an incoming HTTP request:

post("/foo") {
    span("withTimeoutOutside") {
        delay(100)
        withTimeout(500) {
            span("withTimeoutInside") {
                delay(100)
            }
        }
    }
}

I'd expect the root span (for the HTTP request) to have a child of withTimeoutOutside, which has a child of withTimeoutInside. Instead, what I see is this:

Image

This is possibly related to the fixes put in place for https://github.com/DataDog/dd-trace-java/issues/7215.

Let me know if you need me to provide anything else to help with this.

Thanks in advance!

(Am running on OpenJDK 64-Bit Server VM Temurin-21.0.7+6)

Expected Behavior

I'd expect the root span (for the HTTP request) to have a child of withTimeoutOutside, which has a child of withTimeoutInside.

Reproduction Code

No response

yonigibbs avatar Oct 31 '25 13:10 yonigibbs

Hi @yonigibbs - could you attach a buildable example? That would help progress this issue

The changes in #7215 improved context propagation for Kotlin coroutines in general, but it's possible there's an edge-case in how our OpenTelemetry compatibility layer interacts with KTor, specifically KTor's span extension methods that use the OpenTelemetry API to create and activate spans.

mcculls avatar Nov 11 '25 16:11 mcculls

Sure, here's a repo that should allow you to reproduce the issue: https://github.com/yonigibbs/dd-coroutines.

While trying to create this I discovered that it seems to be related to ktor's CallLogging plugin. More details in the README in the repo.

Lemme know if I can help in any way, and thanks for taking the time to reply 👍

yonigibbs avatar Nov 17 '25 14:11 yonigibbs

Thanks for the example and extra info @yonigibbs

mcculls avatar Nov 19 '25 11:11 mcculls