Skip to main content
magpie-server import loads traces from files into a store (src/import.rs). It is for a company that moves to Magpie and wants its old traces too.
The server must not run on the same store. A store has one writer, and the import is a writer: with a server on the store, the import ends with an error and writes nothing. Stop the server, import, start it again. Or import into a new store, and point the server at it after. Time order. A store is fast when each of its files holds one stretch of time: lists stop early, a time filter skips files, and old data goes as whole files (FINDINGS F28, F29). So the import writes the spans in the order of their start times, whatever the order in the files. It reads every line once and sorts the spans in pieces that fit the memory limit, each piece in a temporary file; then it merges the pieces and writes them to the store. Inside one minute the spans of one trace follow each other, so a short trace is written whole. A trace that is longer than a minute comes in pieces, as in live traffic, and is one trace with the right totals at the end. Safe to run again. Every span is written with upsert: it replaces the stored span with the same trace id and span id. A score replaces the one with the same trace, span, name and source. So:
  • An import that stopped (a crash, Ctrl-C, a full disk) is run again with the same command. Nothing is stored twice. It starts from the first line again: there is no resume.
  • A file with spans that the store already has (from live traffic, or from an earlier import) replaces them.
  • The same span twice in the input: the copy with the later start time stays; of copies with one start time, the later one in the input.
Bad lines. A line that is not JSON, is of another format, or holds a span the store refuses (no ids, no start time, an end before the start, attributes or events nested deeper than 64 levels) is skipped. A store that was made by a build from before the depth rule can hold spans with attributes or events of 65 to 126 levels: such a store opens, answers and exports as before (and as before, a store with no pieces that has a span of 124 to 126 levels in its write log does not open after a crash: its log record is deeper than the reader of JSON goes), but an import of its export skips those lines (they are counted and named, as every skipped line). No such store is known: nothing is published. The import goes on, counts it, and names the first 10 with file, line number and reason in its summary. In an OTLP line a refused span is skipped alone, as in live ingest. With --strict the first bad line ends the import. Bad lines are found while the files are read, before the store is written to; only a score that the store refuses is found later, when it is written. Output. Progress goes to stderr, one line per second: lines, bytes and spans read (pass 1), then spans written and the time up to which every span is written (pass 2). At the end one line of JSON goes to stdout:
(The summary of the import in results/import-small.json.) A bad line is {"file": "export.ndjson", "line": 17, "reason": "not JSON: ..."} in firstProblems. skippedLines: lines that gave nothing. refused: spans and scores refused inside lines that were read. from, to: the earliest and the latest start time (ms) of the spans written. runs: temporary files. tempBytes: the most bytes the temporary folder held. seconds: how long the import took. Exit code 0: done (also with skipped lines; they are in the JSON). 1: an error, or no line could be imported (then check --format). 2: wrong flags. 130: stopped by SIGINT or SIGTERM. Memory. --memory-mb is the limit of the import’s own buffers: half of it holds the spans in memory while the files are read. It also sets the store’s write buffers for the second pass (a quarter each for the index writer and the write queue). It is not a limit of the store: while the store takes writes it merges its files in the background, and its memory for that does not follow these numbers. One line must fit in memory several times (an OTLP line is a whole request). More memory makes fewer temporary files and bigger first files in the store. Measured with --memory-mb 256 on 809,982 spans, 1.23 GB of lines (bench/import/run.sh small 256; the peak phys_footprint of each process; two runs, results/import-small.json and results/import-small-run2.json): So an import of traces that the store does not have stays near the limit; it is not a hard bound (one of the two runs was 0.3 MB above it). The limit does not hold when the import replaces spans or adds spans to stored traces: each such upsert is a delete in the index, and the index keeps every delete in memory while one of its merges runs. A span of a new trace needs no delete (store.rs, apply); before that rule was in the store, the import into an empty store took 613 MB (results/import-small-before-the-upsert-fix.json, from the code before that rule). Disk. The temporary folder holds every span once as JSON: about the size of the input (for otlp and langsmith: of the spans after the mapping). With more than 64 temporary files, 64 of them are first merged into one, and the folder holds that one twice for a moment. The folder is removed at the end, also after an error or Ctrl-C. After a kill it stays; the next import of the same store removes it. The import never removes a folder that holds anything but its own files. A store that already has data. The import gives the right answers on any store. What changes is how well the store’s files follow time. Measured on the same data (magpie-scale segments, results/segments-import-small-*.json): the number of span files that cover one moment, as a mean over the time range (1.00 is the best) and at most.
  • Into an empty store: every moment is in one file.
  • Old data into a store that has newer data: not as good (1.21). The cause: while the import runs, the store merges the first imported files with its own oldest file, because at that moment these are neighbours in time. The rest of the import then lies inside the time range of that merged file.
  • Data of the time range that the store already has: two files cover a part of the time.
  • So: import first, into an empty store, then go live. If the server is live already: export the live store, import the history and that export into a new store, and point the server at the new store. An import into the live store works and loses nothing; lists and time filters on the stretches with two files read both.
  • The server’s --retention holds for imported spans too: spans older than the limit are dropped when the server opens the store.

