-
-
Notifications
You must be signed in to change notification settings - Fork 450
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
Using kotlinx.coroutines.flow results in missing instrumentation #3142
Comments
Hey @rodolfoBee. I wouldn't use I can't see any usage of
This seems to work for me at first glance for Here's the spans on the transaction:
For Not quite sure yet how to fix but my guess is you may need WebFlux instead of WebMVC and then use https://docs.spring.io/spring-framework/docs/current/kdoc-api/spring-webflux/org.springframework.web.reactive.function.server/body-and-await.html |
The following code seems to work better:
This has some problems regarding serialization but at least the isn't finished mid request handling. I swapped the following in
Hope this helps. Let me know if you need more help. |
For Spring WebMVC maybe upgrading to Spring Boot 3 could help, but I didn't test. |
Hi @adinauer, Adding You are right that adding these codes prevent the "tracing data is lost" issue. But, in the first call to the endpoint, I see "Missing span instrumentation" note in the tracing flow in Sentry performance page. Spans created after this point are connected to main transaction, not to the related parent span. We could consider this as a workaround. But, after the first call to the endpoint, we dont see "Missing span instrumentation" part any more and time elapsed at that point. As a result, adding withContext or runBlocking is not a solution/workaround for us. We want to use Sentry tracing to detect long running (maybe problematic) parts in our project. But, since Sentry doesn't show these details, it's meaningless to us. Note: if we use Kotlin flow methods (i.e. toList()) in runBlocking, "tracing data is lost" issue is solved and call order is also correct. But, adding runBlocking will make these points blocking and it's not appropriate to use it in this way in reactive programming. |
@omurkartal we'll have to look into how Spring treats coroutines internally and check if there's a place to hook in our code differently. Then we could set the The following screenshot shows what happens with this code:
![]() Calling this endpoint multiple times, produces the same spans / chart for me. Looks like this is caused by using WebFlux instead of WebMVC which seems to handle suspend funs out of the box. So the transaction is no longer finished mid request in test1. With this code:
it ends up in Sentry the same. Can you please provide more details on your Flow usage? Is there always a single response that contains all the items in the flow or is there also some streaming where items are returned as they are produced? An alternative approach to supporting coroutines in Spring could be to offer a new Annotation that wraps a suspend fun with So far I can see that |
@adinauer @omurkartal I have a feeling you could solve the Flow nesting problem via using the
|
@romtsn looks to me like this is related to In The following code replaces the NOTE: this doesn't have any error handling
|
Hi @adinauer @romtsn, I checked the issue a bit more. In the sample project, whole request is executed in the same thread. But, in the real project, I see that a new thread is started in repository component. We use a DB driver which supports R2DBC (Reactive Relational Database Connectivity). Sample db-read code:
Logs:
|
Hi @omurkartal, the switch to another thread should not be problematic if I'm able to reproduce the behaviour of missing For the transactions where |
Adding more spans to |
Using |
Commenting out the flow in |
An alternative here could be using our OpenTelemetry integration and see if that works better. |
This issue has gone three weeks without activity. In another week, I will close it. But! If you comment or otherwise update it, I will reset the clock, and if you remove the label "A weed is but an unloved flower." ― Ella Wheeler Wilcox 🥀 |
Hi @adinauer, I could not test it in detail but I saw that the tracing data was lost in the same way with opentelemetry. |
Thanks for the update @omurkartal |
Hi @omurkartal, I just did a re-test of this using the latest version of our sentry-opentelemetry-agent and updated your sample project accordingly here. Using the newest version of the Agent and OpenTelemetry's In order to use |
We're closing this as this is now covered by our OpenTelemetry offering. Feel free to reopen if you need more help. |
Description
When using
kotlinx.coroutines.flow
to consume messages, the trace information is lost when calling methods (collect) in this package. After the kotlinx.coroutines.flow.collect(..) part is completed, tracing information is still missing.Steps to reproduce:
sample-project.zip
(screenshots available in the project)
The project is using the
@SentrySpan
notation to create custom spans for specific function, but the result is still missing instrumentation. Creating spans manually with startChild api from the custom instrumentation page also does not provide the expected result.The expected result is a transaction with spans for the main controller and all three Components, while the current result is missing the spans from the third component.
The text was updated successfully, but these errors were encountered: