Testing
Testing utilities for Logfire. Seethe guide for examples.
TestExporter¶
TestExporter()
Bases:SpanExporter
A SpanExporter that stores exported spans in a list for asserting in tests.
Source code inlogfire/_internal/exporters/test.py
3031 |
|
export¶
export(spans:Sequence[ReadableSpan])->SpanExportResult
Exports a batch of telemetry data.
Source code inlogfire/_internal/exporters/test.py
33343536 |
|
clear¶
clear()->None
Clears the collected spans.
Source code inlogfire/_internal/exporters/test.py
383940 |
|
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 inlogfire/_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 99100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
|
TestLogExporter¶
Bases:InMemoryLogExporter
A LogExporter that stores exported logs in a list for asserting in tests.
Source code inlogfire/_internal/exporters/test.py
187188189 |
|
SeededRandomIdGeneratordataclass
¶
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 byrandom.seed(...)
.
Trace IDs are 128-bit integers.Span IDs are 64-bit integers.
IncrementalIdGeneratordataclass
¶
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 inlogfire/testing.py
44454647 |
|
generate_span_id¶
generate_span_id()->int
Generates a span id.
Source code inlogfire/testing.py
495051525354 |
|
generate_trace_id¶
generate_trace_id()->int
Generates a trace id.
Source code inlogfire/testing.py
565758596061 |
|
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 inlogfire/testing.py
7172 |
|
CaptureLogfiredataclass
¶
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 ofcapfire
fixture.
get_collected_metrics¶
get_collected_metrics()
Get the collected metrics as a list of dictionaries.
Source code inlogfire/testing.py
969798 |
|
capfire¶
capfire()->CaptureLogfire
A fixture that returns a CaptureLogfire instance.
Source code inlogfire/testing.py
101102103104105106107108109110111112113114115116117118119120 |
|