Testing
Testing utilities for Logfire. See the guide for examples.
TestExporter ¶
TestExporter()
            Bases: SpanExporter
A SpanExporter that stores exported spans in a list for asserting in tests.
Source code in logfire/_internal/exporters/test.py
                  | 30 31 |  | 
export ¶
export(spans: Sequence[ReadableSpan]) -> SpanExportResult
Exports a batch of telemetry data.
Source code in logfire/_internal/exporters/test.py
            | 33 34 35 36 |  | 
clear ¶
clear() -> None
Clears the collected spans.
Source code in logfire/_internal/exporters/test.py
            | 38 39 40 |  | 
exported_spans_as_dict ¶
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.
Parameters:
| Name | Type | Description | Default | 
|---|---|---|---|
|                  | int | None | The line number to use for all spans. | 123 | 
|                  | bool | Whether to strip the filepaths from the exported spans. | True | 
|                  | bool | Whether to include the resource attributes in the exported spans. | False | 
|                  | bool | Whether to include the instrumentation scope in the exported spans. | False | 
|                  | bool | Whether to parse strings containing JSON arrays/objects. | False | 
Returns:
| Type | Description | 
|---|---|
| list[dict[str, Any]] | A list of dicts representing the exported spans. | 
Source code in logfire/_internal/exporters/test.py
            | 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |  | 
TestLogExporter ¶
            Bases: InMemoryLogExporter
A LogExporter that stores exported logs in a list for asserting in tests.
Source code in logfire/_internal/exporters/test.py
                  | 187 188 189 |  | 
          SeededRandomIdGenerator
  
  
      dataclass
  
¶
SeededRandomIdGenerator(
    seed: int | None = 0,
    _ms_timestamp_generator: Callable[
        [], int
    ] = _default_ms_timestamp_generator,
)
            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.
          IncrementalIdGenerator
  
  
      dataclass
  
¶
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.
reset_trace_span_ids ¶
reset_trace_span_ids() -> None
Resets the trace and span ids.
Source code in logfire/testing.py
            | 44 45 46 47 |  | 
generate_span_id ¶
generate_span_id() -> int
Generates a span id.
Source code in logfire/testing.py
            | 49 50 51 52 53 54 |  | 
generate_trace_id ¶
generate_trace_id() -> int
Generates a trace id.
Source code in logfire/testing.py
            | 56 57 58 59 60 61 |  | 
TimeGenerator ¶
TimeGenerator(ns_time: int = 0)
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.
Source code in logfire/testing.py
                  | 71 72 |  | 
          CaptureLogfire
  
  
      dataclass
  
¶
CaptureLogfire(
    exporter: TestExporter,
    metrics_reader: InMemoryMetricReader,
    log_exporter: TestLogExporter,
)
A dataclass that holds a span exporter, log exporter, and metric reader.
This is used as the return type of capfire fixture.
get_collected_metrics ¶
get_collected_metrics()
Get the collected metrics as a list of dictionaries.
Source code in logfire/testing.py
            | 96 97 98 |  | 
capfire ¶
capfire() -> CaptureLogfire
A fixture that returns a CaptureLogfire instance.
Source code in logfire/testing.py
            | 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |  |