Overview
Thetracer is a singleton instance that manages the lifecycle of spans and traces in your application. It handles span creation, context propagation, buffering, and flushing to the ZeroEval backend.
You typically don’t need to use the tracer directly—the @span decorator and helper functions provide a higher-level API. However, the tracer is useful for advanced use cases like manual span management.
Methods
startSpan()
Start a new span. If called within an existing span context, the new span will automatically become a child of the current span.Parameters
The name of the span (e.g.,
"generate-response", "database-query").Additional options for the span
Returns
A new Span instance that you must end by calling
tracer.endSpan(span).Example
endSpan()
End a span and add it to the flush buffer. If the span hasn’t been ended manually, this method will callspan.end() automatically.
Parameters
The span to end.
Example
currentSpan()
Get the currently active span in the current async context, orundefined if there is no active span.
Returns
The currently active span, or
undefined if no span is active.Example
flush()
Immediately flush all buffered spans to the ZeroEval backend. This is called automatically based onflushInterval and maxSpans configuration, but you can call it manually if needed.
Returns
A promise that resolves when the flush completes. If the flush fails, spans are re-added to the buffer for retry.
Example
configure()
Update tracer configuration after initialization. This is called automatically byinit(), but you can use it to change settings at runtime.
Parameters
Configuration options to update
Example
shutdown()
Flush remaining spans and tear down integrations. This is called automatically on process exit (beforeExit, SIGINT, SIGTERM), but you can call it manually for graceful shutdown.
Example
Context Propagation
The tracer uses Node.jsAsyncLocalStorage to automatically propagate span context across async boundaries. This means child spans automatically inherit the parent span’s context without manual threading.
Buffering and Flushing
The tracer buffers completed spans and flushes them to the backend in batches:- Time-based flushing: Spans are flushed every
flushIntervalseconds (default: 10s) - Size-based flushing: Spans are flushed when the buffer reaches
maxSpans(default: 100) - Trace completion: Spans are buffered per trace until the root span completes, then moved to the main buffer
Notes
- The tracer is a singleton—there is one global instance per process
- Spans are automatically ordered parent-first before flushing to ensure proper trace reconstruction
- Failed flushes are retried by re-adding spans to the buffer
- The tracer maintains separate buffers for active traces and completed spans