Class ExtraFieldPropagation<K>
- All Implemented Interfaces:
Propagation<K>
public final class ExtraFieldPropagation<K> extends Object implements Propagation<K>
For example, if you are in a Cloud Foundry environment, you might want to pass the request ID:
// when you initialize the builder, define the extra field you want to propagate
tracingBuilder.propagationFactory(
ExtraFieldPropagation.newFactory(B3Propagation.FACTORY, "x-vcap-request-id")
);
// later, you can tag that request ID or use it in log correlation
requestId = ExtraFieldPropagation.get("x-vcap-request-id");
// You can also set or override the value similarly, which might be needed if a new request
ExtraFieldPropagation.get("x-country-code", "FO");
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!
Passing through alternate trace contexts
You may also need to propagate an second trace context transparently. For example, when in an Amazon Web Services environment, but not reporting data to X-Ray. To ensure X-Ray can co-exist correctly, pass-through its tracing header like so.
tracingBuilder.propagationFactory(
ExtraFieldPropagation.newFactory(B3Propagation.FACTORY, "x-amzn-trace-id")
);
Prefixed fields
You can also prefix fields, if they follow a common pattern. For example, the following will propagate the field "x-vcap-request-id" as-is, but send the fields "country-code" and "user-id" on the wire as "baggage-country-code" and "baggage-user-id" respectively.
// Setup your tracing instance with allowed fields
tracingBuilder.propagationFactory(
ExtraFieldPropagation.newFactoryBuilder(B3Propagation.FACTORY)
.addField("x-vcap-request-id")
.addPrefixedFields("baggage-", Arrays.asList("country-code", "user-id"))
.build()
);
// Later, you can call below to affect the country code of the current trace context
ExtraFieldPropagation.set("country-code", "FO");
String countryCode = ExtraFieldPropagation.get("country-code");
// Or, if you have a reference to a trace context, use it explicitly
ExtraFieldPropagation.set(span.context(), "country-code", "FO");
String countryCode = ExtraFieldPropagation.get(span.context(), "country-code");
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
ExtraFieldPropagation.Factory
static class
ExtraFieldPropagation.FactoryBuilder
Nested classes/interfaces inherited from interface brave.propagation.Propagation
Propagation.Getter<C,K>, Propagation.KeyFactory<K>, Propagation.Setter<C,K>
-
Field Summary
-
Method Summary
Modifier and Type Method Description static String
current(String name)
Synonym forget(String)
<C> TraceContext.Extractor<C>
extractor(Propagation.Getter<C,K> getter)
List<K>
extraKeys()
Returns the extra keys this component can extract.static String
get(TraceContext context, String name)
Returns the value of the field with the specified key or null if not availablestatic String
get(String name)
Returns the value of the field with the specified key or null if not available.static Map<String,String>
getAll()
Returns a mapping of fields in the current trace context, or empty if there are none.static Map<String,String>
getAll(TraceContext context)
Returns a mapping of any fields in the trace context.static Map<String,String>
getAll(TraceContextOrSamplingFlags extracted)
Returns a mapping of any fields in the extraction result.<C> TraceContext.Injector<C>
injector(Propagation.Setter<C,K> setter)
Replaces a propagated field with the given value.List<K>
keys()
Only returns trace context keys.static ExtraFieldPropagation.Factory
newFactory(Propagation.Factory delegate, String... fieldNames)
Wraps an underlying propagation implementation, pushing one or more fieldsstatic ExtraFieldPropagation.Factory
newFactory(Propagation.Factory delegate, Collection<String> fieldNames)
Wraps an underlying propagation implementation, pushing one or more fieldsstatic ExtraFieldPropagation.FactoryBuilder
newFactoryBuilder(Propagation.Factory delegate)
static void
set(TraceContext context, String name, String value)
Sets the value of the field with the specified key, or drops if not a configured fieldstatic void
set(String name, String value)
Sets the current value of the field with the specified key, or drops if not a configured field.
-
Method Details
-
newFactory
public static ExtraFieldPropagation.Factory newFactory(Propagation.Factory delegate, String... fieldNames)Wraps an underlying propagation implementation, pushing one or more fields -
newFactory
public static ExtraFieldPropagation.Factory newFactory(Propagation.Factory delegate, Collection<String> fieldNames)Wraps an underlying propagation implementation, pushing one or more fields -
newFactoryBuilder
-
current
Synonym forget(String)
-
get
Returns the value of the field with the specified key or null if not available.Prefer
get(TraceContext, String)
if you have a reference to a span. -
set
Sets the current value of the field with the specified key, or drops if not a configured field.Prefer
set(TraceContext, String, String)
if you have a reference to a span. -
getAll
Returns a mapping of fields in the current trace context, or empty if there are none.Prefer
set(TraceContext, String, String)
if you have a reference to a span. -
getAll
Returns a mapping of any fields in the extraction result. -
getAll
Returns a mapping of any fields in the trace context. -
get
Returns the value of the field with the specified key or null if not available -
set
Sets the value of the field with the specified key, or drops if not a configured field -
extraKeys
Returns the extra keys this component can extract. This result is lowercase and does not include anytrace context keys
. -
keys
Only returns trace context keys. Extra field names are not returned to ensure tools don't delete them. This is to support users accessing extra fields without Brave apis (ex via headers).- Specified by:
keys
in interfacePropagation<K>
-
injector
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>
- Parameters:
setter
- invoked for each propagation key to add.
-
extractor
- Specified by:
extractor
in interfacePropagation<K>
- Parameters:
getter
- invoked for each propagation key to get.
-