Formats

  • magpie: the lines of POST /v1/export and POST /v1/export/scores (FINDINGS F36): one span or one score per line, in the JSON shape of span and score. Span lines and score lines can be in one file. Every field of a span comes back as it was exported: also cacheReadTokens, cacheWriteTokens, reasoningTokens, timeToFirstTokenMs, inputCost, outputCost, and a cost that the first store computed from its price table (costComputed stays true, and no price table is needed for the import).
  • otlp: OTLP/JSON, one ExportTraceServiceRequest ({"resourceSpans": [...]}) per line. That is what the OpenTelemetry Collector’s file exporter writes by default (format: json, no compression). The OTLP/JSON rules hold: ids as hex text (upper or lower case), 64-bit numbers as text, enums as numbers, unknown fields ignored; tested with the example file of the OpenTelemetry project (tests/samples/otlp-trace.json). The format of the exporter’s file is taken from its README; a Collector binary was not run. Each line goes through the code of POST /v1/traces with a JSON body (genai::spans), so a span from a file is equal to the span of live ingest of the same request (the mapping below, with its usage fields). For the same computed costs give the import the server’s --prices file. A request that is printed over many lines must be made one line first: jq -c . trace.json.
  • langsmith: LangSmith runs, one run per line, with the field names of LangSmith’s “Run (span) data format” (docs.langchain.com/langsmith/run-data-format). The runs API returns them, and the bulk export writes the same fields as Parquet (make JSON lines from it first with a Parquet tool; such a file was not tested here). The mapping table is at the top of src/import/langsmith.rs. In short: id, trace_id, parent_run_id are the ids; run_type llm is model_generation and tool is tool_call; tokens and cost (total_cost, and prompt_cost and completion_cost as inputCost and outputCost) are taken from llm and embedding runs only (LangSmith puts the totals of a trace on its parent runs); timeToFirstTokenMs is first_token_time minus start_time; cached and reasoning tokens are not set (the run format has no field for them); the model is extra.metadata.ls_model_name; the session is extra.metadata.session_id or thread_id (the run’s own session_id is the project and becomes an attribute); feedback_stats gives one score per key with the average as its value.
Why LangSmith and not Langfuse, Phoenix, Opik, Helicone or MLflow: it is the one export format with a sample in public docs that a test can read (tests/samples/langsmith-run.json is that sample). Langfuse writes JSON lines with a documented list of fields, but its docs show no line of such a file; a mapping for it needs a real export file first.

Limits of the import

  • Speed is not measured. Each span is read as JSON twice (once for its place in the order and the checks, once for the write).
  • Memory: --memory-mb does not bound an import that replaces stored spans or adds spans to stored traces (table above).
  • No resume: an import that stopped starts again from the first line. The spans it wrote before are replaced, which costs the memory of the row “the same file a second time”.
  • Old data into a store that has newer data leaves two files on a part of the time range (table above). Writing such an import newest first may avoid it; that is not built and not tested.
  • Not through the server: there is no HTTP route, and the server must be stopped. Not in the Node and Python packages. In Rust it is a library call: magpie_server::import::import(&store, inputs, &options, &mut progress).
  • LangSmith: a score is the average of a run’s feedback; the single feedback rows are another export and are not read. The sample of LangSmith’s docs has empty inputs and outputs, so the text of real LangChain messages was not checked against a real export.
  • A span with a wrong time (year 1970, year 3000) is written as it is. It makes one file of the store cover a long time.
  • One span per trace id and span id: a file from a tool that uses the same span id for two spans of one trace keeps the later one.