@ThreadSafe public interface CorrelationContextManager
CorrelationContexts and CorrelationContexts based on the
current context.
This class returns builders that can be used to create the
implementation-dependent CorrelationContexts.
Implementations may have different constraints and are free to convert entry contexts to their
own subtypes. This means callers cannot assume the current context
is the same instance as the one placed into scope.
| Modifier and Type | Method and Description |
|---|---|
CorrelationContext.Builder |
contextBuilder()
Returns a new
Builder. |
CorrelationContext |
getCurrentContext()
Returns the current
CorrelationContext. |
io.opentelemetry.context.propagation.HttpTextFormat<CorrelationContext> |
getHttpTextFormat()
Returns the
HttpTextFormat for this implementation. |
io.opentelemetry.context.Scope |
withContext(CorrelationContext distContext)
Enters the scope of code where the given
CorrelationContext is in the current context
(replacing the previous CorrelationContext) and returns an object that represents that
scope. |
CorrelationContext getCurrentContext()
CorrelationContext.CorrelationContext.CorrelationContext.Builder contextBuilder()
Builder.Builder.io.opentelemetry.context.Scope withContext(CorrelationContext distContext)
CorrelationContext is in the current context
(replacing the previous CorrelationContext) and returns an object that represents that
scope. The scope is exited when the returned object is closed.distContext - the CorrelationContext to be set as the current context.CorrelationContext is set as the
current context.io.opentelemetry.context.propagation.HttpTextFormat<CorrelationContext> getHttpTextFormat()
HttpTextFormat for this implementation.
Usually this will be the W3C Correlation Context as the HTTP text format. For more details, see correlation-context.
Example of usage on the client:
private static final CorrelationContextManager contextManager =
OpenTelemetry.getCorrelationContextManager();
private static final HttpTextFormat textFormat = contextManager.getHttpTextFormat();
private static final HttpTextFormat.Setter setter =
new HttpTextFormat.Setter<HttpURLConnection>() {
public void put(HttpURLConnection carrier, String key, String value) {
carrier.setRequestProperty(field, value);
}
};
void makeHttpRequest() {
HttpURLConnection connection =
(HttpURLConnection) new URL("http://myserver").openConnection();
textFormat.inject(contextManager.getCurrentContext(), 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 CorrelationContextManager contextManager =
OpenTelemetry.getCorrelationContextManager();
private static final HttpTextFormat textFormat = contextManager.getHttpTextFormat();
private static final HttpTextFormat.Getter<HttpRequest> getter = ...;
void onRequestReceived(HttpRequest request) {
CorrelationContext distContext = textFormat.extract(request, getter);
try (Scope s = contextManager.withContext(distContext)) {
// Handle request and send response back.
}
}
HttpTextFormat for this implementation.