Mapping from an OpenTelemetry span to a Magpie span
src/genai.rs, MAPPING_VERSION = 2 (2: usage and cost; spans stored with mapping 1 have no cached or reasoning tokens, no time to first token and no cost). It follows the OpenTelemetry GenAI semantic conventions, open-telemetry/semantic-conventions-genai release v0.1.0 (2026-11-21). These are still marked “Development”, so the mapping has its own version, and every stored span has magpie.mapping: 2 in its attributes. It also reads older GenAI names and a few clear names of the Vercel AI SDK and OpenInference. The first name found wins.
input and output are readable text, not JSON. A message becomes role: text, one line each; other objects become key: value lines. Why: it is easier to read, and real line breaks keep every word a word. (The core also indexes a word after a JSON escape like \nRefund as refund, so JSON text is found too.)
Why tokens come from model calls only: an invoke_agent span reports the sum of the model calls under it (the conventions say so). Counting both would count the same tokens twice in trace totals and sums. The agent’s own numbers stay in its attributes. The same holds for cached and reasoning tokens, the cost and the time to first token.
Cached and reasoning tokens are parts, not extra tokens. In Magpie, inputTokens is every input token (the cached ones too) and outputTokens is every output token (the reasoning ones too), so totalTokens = inputTokens + outputTokens counts each token once. The GenAI conventions say the same: gen_ai.usage.input_tokens “SHOULD include cached tokens”, and the cache and reasoning counts “SHOULD be included in” the two totals. The providers differ: OpenAI’s input_tokens has the cached tokens in it, Anthropic’s input_tokens does not (the total is input_tokens + cache_read_input_tokens + cache_creation_input_tokens), and Google’s candidatesTokenCount is next to its thoughtsTokenCount, not around it. An instrumentation that follows the conventions adds them up before it sends. For one that does not, the server has one rule: a part that is larger than its whole shows that the parts were left out, so they are added to the count, and the count as it came stays in the attributes (magpie.usage.input_tokens_reported, magpie.usage.output_tokens_reported). When the parts are smaller than the count, the server cannot know, and takes the numbers as they are.
Cost
A span hascost (the total, US dollars), and its two parts inputCost and outputCost. The store keeps them as whole nano-dollars; sums are costNanos, inputCostNanos, outputCostNanos.
- A cost that comes with the span is kept (see the mapping table). When only parts come,
costis their sum. - With
--prices <file>, a model call that comes with tokens and no cost gets a cost computed from the file:inputCost,outputCost,cost, andcostComputed: true. - A model that is not in the file gets no cost. The server never guesses a price. A span with no tokens gets no cost either.
- The cost is computed once, when the span is written, and stored with it. A new price file (the server reads it when it starts) changes the cost of new spans only: old spans keep the cost of their day.
model of the spans (the response model, when the span has one). At most 6 decimals. cacheRead and cacheWrite are the prices of input tokens read from and written to the provider’s cache; left out, each is the input price. These numbers are made up:
inputCost = (input tokens - cache read - cache write) x input + cache read x cacheRead + cache write x cacheWrite. outputCost = output tokens x output (reasoning tokens are output tokens). The math is in whole numbers (pico-dollars per token); each part is rounded to whole nano-dollars, half up. Example with made-up-large: 1,000 input tokens of which 600 from the cache and 100 written to it, 200 output tokens: inputCost = (300 x 3 + 600 x 0.3 + 100 x 3.75) / 1,000,000 = 0.001455 dollars, outputCost = 200 x 15 / 1,000,000 = 0.003, cost = 0.004455.
No price list comes with Magpie: prices change, and they differ by contract and by region. Take them from the price page of each provider (for example https://platform.claude.com/docs/en/about-claude/pricing, https://developers.openai.com/api/docs/pricing, https://aistudio.google.com/docs/pricing), under the model names your spans have. One price per model: a model with price steps (a higher price above a prompt size, batch prices, a 1-hour cache) needs the price you pay most, or a cost sent with the span.
Masking
With--mask <file> the server replaces private data in every span and every score BEFORE it is stored. The write log, the index, the trace summaries, the previews and the exports then hold the masked text only: no search finds the original and no read returns it. It is off by default: without the flag nothing is masked and a write does no extra work.
- A rule is
builtinorpattern(a regex in the syntax of the Rustregexcrate: no look-around, no back-references).nameis its name in the counts; a rule with a pattern needs one, a built-in rule has the name of the built-in by default. No two rules have one name.replaceis the text that goes in the place of each match, as it is (no$1), and it must not be empty. The default is the name in upper case in square brackets:[EMAIL],[ORDER]. dropAttributesremoves attribute values whole, whatever they hold. A dot is a path step, as in an attribute filter:customer.addresstakes the flat keycustomer.address, the nested{"customer": {"address": ..}}, and every key below it (customer.address.street).- A wrong policy stops the start: a rule with no such built-in, a regex that does not compile, a pattern that matches the empty text, an unknown field. The server (also with
--tenants-dir),importandmask-testthen end with an error that names the rule. A write never fails because of the policy. - One policy for all tenants in this version. A policy per tenant is planned, not built.
- New writes only. The server reads the file when it starts. A new policy does not change what is stored: for old data use the deletes (
POST /v1/traces/delete,POST /v1/purge).
What is masked:
input, output, error, every string value in attributes and in events (at every depth, also in lists), and the label and the comment of a score. With OTLP: the prompt and the answer, tool arguments and results, the exception message, and every attribute value that is text.
What is NOT masked: the ids (trace, span, parent span, session, user), the span name, kind, model, service, environment, tags, attribute keys, numbers and booleans, and of a score its name, source, authorId and evalId. Do not put private data there: a user id should be an id, not an email address. (To remove a whole attribute, name it in dropAttributes.)
How overlaps are settled. All rules look at the text as it was sent. They do not look at text another rule has changed. Where two matches overlap: the one that starts first wins; then the longer one; then the rule that is first in the file.
Limits.
- Text that holds JSON is masked as text: it is not parsed. A value that is written with a JSON escape inside it is not found:
bob\u0040example.comstays. An escape before a value (\nbefore an address) is fine, and stays as it is. - A match is replaced whole. Two equal values give the same replacement, and two different values too: the masked text does not say which values were equal.
- Queries are not masked: a search for an address, or a delete by a filter with one, is in the server’s memory (and a delete by filter is in the write log until its commit, a moment later).
- What a rule does not match is stored. Test your policy with real shapes of your data before you rely on it.
- The cost in time: not measured yet.
GET /v1/mask (read right) says how many matches each rule replaced since the store opened, and how many attribute values were dropped. It never holds a value. Nothing that was masked is logged or kept.
--max-open-tenants) starts at 0 again. A server with no policy answers {"droppedAttributes": 0, "enabled": false, "rules": []}.
Try a policy first. mask-test reads a text from the standard input, prints it masked, and prints the counts on stderr. It stores nothing.
tests/mask.rs (the real server binary: OTLP in, then every search and every read; import --mask; mask-test; wrong policies) and in the core (crates/core/src/mask.rs, crates/core/tests/mask.rs: no file of the store folder holds a masked value, also before the first commit and after a stop before the commit).