Skip to content
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

Implement @WithSpan support for kotlin coroutines #8870

Merged
merged 8 commits into from
Aug 16, 2023

Conversation

laurit
Copy link
Contributor

@laurit laurit commented Jul 5, 2023

Resolves #8812
With kotlin coroutines current @WithSpan handling that starts span on method entry and ends it when method exits doesn't work well because method will be exited when it calls a blocking operation and later called again to resume the coroutine. This creates multiple spans for the same method and messes up context propagation. This pr explores an alternative @WithSpan implementation for coroutines where span is started when method is first called and ended when it returns a result, calls to resume the continuation will only restore the scope and not start a new span.
There is also support for the @SpanAttribute annotation, but it supports less types than what is supported for the regular @WithSpan handling. Support for @AddingSpanAttributes annotation has not been implemented.

@laurit laurit requested a review from a team as a code owner July 5, 2023 12:09

public static Context enterCoroutine(
int label, Continuation<?> continuation, MethodRequest request) {
// label 0 means that coroutine is started, any other label means that coroutine is resumed
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you know of any documentation/kotlin design doc that'd explain the label value? I couldn't find any, unfortunately.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://www.youtube.com/watch?v=YrrUCSi72E8 was useful if I remember correctly (around 7:30 there is something about labels). Basically it should work so that when coroutine is suspended it returns the label where it should be resumed and on resume it is called with the same label.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I'll watch that

Copy link
Member

@mateuszrzeszutek mateuszrzeszutek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@cleidiano
Copy link

cleidiano commented Aug 16, 2023

This PR will solve the wrong span duration reported at this issue #6502?

@@ -10,25 +10,35 @@ muzzle {
group.set("org.jetbrains.kotlinx")
module.set("kotlinx-coroutines-core")
versions.set("[1.0.0,1.3.8)")
extraDependency(project(":instrumentation-annotations"))
extraDependency("io.opentelemetry:opentelemetry-api:1.27.0")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we also keep the original muzzle definition and use excludeInstrumentationName, to make sure the kotlin instrumentation is still applied in the absence of these dependencies on the user classpath?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or possibly extract out into separate module?

laurit and others added 2 commits August 16, 2023 23:27
Co-authored-by: Trask Stalnaker <trask.stalnaker@gmail.com>
@trask trask merged commit 56dfd1e into open-telemetry:main Aug 16, 2023
46 checks passed
@laurit laurit deleted the kotlin-coroutine-withspan branch August 17, 2023 04:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Kotlin Coroutine Flow not tracked correctly if it emits more that 1 item and takes time
4 participants