InMemoryExporter keeps span data in process memory instead of reporting it to an external platform. It organizes spans by session_id, which is useful for local debugging, testing, and dumping a session’s trace data to a JSON file for offline analysis.
When to use
- Local development and debugging of agent execution flows;
- Verifying spans in unit or integration tests;
- Offline environments without external platform connectivity, where you need to retain and analyze trace data locally.
Automatic management
InMemoryExporter is created and managed automatically by OpentelemetryTracer and records completed, sampled spans. It therefore cannot be added explicitly to the exporters list of OpentelemetryTracer, or initialization fails.
Usage
After a session run finishes, call the tracer’sdump method to export that session’s spans to a local JSON file:
app.py
Parameters
Constructor parameters
InMemoryExporter usually does not need to be constructed by hand; its constructor parameters are:
dump method parameters
OpentelemetryTracer.dump relies on spans retained by the in-memory exporter, with the following parameters:
The dump file is named like
{name}_{user_id}_{session_id}_{trace_id}.json, and its content is a list of spans, each containing the name, span_id, trace_id, start/end times, attributes, and parent span.
Memory usage grows with the number of spans, so long-running processes should watch memory growth. Session-level filtering relies on a correctly configured
session_id; dump first force-exports all pending spans, then filters by session_id before writing the file.Checking the export
Complete model configuration, save the example asapp.py, and run python app.py. The printed file path should exist, and its JSON list should contain spans from this model call
In-memory data is lost when the process exits. Session filtering relies on session identifiers recorded by model-call spans. The file can be empty if no relevant model span was recorded, sampling omitted the data, or the session ID differs. user_id names the output file; it does not filter spans or enforce access control