Testing
Testing utilities for Logfire.
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
25 26 |
|
export ¶
export(spans: Sequence[ReadableSpan]) -> SpanExportResult
Exports a batch of telemetry data.
Source code in logfire/_internal/exporters/test.py
28 29 30 31 |
|
clear ¶
clear() -> None
Clears the collected spans.
Source code in logfire/_internal/exporters/test.py
33 34 35 |
|
exported_spans_as_dict ¶
exported_spans_as_dict(
fixed_line_number: int | None = 123,
strip_filepaths: bool = True,
include_resources: bool = False,
include_package_versions: bool = False,
include_instrumentation_scope: bool = False,
_include_pending_spans: bool = False,
_strip_function_qualname: bool = True,
) -> 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 package versions in the exported spans. |
False
|
|
bool
|
Whether to include the instrumentation scope in the exported spans. |
False
|
Returns:
Type | Description |
---|---|
list[dict[str, Any]]
|
A list of dicts representing the exported spans. |
Source code in logfire/_internal/exporters/test.py
37 38 39 40 41 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 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
|
SeededRandomIdGenerator
dataclass
¶
SeededRandomIdGenerator(seed: int | None = 0)
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
39 40 41 42 |
|
generate_span_id ¶
generate_span_id() -> int
Generates a span id.
Source code in logfire/testing.py
44 45 46 47 48 49 |
|
generate_trace_id ¶
generate_trace_id() -> int
Generates a trace id.
Source code in logfire/testing.py
51 52 53 54 55 56 |
|
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
66 67 |
|
CaptureLogfire
dataclass
¶
CaptureLogfire(
exporter: TestExporter,
metrics_reader: InMemoryMetricReader,
)
A dataclass that is holds both span exporter and metric renderer.
This is used as the return type of capfire
fixture.
metrics_reader
instance-attribute
¶
metrics_reader: InMemoryMetricReader
The InMemoryMetricReader
instance.
capfire ¶
capfire() -> CaptureLogfire
A fixture that returns a CaptureLogfire instance.
Source code in logfire/testing.py
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
|