Logfire Utilities Testing: Validate Traces & Metrics
Testing utilities for Logfire. See the guide for examples.
Bases: SpanExporter
A SpanExporter that stores exported spans in a list for asserting in tests.
def export(spans: Sequence[ReadableSpan]) -> SpanExportResult
Exports a batch of telemetry data.
SpanExportResult
def clear() -> None
Clears the collected spans.
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.
list[dict[str, Any]] — A list of dicts representing the exported spans.
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.
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.
def reset_trace_span_ids() -> None
Resets the trace and span ids.
def generate_span_id() -> int
Generates a span id.
def generate_trace_id() -> int
Generates a trace id.
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.
A dataclass that holds a span exporter, log exporter, and metric reader.
This is used as the return type of capfire fixture.
The span exporter.
Type: TestExporter
The metric reader.
Type: InMemoryMetricReader
The log exporter.
Type: TestLogExporter
def get_collected_metrics()
Get the collected metrics as a list of dictionaries.
Bases: InMemoryLogRecordExporter
A LogExporter that stores exported logs in a list for asserting in tests.
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.
def capfire() -> CaptureLogfire
A fixture that returns a CaptureLogfire instance.
CaptureLogfire
def get_collected_metrics(metrics_reader: InMemoryMetricReader) -> list[dict[str, Any]]
Get the collected metrics as a list of dictionaries.