Class BaggageField
public final class BaggageField extends Object
BaggagePropagation
is configured.
For example, if you have a need to know a specific request's country code in a downstream service, you can propagate it through the trace:
// Configure your baggage field
COUNTRY_CODE = BaggageField.create("country-code");
Usage
As long as a field is configured withBaggagePropagation
, local reads and updates are
possible in-process.
Ex. once added to `BaggagePropagation`, you can call below to affect the country code of the current trace context:
COUNTRY_CODE.updateValue("FO");
String countryCode = COUNTRY_CODE.get();
Or, if you have a reference to a trace context, it is more efficient to use it explicitly:
COUNTRY_CODE.updateValue(span.context(), "FO");
String countryCode = COUNTRY_CODE.get(span.context());
Tags.BAGGAGE_FIELD.tag(COUNTRY_CODE, span);
Correlation
You can also integrate baggage with other correlated contexts such as logging:
import brave.baggage.BaggagePropagationConfig.SingleBaggageField;
import brave.baggage.CorrelationScopeConfig.SingleCorrelationField;
AMZN_TRACE_ID = BaggageField.create("x-amzn-trace-id");
// Allow logging patterns like %X{traceId} %X{x-amzn-trace-id}
decorator = MDCScopeDecorator.newBuilder()
.add(SingleCorrelationField.create(AMZN_TRACE_ID)).build()
tracingBuilder.propagationFactory(BaggagePropagation.newFactoryBuilder(B3Propagation.FACTORY)
.add(SingleBaggageField.remote(AMZN_TRACE_ID))
.build())
.currentTraceContext(ThreadLocalCurrentTraceContext.newBuilder()
.addScopeDecorator(decorator)
.build())
Appropriate usage
It is generally not a good idea to use the tracing system for application logic or critical code such as security context propagation.Brave is an infrastructure library: you will create lock-in if you expose its apis into business code. Prefer exposing your own types for utility functions that use this class as this will insulate you from lock-in.
While it may seem convenient, do not use this for security context propagation as it was not designed for this use case. For example, anything placed in here can be accessed by any code in the same classloader!
Background
The name Baggage was first introduced by Brown University in Pivot Tracing as maps, sets and tuples. They then spun baggage out as a standalone component, BaggageContext and considered some of the nuances of making it general purpose. The implementations proposed in these papers are different to the implementation here, but conceptually the goal is the same: to propagate "arbitrary stuff" with a request.- Since:
- 5.11
- See Also:
BaggagePropagation
,CorrelationScopeConfig
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
BaggageField.ValueUpdater
Used to decouple baggage value updates fromTraceContext
orTraceContextOrSamplingFlags
storage. -
Method Summary
Modifier and Type Method Description static BaggageField
create(String name)
boolean
equals(Object o)
Returns true for any baggage field with the same name (case insensitive).static List<BaggageField>
getAll()
Deprecated.Since 5.12 usegetAllValues(TraceContext)
static List<BaggageField>
getAll(TraceContext context)
Deprecated.Since 5.12 usegetAllValues(TraceContext)
static List<BaggageField>
getAll(TraceContextOrSamplingFlags extracted)
Deprecated.Since 5.12 usegetAllValues(TraceContext)
static Map<String,String>
getAllValues()
LikegetAllValues(TraceContext)
except against the current trace context.static Map<String,String>
getAllValues(TraceContext context)
static Map<String,String>
getAllValues(TraceContextOrSamplingFlags extracted)
static BaggageField
getByName(TraceContextOrSamplingFlags extracted, String name)
Looks up the field byname
, useful for when you do not have a reference to it.static BaggageField
getByName(TraceContext context, String name)
Looks up the field byname
, useful for when you do not have a reference to it.static BaggageField
getByName(String name)
LikegetByName(TraceContext, String)
except against the current trace context.String
getValue()
LikegetValue(TraceContext)
except against the current trace context.String
getValue(TraceContext context)
Returns the most recent value for this field in the context or null if unavailable.String
getValue(TraceContextOrSamplingFlags extracted)
LikegetValue(TraceContext)
except for use cases that precede a span.int
hashCode()
Returns the same value for any baggage field with the same name (case insensitive).String
name()
The non-empty name of the field.String
toString()
boolean
updateValue(TraceContextOrSamplingFlags extracted, String value)
LikeupdateValue(TraceContext, String)
except for use cases that precede a span.boolean
updateValue(TraceContext context, String value)
Updates the value of this field, or ignores if read-only or not configured.boolean
updateValue(String value)
LikeupdateValue(TraceContext, String)
except against the current trace context.
-
Method Details
-
create
- Parameters:
name
- Seename()
- Since:
- 5.11
-
getAll
Deprecated.Since 5.12 usegetAllValues(TraceContext)
-
getAll
Deprecated.Since 5.12 usegetAllValues(TraceContext)
-
getAll
Deprecated.Since 5.12 usegetAllValues(TraceContext)
-
getAllValues
- Since:
- 5.12
- See Also:
getAllValues(TraceContextOrSamplingFlags)
-
getAllValues
- Since:
- 5.12
- See Also:
getAllValues(TraceContext)
-
getAllValues
LikegetAllValues(TraceContext)
except against the current trace context.Prefer
getAllValues(TraceContext)
if you have a reference to the trace context.- Since:
- 5.12
-
getByName
Looks up the field byname
, useful for when you do not have a reference to it. In general,BaggageField
s should be referenced directly as constants where possible.- Since:
- 5.11
-
getByName
Looks up the field byname
, useful for when you do not have a reference to it. In general,BaggageField
s should be referenced directly as constants where possible.- Since:
- 5.11
-
getByName
LikegetByName(TraceContext, String)
except against the current trace context.Prefer
getByName(TraceContext, String)
if you have a reference to the trace context.- Since:
- 5.11
-
name
The non-empty name of the field. Ex "userId".For example, if using log correlation and with field named "userId", the value becomes the log variable
%{userId}
when the span is next made current.- Since:
- 5.11
- See Also:
getByName(TraceContext, String)
,CorrelationScopeConfig.SingleCorrelationField.name()
-
getValue
Returns the most recent value for this field in the context or null if unavailable.The result may not be the same as the one
extracted
from the incoming context becauseupdateValue(String)
can override it.- Since:
- 5.11
-
getValue
LikegetValue(TraceContext)
except against the current trace context.Prefer
getValue(TraceContext)
if you have a reference to the trace context.- Since:
- 5.11
-
getValue
LikegetValue(TraceContext)
except for use cases that precede a span. For example, a trace ID context.- Since:
- 5.11
-
updateValue
Updates the value of this field, or ignores if read-only or not configured.- Since:
- 5.11
-
updateValue
LikeupdateValue(TraceContext, String)
except for use cases that precede a span. For example, a trace ID context.- Since:
- 5.11
-
updateValue
LikeupdateValue(TraceContext, String)
except against the current trace context.Prefer
updateValue(TraceContext, String)
if you have a reference to the trace context.- Since:
- 5.11
-
toString
-
equals
Returns true for any baggage field with the same name (case insensitive). -
hashCode
public final int hashCode()Returns the same value for any baggage field with the same name (case insensitive).
-