Class TraceProvider

java.lang.Object
ai.acolite.agentsdk.core.tracing.TraceProvider

public class TraceProvider extends Object
TraceProvider

Global singleton for trace/span creation and processor management.

Features: - Environment variable support: OPENAI_AGENTS_DISABLE_TRACING=1 or true - Returns NoopTrace/NoopSpan when disabled (no performance overhead) - Manages multiple processors via MultiTracingProcessor - Automatic shutdown on JVM exit

Usage:

 // Get global instance
 TraceProvider provider = TraceProvider.getGlobalTraceProvider();

 // Register processors
 provider.registerProcessor(new ConsoleTraceProcessor());

 // Create traces
 Trace trace = provider.createTrace(Trace.builder().name("My workflow").build());
 
Environment Variables: - OPENAI_AGENTS_DISABLE_TRACING=1 or true - Disables all tracing

Ported from TypeScript OpenAI Agents SDK Source: tracing/provider.ts

  • Constructor Details

    • TraceProvider

      public TraceProvider()
      Create a new TraceProvider. Checks OPENAI_AGENTS_DISABLE_TRACING environment variable.
  • Method Details

    • registerProcessor

      public void registerProcessor(TraceProcessor processor)
      Add a processor to the list of processors. Each processor will receive all traces/spans.
    • setProcessors

      public void setProcessors(List<TraceProcessor> processors)
      Set the list of processors. This will replace any existing processors.
    • getCurrentTrace

      public Optional<Trace> getCurrentTrace()
      Get the current trace from ThreadLocal context.
    • getCurrentSpan

      public Optional<Span<?>> getCurrentSpan()
      Get the current span from ThreadLocal context.
    • setDisabled

      public void setDisabled(boolean disabled)
      Enable or disable tracing at runtime.
    • isDisabled

      public boolean isDisabled()
      Check if tracing is currently disabled.
    • createTrace

      public Trace createTrace(Trace trace)
      Create a new trace. Returns NoopTrace if tracing is disabled.
    • createSpan

      public <TData extends SpanData> Span<TData> createSpan(Span<TData> span)
      Create a new span. Returns NoopSpan if tracing is disabled or no active trace.
    • shutdown

      public CompletableFuture<Void> shutdown(long timeoutMs)
      Shutdown all processors. Waits up to timeout for export to complete.
    • shutdown

      public CompletableFuture<Void> shutdown()
      Shutdown with default timeout (5 seconds).
    • forceFlush

      public CompletableFuture<Void> forceFlush()
      Force flush all pending traces/spans immediately.
    • getGlobalTraceProvider

      public static TraceProvider getGlobalTraceProvider()
      Get the global TraceProvider singleton. Thread-safe lazy initialization.
    • initializeWithDefaultCloudTracing

      public static void initializeWithDefaultCloudTracing()
      Initialize the global provider with default cloud tracing setup. Sets up BatchTraceProcessor + OpenAITraceExporter.

      This should be called once at application startup if you want automatic cloud tracing with OpenAI.