@ThreadSafe public final class DefaultCorrelationContextManager extends Object implements CorrelationContextManager
CorrelationContextManager.| Constructor and Description |
|---|
DefaultCorrelationContextManager() |
| 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. |
static CorrelationContextManager |
getInstance()
Returns a
CorrelationContextManager singleton that is the default implementation for
CorrelationContextManager. |
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. |
public static CorrelationContextManager getInstance()
CorrelationContextManager singleton that is the default implementation for
CorrelationContextManager.CorrelationContextManager singleton that is the default implementation for
CorrelationContextManager.public CorrelationContext getCurrentContext()
CorrelationContextManagerCorrelationContext.getCurrentContext in interface CorrelationContextManagerCorrelationContext.public CorrelationContext.Builder contextBuilder()
CorrelationContextManagerBuilder.contextBuilder in interface CorrelationContextManagerBuilder.public io.opentelemetry.context.Scope withContext(CorrelationContext distContext)
CorrelationContextManagerCorrelationContext 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.withContext in interface CorrelationContextManagerdistContext - the CorrelationContext to be set as the current context.CorrelationContext is set as the
current context.public io.opentelemetry.context.propagation.HttpTextFormat<CorrelationContext> getHttpTextFormat()
CorrelationContextManagerHttpTextFormat 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.
}
}
getHttpTextFormat in interface CorrelationContextManagerHttpTextFormat for this implementation.