Package brave.baggage

Class CorrelationScopeDecorator

All Implemented Interfaces:

public abstract class CorrelationScopeDecorator
extends Object
implements CurrentTraceContext.ScopeDecorator
Synchronizes fields such as BaggageFields.TRACE_ID with a correlation context, such as logging through decoration of a scope. A maximum of 32 fields are supported.

Setup example:

 import brave.baggage.CorrelationScopeConfig.SingleCorrelationField;

 // Add the field "region", so it can be used as a log expression %X{region}
 CLOUD_REGION = BaggageFields.constant("region", System.getEnv("CLOUD_REGION"));

 decorator = MDCScopeDecorator.newBuilder()

 // Integrate the decorator
 tracing = Tracing.newBuilder()

 // Any scope operations (updates to the current span) apply the fields defined by the decorator.
 ScopedSpan span = tracing.tracer().startScopedSpan("encode");
 try {
   // The below log message will have %X{region} in the context!"Encoding the span, hope it works");
   return encoder.encode();
 } catch (RuntimeException | Error e) {
   span.error(e); // Unless you handle exceptions, you might not know the operation failed!
   throw e;
 } finally {
See Also:
CorrelationScopeConfig, CorrelationScopeCustomizer, BaggagePropagation