Class TracingContext
java.lang.Object
com.loomcache.server.tracing.TracingContext
OpenTelemetry distributed tracing integration for LoomCache.
Architecture
This class provides a thin abstraction over distributed tracing, designed to: - Work with OpenTelemetry when available (via reflection/classloading) - Fall back to SLF4J MDC + ScopedValues when OTel is not available - Support W3C Trace Context format for cross-node propagation - Provide virtual thread-friendly context via ScopedValuesConfiguration
TracingContext is configured during CacheNode startup with: - Exporter type: NONE (disabled), CONSOLE, OTLP, JAEGER - Service name: "loomcache" (fixed) - Resource attributes: service.name, service.version, node.id - Sampling rate: 0.0-1.0 (default 1.0, always-on)Usage
// Create a root span for incoming request
var span = TracingContext.createSpan("cache.get", "root");
try {
span.setAttribute("key", "myKey");
span.setAttribute("cache.name", "myMap");
// ... operation ...
span.setStatus(Span.Status.OK);
} catch (Exception e) {
span.recordException(e);
span.setStatus(Span.Status.ERROR);
} finally {
span.end();
}
Trace Context Propagation
Trace context is automatically available in MDC and ScopedValues: - traceId: unique identifier for the entire distributed transaction - spanId: unique identifier for this span - parentSpanId: optional reference to parent span - sampled: whether sampling is enabled- Since:
- 1.0
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfacestatic interfacestatic interfaceSpan interface representing a unit of work in a distributed trace. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic TracingContext.SpancreateSpan(String spanName, String kind) Create a new span with the given name and kind.extractTraceContext(@Nullable String traceParentHeader) Extract trace context from W3C Trace Context format.static @Nullable TracingContext.SpanGet the current active span (from ScopedValue).static @Nullable StringGet the current trace ID.Get tracing statistics.static voidinitialize(@Nullable TracingConfig config) Initialize TracingContext with the given configuration.static @Nullable StringinjectTraceContext(@Nullable TracingContext.Span span) Inject trace context into W3C Trace Context format.static booleanCheck if OpenTelemetry is available.static voidsetCurrentSpan(@Nullable TracingContext.Span span) Set the current span (for ScopedValue binding).static voidsetCurrentTraceId(@Nullable String traceId) Set the current trace ID (for ScopedValue binding).static voidshutdown()Shutdown tracing and flush any pending spans.static voidwithSpan(String spanName, String kind, TracingContext.CheckedRunnable block) Execute a block of code within a span context.static <T> TwithSpan(String spanName, String kind, TracingContext.CheckedSupplier<T> block) Execute a block of code within a span context and return a result.
-
Constructor Details
-
TracingContext
public TracingContext()
-
-
Method Details
-
initialize
Initialize TracingContext with the given configuration.- Parameters:
config- configuration for tracing (may be null to disable)
-
createSpan
Create a new span with the given name and kind.- Parameters:
spanName- the span name (e.g., "cache.get", "raft.append_entries") (must not be null)kind- the span kind ("root", "internal", "client", "server") (must not be null)- Returns:
- a new Span
- Throws:
NullPointerException- if spanName or kind is null
-
withSpan
public static void withSpan(String spanName, String kind, TracingContext.CheckedRunnable block) throws Exception Execute a block of code within a span context. Automatically ends the span when the block completes.- Parameters:
spanName- the span name (must not be null)kind- the span kind (must not be null)block- the code to execute (must not be null)- Throws:
Exception- if the block throwsNullPointerException- if spanName, kind, or block is null
-
withSpan
public static <T> T withSpan(String spanName, String kind, TracingContext.CheckedSupplier<T> block) throws Exception Execute a block of code within a span context and return a result. Automatically ends the span when the block completes.- Type Parameters:
T- the type of the result- Parameters:
spanName- the span name (must not be null)kind- the span kind (must not be null)block- the code to execute (must not be null)- Returns:
- the result of the block
- Throws:
Exception- if the block throwsNullPointerException- if spanName, kind, or block is null
-
getCurrentSpan
Get the current active span (from ScopedValue).- Returns:
- the current span, or null if none is active
-
getCurrentTraceId
Get the current trace ID.- Returns:
- the trace ID, or null if none is active
-
setCurrentSpan
Set the current span (for ScopedValue binding). Typically called internally by withSpan(); direct use is rare.- Parameters:
span- the span to set
-
setCurrentTraceId
Set the current trace ID (for ScopedValue binding).- Parameters:
traceId- the trace ID to set
-
extractTraceContext
Extract trace context from W3C Trace Context format. Format: traceparent: "00-{traceId}-{spanId}-{flags}"- Parameters:
traceParentHeader- the traceparent header value (may be null)- Returns:
- a Map with extracted context (traceId, spanId, parentSpanId, flags)
-
injectTraceContext
Inject trace context into W3C Trace Context format.- Parameters:
span- the span to inject (may be null)- Returns:
- the traceparent header value, or null if span is null
-
isOpenTelemetryAvailable
public static boolean isOpenTelemetryAvailable()Check if OpenTelemetry is available.- Returns:
- true if OTel SDK is on the classpath
-
shutdown
public static void shutdown()Shutdown tracing and flush any pending spans. Called during CacheNode shutdown. -
getMetrics
-