get_span_buffer()

Signature

1get_span_buffer(trace_id: str) -> SpanBuffer

Returns a SpanBuffer context manager that routes all spans created within it into a local queue instead of exporting them immediately. This enables manual batch collection and deferred export.

Example

1from respan import Respan, get_client
2
3respan = Respan(api_key="your-api-key")
4client = get_client()
5
6# Collect spans into a buffer
7with client.get_span_buffer("trace-123") as buffer:
8 buffer.create_span("step_1", {"status": "completed"})
9 buffer.create_span("step_2", {"status": "completed"})
10
11 # Inspect buffered spans
12 count = buffer.get_span_count() # 2
13 spans = buffer.get_all_spans() # List[ReadableSpan]
14
15# After exiting the context, process the collected spans
16client.process_spans(spans)

SpanBuffer methods

MethodDescription
create_span(name, attributes=None, kind=None)Create a span in the buffer. Returns the span ID.
get_all_spans()Get all buffered spans as a list.
get_span_count()Get the number of buffered spans.
clear_spans()Discard all buffered spans.

All span creation within the buffer context (including decorator-created spans and tracer calls) is buffered. Extract spans with get_all_spans() before exiting the context, then use client.process_spans() to export them.