@ThreadSafe public interface ContextPropagators
This container can be used to access a single, composite propagator for each supported format,
which will be responsible for injecting and extracting data for each registered concern (traces,
correlations, etc). Propagation will happen through io.grpc.Context
, from which values
will be read upon injection, and which will store values from the extraction step. The resulting
Context
can then be used implicitly or explicitly by the OpenTelemetry API.
Example of usage on the client:
private static final Tracer tracer = OpenTelemetry.getTracer();
void onSendRequest() {
try (Scope scope = tracer.withSpan(span)) {
ContextPropagators propagators = OpenTelemetry.getPropagators();
HttpTextFormat textFormat = propagators.getHttpTextFormat();
// Inject the span's SpanContext and other available concerns (such as correlations)
// contained in the specified Context.
Map<String, String> map = new HashMap<>();
httpTextFormat.inject(Context.current(), map, new Setter<String, String>() {
public void put(Map<String, String> map, String key, String value) {
map.put(key, value);
}
});
// Send the request including the text map and wait for the response.
}
}
Example of usage in the server:
private static final Tracer tracer = OpenTelemetry.getTracer();
void onRequestReceived() {
ContextPropagators propagators = OpenTelemetry.getPropagators();
HttpTextFormat textFormat = propagators.getHttpTextFormat();
// Extract and store the propagated span's SpanContext and other available concerns
// in the specified Context.
Context context = textFormat.extract(Context.current(), request, new Getter<String, String>() {
public String get(Object request, String key) {
// Return the value associated to the key, if available.
}
});
Span span = tracer.spanBuilder("MyRequest")
.setParent(context)
.setSpanKind(Span.Kind.SERVER).startSpan();
try (Scope ss = tracer.withSpan(span)) {
// Handle request and send response back.
} finally {
span.end();
}
}
Modifier and Type | Method and Description |
---|---|
HttpTextFormat |
getHttpTextFormat()
Returns a
HttpTextFormat propagator. |
HttpTextFormat getHttpTextFormat()
HttpTextFormat
propagator.
The returned value will be a composite instance containing all the registered HttpTextFormat
propagators. If none is registered, the returned value will be a no-op
instance.
HttpTextFormat
propagator to inject and extract data.