Interface RunContextPropagator

All Known Implementing Classes:
MicrometerRunContextPropagator, OpenTelemetryRunContextPropagator
Functional Interface:
This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

@Experimental @FunctionalInterface public interface RunContextPropagator
Propagates thread-local state from the thread running the handler code to the thread executing a Restate.run(java.lang.String, java.lang.Class<T>, dev.restate.common.function.ThrowingSupplier<T>) closure.

Restate.run(java.lang.String, java.lang.Class<T>, dev.restate.common.function.ThrowingSupplier<T>) closures are scheduled on the Executor provided in HandlerRunner.Options.setExecutor(Executor). Thread-local state installed on the handler thread (e.g. by a HandlerInterceptor opening a tracing span / observation scope, MDC values, security context, …) is not visible there by default. Implementations of this interface capture that state at submission time and restore it around the closure execution.

capture() is invoked on the handler thread (or whatever thread calls Restate.run(java.lang.String, java.lang.Class<T>, dev.restate.common.function.ThrowingSupplier<T>)) at submission time; RunContextPropagator.CapturedContext.wrap(java.lang.Runnable) is invoked later, and the resulting Runnable executes on the worker thread. The classic shape is:


 () -> {
   var captured = captureCurrentThreadState();
   return runnable -> () -> {
     try (var scope = captured.install()) {
       runnable.run();
     }
   };
 }
 

Propagators are discovered via ServiceLoader (global defaults) or registered explicitly via HandlerRunner.Options.addRunContextPropagator(RunContextPropagator). They apply to every Restate.run(java.lang.String, java.lang.Class<T>, dev.restate.common.function.ThrowingSupplier<T>) execution, including retries.