OpenTelemetry Context propagation mechanism for carrying scoped values across API boundaries and between threads in Java applications
npx @tessl/cli install tessl/maven-io-opentelemetry--opentelemetry-context@1.51.0OpenTelemetry Context provides a context propagation mechanism for carrying scoped values across API boundaries and between threads in Java applications. It enables thread-local storage and propagation of contextual information such as tracing data, baggage, and other cross-cutting concerns in distributed systems.
pom.xml or build.gradleMaven:
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-context</artifactId>
<version>1.51.0</version>
</dependency>Gradle:
implementation 'io.opentelemetry:opentelemetry-context:1.51.0'import io.opentelemetry.context.Context;
import io.opentelemetry.context.ContextKey;
import io.opentelemetry.context.Scope;For context propagation:
import io.opentelemetry.context.propagation.ContextPropagators;
import io.opentelemetry.context.propagation.TextMapPropagator;
import io.opentelemetry.context.propagation.TextMapGetter;
import io.opentelemetry.context.propagation.TextMapSetter;import io.opentelemetry.context.Context;
import io.opentelemetry.context.ContextKey;
import io.opentelemetry.context.Scope;
// Create a context key
private static final ContextKey<String> USER_ID_KEY = ContextKey.named("userId");
// Store and retrieve values
Context context = Context.current().with(USER_ID_KEY, "user123");
try (Scope scope = context.makeCurrent()) {
// Context is available to all code within this scope
String userId = Context.current().get(USER_ID_KEY);
// Context automatically propagates to wrapped operations
Runnable task = Context.current().wrap(() -> {
// This code runs with the current context
String contextUserId = Context.current().get(USER_ID_KEY);
System.out.println("User ID: " + contextUserId);
});
task.run();
}
// Previous context is automatically restoredThe OpenTelemetry Context API is built around several key concepts:
Fundamental context creation, storage, and retrieval operations for managing scoped values within applications.
// Context static methods
static Context current();
static Context root();
// Instance methods
<V> V get(ContextKey<V> key);
<V> Context with(ContextKey<V> key, V value);
Context with(ImplicitContextKeyed value);
Scope makeCurrent();Type-safe key creation and scope management for maintaining context boundaries and automatic cleanup.
// ContextKey creation
static <T> ContextKey<T> named(String name);
// Scope management
interface Scope extends AutoCloseable {
static Scope noop();
void close();
}Context-aware wrappers for Java executor services that automatically propagate context across asynchronous operations.
// Static executor wrapping (uses current context)
static Executor taskWrapping(Executor executor);
static ExecutorService taskWrapping(ExecutorService executorService);
static ScheduledExecutorService taskWrapping(ScheduledExecutorService executorService);
// Instance executor wrapping (uses specific context)
Executor wrap(Executor executor);
ExecutorService wrap(ExecutorService executor);
ScheduledExecutorService wrap(ScheduledExecutorService executor);Context propagation utilities for wrapping various Java functional interfaces including runnables, callables, and lambda expressions.
// Runnable and Callable wrapping
Runnable wrap(Runnable runnable);
<T> Callable<T> wrap(Callable<T> callable);
// Functional interface wrapping
<T, U> Function<T, U> wrapFunction(Function<T, U> function);
<T, U, V> BiFunction<T, U, V> wrapFunction(BiFunction<T, U, V> function);
<T> Consumer<T> wrapConsumer(Consumer<T> consumer);
<T, U> BiConsumer<T, U> wrapConsumer(BiConsumer<T, U> consumer);
<T> Supplier<T> wrapSupplier(Supplier<T> supplier);Cross-process context propagation via configurable text map propagators for distributed tracing and baggage transmission.
// Context Propagators
static ContextPropagators create(TextMapPropagator textPropagator);
static ContextPropagators noop();
TextMapPropagator getTextMapPropagator();
// Text Map Propagation
static TextMapPropagator composite(TextMapPropagator... propagators);
static TextMapPropagator noop();
Collection<String> fields();
<C> void inject(Context context, C carrier, TextMapSetter<C> setter);
<C> Context extract(Context context, C carrier, TextMapGetter<C> getter);Pluggable context storage implementations with wrapper support for custom context management strategies and debugging.
// Context Storage
static ContextStorage get();
static ContextStorage defaultStorage();
static void addWrapper(Function<? super ContextStorage, ? extends ContextStorage> wrapper);
// Storage operations
Scope attach(Context toAttach);
Context current();
Context root();Interface for values that can be automatically stored in context without exposing explicit context keys.
interface ImplicitContextKeyed {
Scope makeCurrent();
Context storeInContext(Context context);
}// Core interfaces
interface Context {
// Methods documented in capabilities above
}
interface ContextKey<T> {
// Marker interface for type-safe context keys
}
interface Scope extends AutoCloseable {
void close();
}
interface ContextStorage {
Scope attach(Context toAttach);
Context current();
Context root();
}
interface ImplicitContextKeyed {
Scope makeCurrent();
Context storeInContext(Context context);
}
// Propagation interfaces
interface ContextPropagators {
TextMapPropagator getTextMapPropagator();
}
interface TextMapPropagator {
Collection<String> fields();
<C> void inject(Context context, C carrier, TextMapSetter<C> setter);
<C> Context extract(Context context, C carrier, TextMapGetter<C> getter);
}
interface TextMapGetter<C> {
Iterable<String> keys(C carrier);
String get(C carrier, String key);
default Iterator<String> getAll(@Nullable C carrier, String key);
}
interface TextMapSetter<C> {
void set(C carrier, String key, String value);
}
// Storage Provider
interface ContextStorageProvider {
ContextStorage get();
}