public abstract class TagContextTextFormat extends Object
TagContext
as text into carriers that travel in-band
across process boundaries. Tags are often encoded as messaging or RPC request headers.
When using http, the carrier of propagated data on both the client (injector) and server (extractor) side is usually an http request. Propagation is usually implemented via library- specific request interceptors, where the client-side injects tags and the server-side extracts them.
Example of usage on the client:
private static final Tagger tagger = Tags.getTagger();
private static final TagContextTextFormat textFormat =
Tags.getPropagationComponent().getCorrelationContextFormat();
private static final TagContextTextFormat.Setter setter =
new TagContextTextFormat.Setter<HttpURLConnection>() {
public void put(HttpURLConnection carrier, String key, String value) {
carrier.setRequestProperty(field, value);
}
};
void makeHttpRequest() {
TagContext tagContext = tagger.emptyBuilder().put(K, V).build();
try (Scope s = tagger.withTagContext(tagContext)) {
HttpURLConnection connection =
(HttpURLConnection) new URL("http://myserver").openConnection();
textFormat.inject(tagContext, connection, httpURLConnectionSetter);
// Send the request, wait for response and maybe set the status if not ok.
}
}
Example of usage on the server:
private static final Tagger tagger = Tags.getTagger();
private static final TagContextTextFormat textFormat =
Tags.getPropagationComponent().getCorrelationContextFormat();
private static final TagContextTextFormat.Getter<HttpRequest> getter = ...;
void onRequestReceived(HttpRequest request) {
TagContext tagContext = textFormat.extract(request, getter);
try (Scope s = tagger.withTagContext(tagContext)) {
// Handle request and send response back.
}
}
Modifier and Type | Class and Description |
---|---|
static class |
TagContextTextFormat.Getter<C>
Class that allows a
TagContextTextFormat to read propagated fields from a carrier. |
static class |
TagContextTextFormat.Setter<C>
Class that allows a
TagContextTextFormat to set propagated fields into a carrier. |
Constructor and Description |
---|
TagContextTextFormat() |
Modifier and Type | Method and Description |
---|---|
abstract <C> TagContext |
extract(C carrier,
TagContextTextFormat.Getter<C> getter)
Extracts the tag context from upstream.
|
abstract List<String> |
fields()
The propagation fields defined.
|
abstract <C> void |
inject(TagContext tagContext,
C carrier,
TagContextTextFormat.Setter<C> setter)
Injects the tag context downstream.
|
public abstract List<String> fields()
inject(TagContext, Object, Setter)
.
For example, if the carrier is a single-use or immutable request object, you don't need to clear fields as they couldn't have been set before. If it is a mutable, retryable object, successive calls should clear these fields first.
public abstract <C> void inject(TagContext tagContext, C carrier, TagContextTextFormat.Setter<C> setter) throws TagContextSerializationException
tagContext
- the tag context.carrier
- holds propagation fields. For example, an outgoing message or http request.setter
- invoked for each propagation key to add or remove.TagContextSerializationException
- if the given tag context cannot be serialized.public abstract <C> TagContext extract(C carrier, TagContextTextFormat.Getter<C> getter) throws TagContextDeserializationException
carrier
- holds propagation fields. For example, an outgoing message or http request.getter
- invoked for each propagation key to get.TagContextDeserializationException
- if the input is invalid