Package io.opentelemetry.context
Interface ContextKey<T>
public interface ContextKey<T>
Key for indexing values of type
ContextKey
stored in a Context
. ContextKey
are
compared by reference, so it is expected that only one ContextKey
is created for a
particular type of context value.
public class ContextUser {
private static final ContextKey<MyState> KEY = ContextKey.named("MyState");
public Context startWork() {
return Context.withValues(KEY, new MyState());
}
public void continueWork(Context context) {
MyState state = context.get(KEY);
// Keys are compared by reference only.
assert state != Context.current().get(ContextKey.named("MyState"));
...
}
}
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> ContextKey<T>
Returns a newContextKey
with the given debug name.
-
Method Details
-
named
Returns a newContextKey
with the given debug name. The name does not impact behavior and is only for debugging purposes. Multiple different keys with the same name will be separate keys.
-