TraceSpan

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

Link copied to clipboard

Key of TraceSpan in a CoroutineContext. Use coroutineContext[TraceSpan] to read it.

Properties

Link copied to clipboard
val id: Long

identifier of this span, unique per process.

Link copied to clipboard
open override val key: CoroutineContext.Key<*>
Link copied to clipboard

label of this span, such as "checkout". Keep it low-cardinality.

Link copied to clipboard

id of the enclosing span, or null for a root span.

Functions

Link copied to clipboard
open override fun <R> fold(initial: R, operation: (R, CoroutineContext.Element) -> R): R
Link copied to clipboard
open operator override fun <E : CoroutineContext.Element> get(key: CoroutineContext.Key<E>): E?
Link copied to clipboard
open override fun minusKey(key: CoroutineContext.Key<*>): CoroutineContext
Link copied to clipboard
open operator fun plus(context: CoroutineContext): CoroutineContext
Link copied to clipboard
open override fun toString(): String