Class TraceContextPropagator
java.lang.Object
com.loomcache.server.tracing.TraceContextPropagator
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic StringcreateBaggage(Map<String, String> items) Create a baggage map for propagating request metadata.static TracingContext.SpancreateChildSpan(String spanName, Map<String, String> extractedContext) Create a child span from extracted trace context.extractFromHeaders(Map<String, String> headers) Extract trace context from a headers map.extractFromMessageBody(Message message) Extract trace context from a message body (if stored there).extractFromTraceparent(@Nullable String traceparent) Extract trace context from W3C traceparent header format.extractTraceContext(Message message) Extract trace context from an incoming message.static @Nullable StringGet the current trace ID from MDC (for logging integration).static booleanhasTraceContext(Message message) Check if a message contains trace context.static voidinjectIntoHeaders(Map<String, String> headers) Inject trace context into a headers map (for future use with custom message headers).static voidinjectTraceContext(Message message) Inject trace context into a message for transmission to another node.parseBaggage(String baggageString) Parse a baggage string into key-value pairs.
-
Constructor Details
-
TraceContextPropagator
public TraceContextPropagator()
-
-
Method Details
-
injectTraceContext
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
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
-
injectIntoHeaders
-
extractFromHeaders
-
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 spanextractedContext- the extracted trace context (from extractTraceContext)- Returns:
- a new child span with the parent context linked
-
getCurrentTraceId
Get the current trace ID from MDC (for logging integration).- Returns:
- the current trace ID, or null if none is active
-
createBaggage
-
parseBaggage
-
extractFromMessageBody
-
hasTraceContext
Check if a message contains trace context.- Parameters:
message- the message to check- Returns:
- true if the message has trace context
-