Trace Span
A unit of traced work, carried in the coroutine context. TracingDispatcher reads it from the context of every dispatch and attaches id, parentId and name to the run events, so a backend can draw parent-child links and show the span label.
A plain context element (not a thread-context element), so it works on every target. It is inherited by child coroutines like any other element. Ids are unique per process, assigned from an increasing counter. Open spans with withTraceSpan; the library helpers hedged, parallel, supervisedAll and concurrentOrdered open a child span only when the caller already has one.
Example:
withTraceSpan("checkout") {
val span = coroutineContext[TraceSpan] // id, parentId = null, name = "checkout"
}Requires opting in to ExperimentalStructuredCoroutines. Instances are created by withTraceSpan; the constructor is internal.
Types
Key of TraceSpan in a CoroutineContext. Use coroutineContext[TraceSpan] to read it.