Package com.tnsai.quality.observability
Class TraceContext
java.lang.Object
com.tnsai.quality.observability.TraceContext
Distributed tracing context for tracking operations across agents.
TraceContext follows the W3C Trace Context specification principles, providing correlation IDs that can be propagated across agent boundaries.
Key concepts:
- Trace ID - Unique identifier for the entire trace (request flow)
- Span ID - Unique identifier for a single operation within the trace
- Parent Span ID - Links child spans to their parent
- Baggage - Key-value pairs propagated along the trace
Example usage:
// Create root trace (at entry point)
TraceContext trace = TraceContext.newTrace();
// Create child span for nested operation
TraceContext childSpan = trace.createChildSpan("processAction");
// Add baggage (propagated to all child spans)
trace.setBaggage("userId", "user-123");
// Use with structured logging
logger.info("Processing request", trace.toLogContext());
// Record timing
childSpan.recordDuration();
Thread-local context for automatic propagation:
TraceContext.setCurrent(trace); // ... nested calls can access via TraceContext.current() TraceContext current = TraceContext.current();
- See Also:
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptionstatic voidClears the current trace context.createChildSpan(String operationName) Creates a child span for a nested operation.static TraceContextcurrent()Gets the current trace context for this thread.voidend()Records the end time and calculates duration.static TraceContextfromTraceParent(String traceparent, String operationName) Parses a W3C traceparent header.static TraceContextfromUpstream(String traceId, String parentSpanId, String operationName) Creates a trace context from an existing trace ID (for distributed tracing).Gets all baggage items.getAttribute(String key) Gets an attribute value.Gets all attributes.getBaggage(String key) Gets a baggage item.longGets the duration in milliseconds.booleanisRoot()Checks if this is a root span (no parent).static TraceContextnewTrace()Creates a new root trace context.static TraceContextCreates a new root trace context with an operation name.voidExecutes a runnable with this context as current.voidsetAttribute(String key, Object value) Sets an attribute on this span (not propagated).voidsetBaggage(String key, String value) Sets a baggage item (propagated to child spans).static voidsetCurrent(TraceContext context) Sets the current trace context for this thread.voidMarks the span as failed with an error message.voidMarks the span as failed with an exception.voidMarks the span as successful.Creates a map suitable for structured logging (MDC).toString()Creates a W3C traceparent header value.
-
Method Details
-
newTrace
Creates a new root trace context.- Returns:
- a new trace context with generated IDs
-
newTrace
Creates a new root trace context with an operation name.- Parameters:
operationName- the operation name- Returns:
- a new trace context
-
fromUpstream
Creates a trace context from an existing trace ID (for distributed tracing).- Parameters:
traceId- the trace ID from upstream serviceparentSpanId- the parent span IDoperationName- the operation name- Returns:
- a new span in the existing trace
-
current
Gets the current trace context for this thread.- Returns:
- the current trace context, or null if none set
-
setCurrent
Sets the current trace context for this thread.- Parameters:
context- the trace context to set
-
clearCurrent
public static void clearCurrent()Clears the current trace context. -
run
Executes a runnable with this context as current.- Parameters:
runnable- the code to execute
-
createChildSpan
Creates a child span for a nested operation.- Parameters:
operationName- the child operation name- Returns:
- a new child span
-
end
public void end()Records the end time and calculates duration. -
setSuccess
public void setSuccess()Marks the span as successful. -
setError
Marks the span as failed with an error message.- Parameters:
message- the error message
-
setError
Marks the span as failed with an exception.- Parameters:
exception- the exception that caused the failure
-
setBaggage
Sets a baggage item (propagated to child spans).- Parameters:
key- the baggage keyvalue- the baggage value
-
getBaggage
Gets a baggage item.- Parameters:
key- the baggage key- Returns:
- the value, or null if not set
-
getAllBaggage
Gets all baggage items.- Returns:
- unmodifiable map of baggage
-
setAttribute
Sets an attribute on this span (not propagated).- Parameters:
key- the attribute keyvalue- the attribute value
-
getAttribute
Gets an attribute value.- Parameters:
key- the attribute key- Returns:
- the value, or null if not set
-
getAttributes
Gets all attributes.- Returns:
- unmodifiable map of attributes
-
getTraceId
-
getSpanId
-
getParentSpanId
-
getOperationName
-
getStartTime
-
getEndTime
-
getStatus
-
getStatusMessage
-
getDurationMs
public long getDurationMs()Gets the duration in milliseconds.- Returns:
- duration in ms, or -1 if not ended
-
isRoot
public boolean isRoot()Checks if this is a root span (no parent).- Returns:
- true if root span
-
toLogContext
Creates a map suitable for structured logging (MDC).- Returns:
- map with trace context fields
-
toTraceParent
Creates a W3C traceparent header value.- Returns:
- traceparent header value
-
fromTraceParent
Parses a W3C traceparent header.- Parameters:
traceparent- the header valueoperationName- the operation name for the new span- Returns:
- a new trace context, or null if parsing fails
-
toString
-