Class TraceContext

java.lang.Object
com.tnsai.quality.tracing.TraceContext

public final class TraceContext extends Object
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 Classes
    Modifier and Type
    Class
    Description
    static final class 
    AutoCloseable scope for try-with-resources pattern.
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    Clears all thread-local state.
    static String
    Returns the current parent observation id (top of stack), or null if empty.
    static AgentTrace
    Returns the active trace for the current thread, or null.
    static int
    Returns the nesting depth (number of pushed observations).
    static String
    pop()
    Pops the current observation id from the context stack.
    static void
    push(String observationId)
    Pushes an observation id onto the context stack, making it the current parent.
    scoped(String observationId)
    Creates an AutoCloseable scope that pushes the observation id on creation and pops it on close.
    static void
    Sets the active trace for the current thread.

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • setTrace

      public static void setTrace(AgentTrace trace)
      Sets the active trace for the current thread.
    • currentTrace

      public static AgentTrace currentTrace()
      Returns the active trace for the current thread, or null.
    • push

      public static void push(String observationId)
      Pushes an observation id onto the context stack, making it the current parent.
    • pop

      public static String pop()
      Pops the current observation id from the context stack.
      Returns:
      the popped observation id, or null if the stack was empty
    • currentObservationId

      public static String 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

      public static TraceContext.Scope scoped(String observationId)
      Creates an AutoCloseable scope that pushes the observation id on creation and pops it on close. For use with try-with-resources.