Flushing Tracing Data
Understand how and when to manually send buffered tracing data to SGP.
By default, tracing data (spans) are collected in an in-memory queue and sent to the SGP platform asynchronously in batches by a background worker. This batching mechanism is designed for efficiency and minimal performance impact on your application.
However, there are specific scenarios where you might need to force an immediate export of tracing data:
- Critical workflow points: To ensure data is persisted before sensitive operations or transitions in a distributed workflow.
- Debugging: To see tracing data appear on the Traces page immediately during development.
How to Flush Tracing Data
The SDK provides methods to manually trigger data export:
Flushing the entire queue
Use tracing.flush_queue()
to force a synchronous export of all spans currently buffered in the SDK’s internal queue. This method will block the calling thread until all pending spans have been sent.
Flushing individual spans or traces
You can also explicitly flush an individual Span
object. When you call span.flush()
, that specific span will be sent. For Trace
objects, flushing (trace.flush()
) will send the root span.
By default, span.flush()
and trace.flush()
are blocking operations. This means the calling thread will pause until the data is successfully sent to the SGP platform.
For non-blocking behavior (recommended in performance-sensitive applications), pass blocking=False
:
Automatic Shutdown Flush
You typically do not need to manually call flush_queue()
at the very end of your program. When the SDK’s background worker is shut down (e.g., when your Python application exits normally), it will automatically attempt to flush any remaining tracing data in the queue before exiting.