CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-io-opentelemetry--opentelemetry-context

OpenTelemetry Context propagation mechanism for carrying scoped values across API boundaries and between threads in Java applications

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

OpenTelemetry Context

OpenTelemetry 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.

Package Information

  • Package Name: io.opentelemetry:opentelemetry-context
  • Package Type: Maven
  • Language: Java
  • Installation: Add dependency to your pom.xml or build.gradle

Maven:

<dependency>
    <groupId>io.opentelemetry</groupId>
    <artifactId>opentelemetry-context</artifactId>
    <version>1.51.0</version>
</dependency>

Gradle:

implementation 'io.opentelemetry:opentelemetry-context:1.51.0'

Core Imports

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;

Basic Usage

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 restored

Architecture

The OpenTelemetry Context API is built around several key concepts:

  • Context: Immutable containers that store key-value pairs and form a chain of parent-child relationships
  • ContextKey: Type-safe keys for indexing values within contexts, compared by reference equality
  • Scope: AutoCloseable resources that manage context lifecycle with automatic cleanup
  • ContextStorage: Pluggable storage implementations for managing current context (default uses ThreadLocal)
  • Context Propagation: Cross-process context transmission via text map formats

Capabilities

Core Context Management

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();

Core Context Management

Context Keys and Scoping

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 Keys and Scoping

Executor Integration

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);

Executor Integration

Function Wrapping

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);

Function Wrapping

Context Propagation

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);

Context Propagation

Storage Customization

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();

Storage Customization

Implicit Context Values

Interface for values that can be automatically stored in context without exposing explicit context keys.

interface ImplicitContextKeyed {
    Scope makeCurrent();
    Context storeInContext(Context context);
}

Implicit Context Values

Types

// 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();
}
Workspace
tessl
Visibility
Public
Created
Last updated
Describes
mavenpkg:maven/io.opentelemetry/opentelemetry-context@1.51.x
Publish Source
CLI
Badge
tessl/maven-io-opentelemetry--opentelemetry-context badge