Package brave.baggage
Class BaggagePropagation<K>
- java.lang.Object
-
- brave.baggage.BaggagePropagation<K>
-
- All Implemented Interfaces:
Propagation<K>
public final class BaggagePropagation<K> extends Object implements Propagation<K>
This implements in-process and remote baggage propagation.For example, if you have a need to know the a specific request's country code, you can propagate it through the trace as HTTP headers.
import brave.baggage.BaggagePropagationConfig.SingleBaggageField; // Configure your baggage field COUNTRY_CODE = BaggageField.create("country-code"); // When you initialize the builder, add the baggage you want to propagate tracingBuilder.propagationFactory( BaggagePropagation.newFactoryBuilder(B3Propagation.FACTORY) .add(SingleBaggageField.remote(COUNTRY_CODE)) .build() ); // later, you can tag that country code Tags.BAGGAGE_FIELD.tag(COUNTRY_CODE, span);
See
BaggageField
for baggage usage examples.Customizing propagation keys
BaggagePropagationConfig.SingleBaggageField.remote(BaggageField)
sets the name used as a propagation key (header) to the lowercase variant of the field name. You can override this by supplying different key names. Note: they will be lower-cased.For example, the following will propagate the field "x-vcap-request-id" as-is, but send the fields "countryCode" and "userId" on the wire as "baggage-country-code" and "baggage-user-id" respectively.
import brave.baggage.BaggagePropagationConfig.SingleBaggageField; REQUEST_ID = BaggageField.create("x-vcap-request-id"); COUNTRY_CODE = BaggageField.create("countryCode"); USER_ID = BaggageField.create("userId"); tracingBuilder.propagationFactory( BaggagePropagation.newFactoryBuilder(B3Propagation.FACTORY) .add(SingleBaggageField.remote(REQUEST_ID)) .add(SingleBaggageField.newBuilder(COUNTRY_CODE) .addKeyName("baggage-country-code").build()) .add(SingleBaggageField.newBuilder(USER_ID) .addKeyName("baggage-user-id").build()) .build() );
- Since:
- 5.11
- See Also:
BaggageField
,BaggagePropagationConfig
,BaggagePropagationCustomizer
,CorrelationScopeDecorator
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
BaggagePropagation.FactoryBuilder
-
Nested classes/interfaces inherited from interface brave.propagation.Propagation
Propagation.Getter<R,K>, Propagation.KeyFactory<K>, Propagation.RemoteGetter<R>, Propagation.RemoteSetter<R>, Propagation.Setter<R,K>
-
-
Field Summary
-
Fields inherited from interface brave.propagation.Propagation
B3_SINGLE_STRING, B3_STRING
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static List<String>
allKeyNames(Propagation<String> propagation)
Returns the key names used for propagation, including those used for the trace context and baggage.<R> TraceContext.Extractor<R>
extractor(Propagation.Getter<R,K> getter)
<R> TraceContext.Injector<R>
injector(Propagation.Setter<R,K> setter)
Replaces a propagated field with the given value.List<K>
keys()
Only returns trace context keys.static BaggagePropagation.FactoryBuilder
newFactoryBuilder(Propagation.Factory delegate)
Wraps an underlying propagation implementation, pushing one or more fields.
-
-
-
Method Detail
-
newFactoryBuilder
public static BaggagePropagation.FactoryBuilder newFactoryBuilder(Propagation.Factory delegate)
Wraps an underlying propagation implementation, pushing one or more fields.
-
keys
public List<K> keys()
Only returns trace context keys. Baggage field names are not returned to ensure tools don't delete them. This is to support users accessing baggage without Brave apis (ex via headers).- Specified by:
keys
in interfacePropagation<K>
- See Also:
allKeyNames(Propagation)
-
allKeyNames
public static List<String> allKeyNames(Propagation<String> propagation)
Returns the key names used for propagation, including those used for the trace context and baggage. The result can be cached in the same scope as the propagation instance.This is here for the remote propagation use cases:
- To generate constants for all key names. ex. gRPC Metadata.Key
- To iterate fields when missing a get field by name function. ex. OpenTracing TextMap
- To clear fields on re-usable requests. ex. JMS message
Details
Thepropagation
parameter is required because there may be multiple tracers with different baggage configuration. Also,Propagation
instances can be wrapped, so you cannot useinstanceof
to identify if baggage is internally supported. For example,ExtraFieldPropagation
internally wrapsBaggagePropagation
.This is different than
BaggageField.getAll(TraceContext)
, as propagation keys may be different than baggage field names.- Parameters:
propagation
- used to extract configuration- Returns:
- a list of remote propagation key names used for trace context and baggage.
- Since:
- 5.12
-
injector
public <R> TraceContext.Injector<R> injector(Propagation.Setter<R,K> setter)
Description copied from interface:Propagation
Replaces a propagated field with the given value. Saved as a constant to avoid runtime allocations. For example, a setter for anHttpURLConnection
would be the method referenceURLConnection.addRequestProperty(String, String)
- Specified by:
injector
in interfacePropagation<K>
- Type Parameters:
R
- Usually, but not always, an instance ofRequest
.- Parameters:
setter
- invoked for each propagation key to add.- See Also:
Propagation.RemoteSetter
-
extractor
public <R> TraceContext.Extractor<R> extractor(Propagation.Getter<R,K> getter)
- Specified by:
extractor
in interfacePropagation<K>
- Type Parameters:
R
- Usually, but not always, an instance ofRequest
.- Parameters:
getter
- invoked for each propagation key to get.- See Also:
Propagation.RemoteGetter
-
-