All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.opentelemetry.context.ContextKey Maven / Gradle / Ivy

There is a newer version: 2.0.0.0-RC3
Show newest version
/*
 * 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); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy