CorrelationContext

Thread-local correlation context for distributed tracing and audit trail linkage.

Provides automatic correlation ID generation and propagation across SDK operations, enabling end-to-end request tracing through logs, spans, and audit events.

Compliance (ISO 27001 A.12.4): Enables comprehensive audit trail by linking related operations across service boundaries.

Thread Safety: Uses ThreadLocal storage. Context must be explicitly copied when crossing thread boundaries (e.g., coroutine dispatchers).

Since

0.4.0

Properties

Link copied to clipboard

Current correlation ID. Auto-generated if not explicitly set. Format: 16 character lowercase hex string.

Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
const val KEY_SPAN_ID: String
Link copied to clipboard
const val KEY_TRACE_ID: String
Link copied to clipboard
const val KEY_USER_ID: String
Link copied to clipboard

Operation name for the current context.

Link copied to clipboard

Parent span ID for nested spans, or null if root span.

Link copied to clipboard

Current OpenTelemetry span ID (16 hex chars), or null if not in a span.

Link copied to clipboard

Current OpenTelemetry trace ID (32 hex chars), or null if not in a trace context.

Functions

Link copied to clipboard
fun clear()

Clear all context values for the current thread.

Link copied to clipboard

Generate a new correlation ID (16 lowercase hex characters).

Link copied to clipboard

Generate a new span ID (16 lowercase hex characters) following W3C Trace Context.

Link copied to clipboard

Generate a new trace ID (32 lowercase hex characters) following W3C Trace Context.

Link copied to clipboard
fun get(key: String): String?

Get a context value by key.

Link copied to clipboard
fun remove(key: String)

Remove a context value.

Link copied to clipboard
fun restore(snapshot: Map<String, String>)

Restore context from a snapshot (e.g., when crossing thread boundaries).

Link copied to clipboard
fun set(key: String, value: String)

Set a context value.

Link copied to clipboard

Get a snapshot of the current context for cross-thread propagation.

Link copied to clipboard
inline fun <T> withAttributes(attributes: Map<String, String>, block: () -> T): T

Execute a block with additional context attributes. Attributes are merged with existing context and removed after block completes.

Link copied to clipboard
inline fun <T> withContext(correlationId: String? = null, operationName: String? = null, block: () -> T): T

Execute a block with a new or specified correlation context. Previous context is restored after the block completes.