Class TraceContext

java.lang.Object
org.bithon.agent.sdk.tracing.TraceContext

public class TraceContext extends Object
Author:
frank.chen021@outlook.com
Date:
8/5/25 5:47 pm
  • Field Details

    • LOGGER

      private static final Logger LOGGER
    • LOG_INTERVAL_MS

      private static final long LOG_INTERVAL_MS
      See Also:
    • lastLogTime

      private static long lastLogTime
  • Constructor Details

    • TraceContext

      public TraceContext()
  • Method Details

    • shouldLog

      private static boolean shouldLog()
    • currentTraceId

      public static String currentTraceId()
      Returns the trace ID of the current trace context, or null if there is no active trace.
    • currentSpanId

      public static String currentSpanId()
      Returns the span ID of the current span, or null if there is no active span.
    • currentSpan

      public static ISpanScope currentSpan()
      Returns the current span scope, or a no-op span scope if there is no active span. Generally, you should not call the 'close' method after using the returned span scope.
      Since:
      1.2.3
    • newScopedSpan

      @Deprecated public static ISpan newScopedSpan()
      Deprecated.
      Use newScopedSpan(String) instead. This method will be removed in a future version.
    • newScopedSpan

      public static SpanScopeBuilder newScopedSpan(String operationName)
      Creates a new scoped span builder for the given operation. Use the builder to configure the span and then call attach() to start the span.

      Examples:

      
       // Simple scoped span
       try (ISpanScope span = TraceContext.newScopedSpan("operation").attach()) {
           // do your work here
       }
      
       // Scoped span with custom kind
       try (ISpanScope span = TraceContext.newScopedSpan("operation")
               .kind(SpanKind.CLIENT)
               .attach()) {
           // do your work here
       }
       
      Parameters:
      operationName - the name of the operation.
      Returns:
      a SpanScopeBuilder for configuring and creating the span
      Throws:
      IllegalArgumentException - if the given operation name is null
      Since:
      1.2.2
    • newTrace

      public static TraceScopeBuilder newTrace(String operationName)
      Creates a new trace scope builder for the given operation. Use the builder to configure the trace and then call attach() to start tracing.

      Examples:

      
       // Simple root trace
       try (ITraceScope scope = TraceContext.newTrace("operation").attach()) {
           // do your work here
       }
      
       // Root trace with custom tracing mode
       try (ITraceScope scope = TraceContext.newTrace("operation")
               .withTracingMode(TracingMode.LOGGING)
               .attach()) {
           // do your work here
       }
      
       // Cross-thread continuation
       String traceId = TraceContext.currentTraceId();
       String parentSpanId = TraceContext.currentSpanId();
       ITraceScope asyncTrace = TraceContext.newTrace("background-work")
           .withParent(traceId, parentSpanId)
           .attach();
       // pass asyncTrace to another thread...
       
      Parameters:
      operationName - the name of the operation.
      Returns:
      a TraceScopeBuilder for configuring and creating the trace
      Throws:
      IllegalArgumentException - if the given operation name is null
      Since:
      1.2.2