Class TraceContextPropagator

java.lang.Object
com.loomcache.server.tracing.TraceContextPropagator

public class TraceContextPropagator extends Object
Propagates trace context across distributed cache nodes.

Format

Uses W3C Trace Context format (https://www.w3.org/TR/trace-context/): - traceparent header: "version-traceId-spanId-flags" - version: protocol version (00) - traceId: 32 hex characters (128 bits) - spanId: 16 hex characters (64 bits) - flags: sampled flag (01 = sampled, 00 = not sampled)

Implementation

Since Message.java uses a fixed binary protocol, trace context is injected into the message metadata using a special message header or encoded in the map name as a prefix. For maximum compatibility with existing protocol, trace context is stored in MDC during message handling.

Usage

// On sender side (inject trace context)
Message msg = Message.request(MessageType.MAP_GET)
    .withMapName("myMap")
    .withKey("key1".getBytes());
TraceContextPropagator.injectTraceContext(msg);

// On receiver side (extract trace context)
TraceContextPropagator.extractTraceContext(incomingMessage);
// ... handle message with trace context available in MDC ...
Since:
1.0
  • Constructor Details

    • TraceContextPropagator

      public TraceContextPropagator()
  • Method Details

    • injectTraceContext

      public static void injectTraceContext(Message message)
      Inject trace context into a message for transmission to another node. The trace context is extracted from the current MDC or active span.
      Parameters:
      message - the message to inject trace context into
    • extractTraceContext

      public static Map<String,String> extractTraceContext(Message message)
      Extract trace context from an incoming message. Automatically restores trace context to MDC for logging.
      Parameters:
      message - the message to extract trace context from
      Returns:
      a Map with extracted context (traceId, spanId, parentSpanId, flags)
    • extractFromTraceparent

      public static Map<String,String> extractFromTraceparent(@Nullable String traceparent)
      Extract trace context from W3C traceparent header format.
      Parameters:
      traceparent - the traceparent header value (format: "00-{traceId}-{spanId}-{flags}")
      Returns:
      a Map with extracted context
    • injectIntoHeaders

      public static void injectIntoHeaders(Map<String,String> headers)
      Inject trace context into a headers map (for future use with custom message headers).
      Parameters:
      headers - the headers map to inject into
    • extractFromHeaders

      public static Map<String,String> extractFromHeaders(Map<String,String> headers)
      Extract trace context from a headers map.
      Parameters:
      headers - the headers map to extract from
      Returns:
      a Map with extracted context
    • createChildSpan

      public static TracingContext.Span createChildSpan(String spanName, Map<String,String> extractedContext)
      Create a child span from extracted trace context. Used when processing an incoming message to link back to the original request.
      Parameters:
      spanName - the name of the child span
      extractedContext - the extracted trace context (from extractTraceContext)
      Returns:
      a new child span with the parent context linked
    • getCurrentTraceId

      public static @Nullable String getCurrentTraceId()
      Get the current trace ID from MDC (for logging integration).
      Returns:
      the current trace ID, or null if none is active
    • createBaggage

      public static String createBaggage(Map<String,String> items)
      Create a baggage map for propagating request metadata. Baggage includes context like user ID, request priority, etc.
      Parameters:
      items - key-value pairs to include in baggage
      Returns:
      a formatted baggage string
    • parseBaggage

      public static Map<String,String> parseBaggage(String baggageString)
      Parse a baggage string into key-value pairs.
      Parameters:
      baggageString - the baggage string
      Returns:
      a Map of baggage items
    • extractFromMessageBody

      public static Map<String,String> extractFromMessageBody(Message message)
      Extract trace context from a message body (if stored there). This is a fallback for cases where metadata is not available.
    • hasTraceContext

      public static boolean hasTraceContext(Message message)
      Check if a message contains trace context.
      Parameters:
      message - the message to check
      Returns:
      true if the message has trace context