Setup Function
init_tracing
Initialize OpenTelemetry tracing with streaming file export. Signature:| Name | Type | Default | Description |
|---|---|---|---|
service_name | str | "merit" | Service name in trace metadata |
trace_content | bool | None | None | Content capture toggle. Note: today Merit uses MERIT_TRACE_CONTENT to control SUT span input/output attributes; trace_content is not currently applied to OpenAI/Anthropic client instrumentation. |
output_path | Path | str | ".merit/traces.jsonl" | File path for trace export |
None
Side Effects:
- Sets up OpenTelemetry tracer provider
- Instruments OpenAI and Anthropic clients
- Creates/truncates output file
MERIT_TRACE_CONTENT: Set to"false"to avoid recording SUT input/output content (sut.input.*,sut.output). This does not currently guarantee LLM client instrumentation content is redacted.
Context Manager
trace_step
Create a custom span for tracing application logic. Signature:| Name | Type | Default | Description |
|---|---|---|---|
name | str | - | Name of the span |
attributes | dict[str, Any] | None | None | Optional attributes to attach to span |
Span - OpenTelemetry span object
Example:
Classes
TraceContext
Provides access to trace data for the current test execution. Injection:TraceContext is automatically injected when a merit function declares trace_context as a parameter. It enables querying child spans, LLM calls, and setting custom attributes on the test span.
Properties:
| Name | Type | Description |
|---|---|---|
trace_id | str | The trace ID for this test’s span (32 hex characters) |
span_id | str | The span ID for this test’s span (16 hex characters) |
is_enabled | bool | Whether tracing is currently enabled |
| Method | Returns | Description |
|---|---|---|
get_child_spans() | list[ReadableSpan] | Get all spans created during this test’s execution |
get_llm_calls() | list[ReadableSpan] | Get spans from LLM API calls (OpenAI, Anthropic) |
get_sut_spans(name=None) | list[ReadableSpan] | Get spans from @merit.sut decorated functions, optionally filtered by name |
set_attribute(key, value) | None | Set a custom attribute on the test span |
Utility Functions
get_tracer
Get an OpenTelemetry tracer instance for creating custom spans. Signature:| Name | Type | Default | Description |
|---|---|---|---|
name | str | "merit" | Tracer name |
Tracer - OpenTelemetry tracer instance
Example:
clear_traces
Clear the trace output file. Signature:None
Example:
set_trace_output_path
Change the trace output path for the current exporter. Signature:| Name | Type | Description |
|---|---|---|
output_path | Path | str | New file path for trace export |
None
Example:
get_span_collector
Get the current span collector instance for accessing collected spans. Signature:InMemorySpanCollector | None - The active span collector, or None if tracing is not enabled
Example:
trace_context parameter instead, which provides a cleaner API scoped to the current test. get_span_collector() is useful for advanced scenarios requiring access to all spans.
InMemorySpanCollector
Internal class that collects and stores OpenTelemetry spans during test execution. Purpose: This is an advanced/internal API used by Merit’s tracing system. Most users should useTraceContext instead.
Methods:
| Method | Returns | Description |
|---|---|---|
get_spans(trace_id) | list[ReadableSpan] | Get all spans for a specific trace ID |
clear(trace_id) | None | Clear spans for a specific trace ID |
clear_all() | None | Clear all collected spans |
- Custom test runners or frameworks built on Merit
- Advanced trace analysis across multiple tests
- Performance profiling and debugging
- Regular test assertions (use
trace_contextparameter) - Single-test trace inspection (use
trace_context.get_child_spans())
Automatic Tracing
LLM Client Instrumentation
Wheninit_tracing() is called, Merit automatically instruments:
- OpenAI -
openaipackage - Anthropic -
anthropicpackage
- Request parameters (model, temperature, messages, etc.)
- Response/content details depend on the underlying OpenTelemetry instrumentor configuration (Merit does not currently toggle this via
trace_content) - Timing information
- Token usage
- Error details
SUT Tracing
Functions and classes decorated with@sut are automatically traced:
- Input arguments (args and kwargs)
- Output values
- Execution time
- Nested LLM calls (as child spans)
Usage Patterns
Basic Setup
Custom Steps
Debugging with Traces
Privacy Controls
CI/CD Integration
Trace File Format
Traces are exported as JSONL (JSON Lines). Each line is a complete OpenTelemetry span serialized viaReadableSpan.to_json().
Because the exact shape can vary by OpenTelemetry version and installed instrumentations, inspect a line directly in your trace file.
Merit-added attributes to look for (may be absent when MERIT_TRACE_CONTENT=false):
merit.sut/merit.sut.namesut.input.args/sut.input.kwargs/sut.input.countsut.output/sut.output.type
CLI Integration
Merit CLI automatically enables tracing when--trace flag is used: