Package com.tnsai.quality.tracing
Class TraceContext
java.lang.Object
com.tnsai.quality.tracing.TraceContext
Thread-local context for automatic parent-child nesting of observations.
When entering an observation scope, push the observation id; children created within that scope automatically receive the pushed id as their parent.
Usage:
TraceContext.setTrace(trace);
TraceContext.push(spanId);
try {
// observations created here will have spanId as parent
} finally {
TraceContext.pop();
}
Or use the convenience scoped(String) method for try-with-resources:
try (var scope = TraceContext.scoped(spanId)) {
// observations created here will have spanId as parent
}
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classAutoCloseable scope for try-with-resources pattern. -
Method Summary
Modifier and TypeMethodDescriptionstatic voidclear()Clears all thread-local state.static StringReturns the current parent observation id (top of stack), or null if empty.static AgentTraceReturns the active trace for the current thread, or null.static intdepth()Returns the nesting depth (number of pushed observations).static Stringpop()Pops the current observation id from the context stack.static voidPushes an observation id onto the context stack, making it the current parent.static TraceContext.ScopeCreates anAutoCloseablescope that pushes the observation id on creation and pops it on close.static voidsetTrace(AgentTrace trace) Sets the active trace for the current thread.
-
Method Details
-
setTrace
Sets the active trace for the current thread. -
currentTrace
Returns the active trace for the current thread, or null. -
push
Pushes an observation id onto the context stack, making it the current parent. -
pop
Pops the current observation id from the context stack.- Returns:
- the popped observation id, or null if the stack was empty
-
currentObservationId
Returns the current parent observation id (top of stack), or null if empty. -
depth
public static int depth()Returns the nesting depth (number of pushed observations). -
clear
public static void clear()Clears all thread-local state. Call this at the end of a request or thread lifecycle. -
scoped
Creates anAutoCloseablescope that pushes the observation id on creation and pops it on close. For use with try-with-resources.
-