-
Notifications
You must be signed in to change notification settings - Fork 916
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
javaagent Kotlin coroutines usage (with ktor client), potential bug with delay and contexts. #12611
Comments
I didn't try to run and debug the snippets you provided and I don't know much about kotlin so the following is just a guess. Firstly when you open a scope with |
This has been automatically marked as stale because it has been marked as needing author feedback and has not had any activity for 7 days. It will be closed automatically if there is no response from the author within 7 additional days from this comment. |
I see so the following is a better baseline, avoiding using
This does work as expected, however adding a
Results in typically one orphaned span being created for one of the posts. The other is fine. It looks like |
I've done some further debugging, this behaviour appears to be specific to if there is no In my example I was launching the application as: suspend fun main(): Unit = coroutineScope { .. application .. } Which has no Starting the application as: suspend fun main(): Unit = withContext(Dispatchers.Default) { .. application .. } Generally solves it. This is something I can correct for, but perhaps there's something that could be done in the agent instrumentation to ensure that the OTEL context Element asserts that there's a Using my above examples the following works as expected in both conditions, and if the calling code is using a different dispatcher it will continue to use that: withContext(when {
coroutineContext[ContinuationInterceptor] != null -> span.asContextElement()
else -> span.asContextElement() + Dispatchers.Default
}) {
delay(1000)
post.execute { response ->
response.body<DummyDto>()
}
post.execute { response ->
response.body<DummyDto>()
}
} |
Whilst trying to get to grips on the expected usage of OTEL using the agent and coroutines I felt it was ambiguous the intended usage. And I'm not clear how and when the out-of-the-box javaagent behaviour kicks in. It appears like it's hard to escape peppering in the OTEL additions which looks easy for people to accidentally miss (which is not ideal!).
Let's assume this is always how the parent and root span is defined:
And the Ktor HttpClient is defined like this, I'll omit the request prepare statement as it's not relevant (as far as I know):
Both of the following leads to new trace IDs, not using my defined span as the parent:
If I'm not mistaken this looks like the intended use, which implies there's nothing out of the box with coroutines:
Works as expected, traceIds are correct with this example
In the prior example where the
withContext(Dispatchers.IO) { .. }
was used, whenever withContext is used we must+
the coroutine context e.g.:Works as expected
And to throw a spanner in the works if I add a
delay
in here it goes wrong again:Does not work as expected
However if I add the
Dispatchers.IO
it repairs it:Works as expected
I can also isolate the
delay
but it's getting very noisy at this point:Works as expected
I'm hoping some of this usability is the result of some subtle bug or something I've missed.
Using 2.9.0 agent with
The text was updated successfully, but these errors were encountered: