I am trying to correctly trace transactions in a spark-kotlin server with newrelic. This particular server has intensive coroutine usage since it makes a lot of requests to other servers.
Newrelic has custom instrumentation that allows to track work being done in other threads from the one the transaction was originated at. The key instruments for this are:
- Token
- Trace with "async = true"
So to track the work on another thread, we must:
- Issue a Token in the original thread
- Link the token in the "worker thread"
- Expire the token when no more threads will be asociated with the transaction (See doc)
We also know, that one coroutine can be execute across multiple threads, so to correctly track the the work done in one coroutine we must link each of the threads. For this I figured a continuation interceptor might be used as explained in this question. The problem araises when we want to expire the token. Lets say we issue a token before calling a coroutine builder and set it in the coroutine context. We can then link the token in the continuation interceptor, but how can I know when a coroutine is finishing in order to expire the token? I tried using "ContinuationInterceptor::releaseInterceptedContinuation()" but it turns out it may be called several times in a single coroutine, as it is called after each continuation is relased.