Skip to main content

Setup Function

init_tracing

Initialize OpenTelemetry tracing with streaming file export. Signature:
Parameters: Returns: None Side Effects:
  • Sets up OpenTelemetry tracer provider
  • Instruments OpenAI and Anthropic clients
  • Creates/truncates output file
Important: Must be called before instantiating LLM clients to ensure instrumentation captures all calls. Example:
Environment Variables:
  • 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:
Parameters: Yields: Span - OpenTelemetry span object Example:
Nested Spans:

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: Methods: Example:
Filtering SUT Spans:
Conditional Logic Based on Tracing:

Utility Functions

get_tracer

Get an OpenTelemetry tracer instance for creating custom spans. Signature:
Parameters: Returns: Tracer - OpenTelemetry tracer instance Example:

clear_traces

Clear the trace output file. Signature:
Parameters: None Returns: None Example:

set_trace_output_path

Change the trace output path for the current exporter. Signature:
Parameters: Returns: None Example:

get_span_collector

Get the current span collector instance for accessing collected spans. Signature:
Parameters: None Returns: InMemorySpanCollector | None - The active span collector, or None if tracing is not enabled Example:
Note: Most tests should use 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 use TraceContext instead. Methods: Example:
When to use:
  • Custom test runners or frameworks built on Merit
  • Advanced trace analysis across multiple tests
  • Performance profiling and debugging
When NOT to use:
  • Regular test assertions (use trace_context parameter)
  • Single-test trace inspection (use trace_context.get_child_spans())

Automatic Tracing

LLM Client Instrumentation

When init_tracing() is called, Merit automatically instruments:
  • OpenAI - openai package
  • Anthropic - anthropic package
All LLM calls are captured with:
  • 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
Example:

SUT Tracing

Functions and classes decorated with @sut are automatically traced:
Captured Information:
  • Input arguments (args and kwargs)
  • Output values
  • Execution time
  • Nested LLM calls (as child spans)

Usage Patterns

Basic Setup

Trace Structure:

Custom Steps

Trace Structure:

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 via ReadableSpan.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.name
  • sut.input.args / sut.input.kwargs / sut.input.count
  • sut.output / sut.output.type

CLI Integration

Merit CLI automatically enables tracing when --trace flag is used: