io.opentelemetry.context.ContextKey Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of opentelemetry-context Show documentation
Show all versions of opentelemetry-context Show documentation
OpenTelemetry Context (Incubator)
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.context;
/**
* Key for indexing values of type {@link T} stored in a {@link Context}. {@link ContextKey} are
* compared by reference, so it is expected that only one {@link ContextKey} is created for a
* particular type of context value.
*
* {@code
* public class ContextUser {
*
* private static final ContextKey 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"));
* ...
* }
* }
*
* }
*/
// ErrorProne false positive, this is used for its type constraint, not only as a bag of statics.
@SuppressWarnings("InterfaceWithOnlyStatics")
public interface ContextKey {
/**
* Returns a new {@link ContextKey} 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.
*/
static ContextKey named(String name) {
return new DefaultContextKey<>(name);
}
}