Skip to content

Logfire Utilities Testing: Validate Traces & Metrics

Testing utilities for Logfire. See the guide for examples.

TestExporter

Bases: SpanExporter

A SpanExporter that stores exported spans in a list for asserting in tests.

Methods

export

def export(spans: Sequence[ReadableSpan]) -> SpanExportResult

Exports a batch of telemetry data.

Returns

SpanExportResult

clear

def clear() -> None

Clears the collected spans.

Returns

None

exported_spans_as_dict

def exported_spans_as_dict(
    fixed_line_number: int | None = 123,
    strip_filepaths: bool = True,
    include_resources: bool = False,
    include_instrumentation_scope: bool = False,
    _include_pending_spans: bool = False,
    _strip_function_qualname: bool = True,
    parse_json_attributes: bool = False,
) -> list[dict[str, Any]]

The exported spans as a list of dicts.

Returns

list[dict[str, Any]] — A list of dicts representing the exported spans.

Parameters

fixed_line_number : int | None Default: 123

The line number to use for all spans.

strip_filepaths : bool Default: True

Whether to strip the filepaths from the exported spans.

include_resources : bool Default: False

Whether to include the resource attributes in the exported spans.

include_instrumentation_scope : bool Default: False

Whether to include the instrumentation scope in the exported spans.

parse_json_attributes : bool Default: False

Whether to parse strings containing JSON arrays/objects.

IncrementalIdGenerator

Bases: IdGenerator

Generate sequentially incrementing span/trace IDs for testing.

Trace IDs start at 1 and increment by 1 each time. Span IDs start at 1 and increment by 1 each time.

Methods

reset_trace_span_ids

def reset_trace_span_ids() -> None

Resets the trace and span ids.

Returns

None

generate_span_id

def generate_span_id() -> int

Generates a span id.

Returns

int

generate_trace_id

def generate_trace_id() -> int

Generates a trace id.

Returns

int

TimeGenerator

Generate incrementing timestamps for testing.

Timestamps are in nanoseconds, start at 1_000_000_000, and increment by 1_000_000_000 (1 second) each time.

CaptureLogfire

A dataclass that holds a span exporter, log exporter, and metric reader.

This is used as the return type of capfire fixture.

Attributes

exporter

The span exporter.

Type: TestExporter

metrics_reader

The metric reader.

Type: InMemoryMetricReader

log_exporter

The log exporter.

Type: TestLogExporter

Methods

get_collected_metrics

def get_collected_metrics()

Get the collected metrics as a list of dictionaries.

TestLogExporter

Bases: InMemoryLogRecordExporter

A LogExporter that stores exported logs in a list for asserting in tests.

SeededRandomIdGenerator

Bases: IdGenerator

Generate random span/trace IDs from a seed for deterministic tests.

Similar to RandomIdGenerator from OpenTelemetry, but with a seed. Set the seed to None for non-deterministic randomness. In that case the difference from RandomIdGenerator is that it’s not affected by random.seed(...).

Trace IDs are 128-bit integers. Span IDs are 64-bit integers.

capfire

def capfire() -> CaptureLogfire

A fixture that returns a CaptureLogfire instance.

Returns

CaptureLogfire

get_collected_metrics

def get_collected_metrics(metrics_reader: InMemoryMetricReader) -> list[dict[str, Any]]

Get the collected metrics as a list of dictionaries.

Returns

list[dict[str, Any]]