MDCContext

org.llm4s.agent.orchestration.MDCContext
object MDCContext

Thread-safe MDC (Mapped Diagnostic Context) management for async operations.

SLF4J MDC is thread-local, so context is lost when work moves across threads. This utility captures, restores, and propagates MDC state to ensure consistent logging context in Future-based and ExecutionContext-based pipelines.

Attributes

Graph
Supertypes
class Object
trait Matchable
class Any
Self type
MDCContext.type

Members list

Value members

Concrete methods

def capture(): Map[String, String]

Capture the current thread's MDC context as an immutable map.

Capture the current thread's MDC context as an immutable map.

Attributes

Returns

a snapshot of all MDC key-value pairs, or an empty map if none are set

def cleanup(keys: String*): Unit

Remove specific keys from the current thread's MDC.

Remove specific keys from the current thread's MDC.

Value parameters

keys

the MDC keys to remove

Attributes

def preservingExecutionContext(underlying: ExecutionContext): ExecutionContext

Wrap an ExecutionContext so that MDC context is captured at submission time and restored on the executing thread before each runnable runs.

Wrap an ExecutionContext so that MDC context is captured at submission time and restored on the executing thread before each runnable runs.

Value parameters

underlying

the execution context to wrap

Attributes

Returns

an MDC-preserving execution context

def preservingFuture[T](future: Future[T])(implicit ec: ExecutionContext): Future[T]

Map over a Future while preserving the caller's MDC context on the callback thread.

Map over a Future while preserving the caller's MDC context on the callback thread.

Value parameters

ec

implicit execution context for the map callback

future

the future to wrap

Attributes

Returns

a future whose map callback runs with the caller's MDC context

def set(context: Map[String, String]): Unit

Replace the current thread's MDC context with the given map.

Replace the current thread's MDC context with the given map.

Value parameters

context

the MDC key-value pairs to install

Attributes

def withContext[T](context: Map[String, String])(block: => T): T

Execute a block with the given MDC context, restoring the previous context afterward.

Execute a block with the given MDC context, restoring the previous context afterward.

Value parameters

block

the code to execute

context

MDC key-value pairs to set during execution

Attributes

Returns

the result of block

def withValues[T](values: (String, String)*)(block: => T): T

Execute a block with additional MDC values merged into the current context. The previous context is restored afterward.

Execute a block with additional MDC values merged into the current context. The previous context is restored afterward.

Value parameters

block

the code to execute

values

key-value pairs to add to the current MDC

Attributes

Returns

the result of block