requireTrace
Unsafe-ish extension for extracting a Trace from inside a coroutine.
This will throw if attempting to do any sort of tracing from inside a no-context runBlocking call. If it's necessary to use runBlocking
, the parent trace must be passed in as a `coroutineContext] argument.
suspend function doSomething() {
// extract the trace from the existing context.
val trace = coroutineContext.requireTrace()
// Sequence.forEach { ... } is an example of a lambda which doesn't suspend
// or provide a CoroutineScope
getSomeSequence().forEach {
// now if we want to do suspending work, we need runBlocking
// manually pass in the trace from the parent coroutine
runBlocking(trace) {
// now the trace continues
}
}
}
Content copied to clipboard
See also
Throws
if the coroutineContext does not have a Trace