# Query language and saved views

**Last updated:** 2026-11-27T07:51:36-03:00 (UTC-3)

The gateway has one query language. You write the same string in the **Traces** screen, in the **Request logs** screen, in the filter of an evaluator rule and in an alert rule, and you can keep it as a saved view. It reads the gateway's own facts about a request (did it fall back, which arm of a canary served it, did a cache answer, what did each guardrail decide, which customer and team it belongs to, which prompt revision it used, how it was judged) next to the usual ones (status, model, duration, tokens, cost).

This page is the reference of the language and of saved views. Start with [the trace explorer](/docs/features/trace-explorer/) if you have not used the Traces screen yet.

## Turn it on

```yaml
features:
  usage_analytics: true
  request_logs: true
  trace_query: true     # the query language and saved views (off by default)
  span_store: true      # needed for traces, spans and span fields on the request log
  trace_explorer: true  # the Traces screen
```

`trace_query` needs `request_logs`. It is a tenant feature: with team plans, a team role uses it only for teams whose plan includes it. You can also turn it on from **Settings → Features** or with `PUT /api/v1/admin/features/trace_query`.

With `trace_query` off nothing here is available, with one exception that keeps the Traces screen as it was: its query bar still reads terms joined by AND (`status:error model:chat* duration>2s`, a leading `-` to negate), and refuses everything else with a message that names the feature.

## Write a query

A query is a list of terms. Terms written side by side must all hold.

| You write | It means |
|---|---|
| `status:error` | the field equals the value |
| `model:chat*` | the value starts with `chat` (a trailing `*` is the only wildcard) |
| `session:*` | the field has a value |
| `-session:*` | the field has no value |
| `name:"POST /v1/chat/completions"` | a value with spaces, in double quotes (a quoted value is never a wildcard) |
| `name:"POST /v1/chat"*` | a prefix with spaces: the `*` goes after the closing quote |
| `duration>2s`, `cost<=0.05`, `tokens.input>=1000` | a comparison on a number, a duration (`250ms`, `2s`, `1m`, `1h`; a bare number is milliseconds) or a cost in US dollars |
| `duration:[1s TO 5s]`, `http_status:[500 TO *]` | a range, both ends included; `*` leaves a side open |
| `model:(chat OR fast*)` | any of up to 32 values |
| `a b`, `a AND b` | both |
| `a OR b` | either |
| `-a`, `NOT a` | not |
| `(a OR b) c` | parentheses group; without them AND binds tighter than OR, so `a b OR c` is `(a b) OR c` |
| `refund`, `"charged twice"` | a word or a phrase in the captured content (see [text search](#search-the-captured-text)) |

`AND`, `OR`, `NOT` and `TO` are read in any case. A value that is one of those words, or holds a space, a parenthesis or a bracket, goes in quotes: `model:"or"`.

Time is never part of the query: every screen has its own time range, and every read is bounded by it.

A term that compares a missing value is false, and its negation is true: `-cost>1` keeps the rows with a cost of at most 1 and the rows with no cost at all.

### Limits

| Limit | Value |
|---|---|
| Length of a query | 4,096 bytes |
| Terms (each value of a list counts) | 64 |
| Nesting of parentheses and negations | 8 levels |
| Values in a list | 32 |
| Length of a value | 256 bytes |
| Length of a text search phrase | 2 to 128 bytes |

A query over any of them is refused before anything is read. A refusal always says what is wrong and lists the fields you may use; it never shows a statement. Whatever you write in a value is passed to the storage engine as a bound value, never as part of a statement.

## Scopes

A query selects one kind of row. The fields you may write depend on it.

| Scope | Selects | Where |
|---|---|---|
| `traces` | traces: one row per team's summary of a trace | Traces screen, **Traces** |
| `spans` | spans | Traces screen, **Spans**; alert rules on the spans signal |
| `logs` | request log rows | Request logs screen; alert rules on the requests signal |
| `rules` | one request, as the gateway captures it | the filter of an evaluator rule |
| `sessions` | sessions: one row per team's summary of a session | Traces screen, **Sessions** ([sessions and end users](/docs/features/sessions-and-users/)) |
| `users` | end users: one row per team's end user over the time range | Traces screen, **Users** ([sessions and end users](/docs/features/sessions-and-users/)) |

`GET /api/v1/admin/query/fields?scope=logs` lists the fields of a scope **for the session that asks**: a field your rows do not show is not listed and not accepted (see [who may filter on what](#who-may-filter-on-what)).

## Fields

### Fields of a request, a span or a trace

| Field | Type | Scopes | What it is |
|---|---|---|---|
| `status` | `ok`, `error`, `unset` | traces, spans, logs | A trace is an error when one of its spans is. A request is an error when it answered 400 or more, or its answer was cut after it started |
| `http_status` | number | spans, logs | The HTTP status |
| `error_code` | text | spans, logs | The gateway's error code |
| `origin` | text | traces, spans, logs | How the traffic entered: `app`, `evaluation`, `experiment`, `playground`, `helper`, `shadow`, `replay` |
| `model` | text | all | The model alias (of a trace: any alias its requests used) |
| `requested_model` | text | spans, logs | The model the client asked for, when the gateway served another |
| `provider`, `provider_model` | text | spans (`provider` also logs) | The provider connection and its own model name |
| `operation` | text | spans, logs | `chat`, `embeddings`, `responses`, ... |
| `route` | text | spans | The API route of a request span |
| `team` | text | traces, spans, logs | Team ID |
| `organization` | text | traces, spans, logs | Organization ID, exact (`organization:org_a`; no wildcard): the rows recorded in the organization, and the rows that record none whose team is in it now. The same rule as the usage query's `organization` dimension. Needs the `organizations` feature; not available in an evaluator's filter, which matches spans as they are stored |
| `key`, `principal` | text | spans, logs (`key` also rules) | The key and its principal |
| `owner` | text | logs | The owner of the key: `user:<id>` or `workload:<id>` (needs `identity_ownership`) |
| `end_user`, `session` | text | all | The end user and the session the client declared |
| `parent_session`, `agent` | text | spans | The session a sub-session was started from, and the agent the client declared |
| `tag.<name>` | text | spans, logs, rules | A request tag: `tag.env:prod`, `tag.env:*` |
| `request_id`, `trace_id`, `span_id` | text | as they apply | Identifiers |
| `name` | text | traces, spans | The root span's name of a trace, or the span's name |
| `duration`, `ttfb` | duration | `duration` all but rules; `ttfb` spans, logs | Total time, and time to the first byte (`ttfb` on logs needs `request_timing`) |
| `cost` | money | traces, spans | Cost in US dollars |
| `tokens.input`, `tokens.output`, `tokens.total` | number | traces, spans, logs | Input tokens, output tokens, and their sum |
| `tokens.cached`, `tokens.reasoning` | number | spans | Cached input tokens and reasoning tokens |
| `service_tier` | text | spans, logs | Service tier |
| `helper`, `batch`, `parent_request` | text | logs | Helper sub-kind, batch ID, and the request a helper or judge call was made for |

### The gateway's own facts

| Field | Type | Scopes | What it is |
|---|---|---|---|
| `fell_back` | `true`, `false` | traces, spans | A request moved to another target |
| `attempts` | number | spans, logs | Provider sends the request made (on logs it needs `request_attempts`) |
| `canary.side` | text | spans | The arm of a canary rollout: `stable` or `canary`; empty without a rollout |
| `shadow` | `true`, `false` | traces, spans, logs | Shadow traffic |
| `auto_route` | text | spans | The auto-routing alias that chose the model |
| `cache` | `hit`, `none` | traces, spans | A cache answered |
| `cache.kind` | text | spans | Which cache answered: `exact` or `semantic` |
| `guardrail.flagged` | `true`, `false` | traces, spans | A guardrail flagged, masked or blocked |
| `guardrail.blocked` | `true`, `false` | spans, logs | A guardrail blocked the request |
| `guardrail.<id>.outcome` | text | spans | What one guardrail decided: `passed`, `blocked`, `masked`, `would_block`, `would_mask` |
| `timeout` | text | spans, logs | The timeout that ended the request: `first_byte`, `idle`, `max_duration`, `client_stall`, `request`, `attempt` |
| `valuation` | `known`, `partial`, `unknown`, `none` | traces, spans | Whether the cost is known |
| `customer.org`, `customer.team`, `customer.member` | text | spans | The customer the request was made for |
| `prompt.id`, `prompt.revision` | text, number | spans, logs | The registry prompt and the revision the request was built from: with [`prompt_tracking`](/docs/features/prompt-registry/#prompt-revision-on-traffic) the one it named (render token or declared headers) or else the [required system prompt](/docs/features/enforced-system-prompt/); without it, the required system prompt only, on request spans while `trace_query` is on. On the request log it needs `prompt_tracking` |
| `prompt.source` | `rendered`, `system`, `declared` | spans, logs | How the gateway knows the prompt revision: `rendered` and `system` are observed by the gateway, `declared` is the client's claim. Needs `prompt_tracking` |
| `prompt.label` | text | spans, logs | The [prompt label](/docs/features/prompt-registry/#labels-and-deploy-to) the render resolved (`production`, `staging`), when the prompt was rendered by label (`prompt_labels`) |
| `capture` | `none`, `audit_only`, `evaluation` | spans | Whether the request's content was captured, and for what |
| `span.kind` | `request`, `routing`, `provider_attempt`, `cache`, `guardrail`, `tool`, `judge`, `helper`, `app` | spans | Span kind |
| `span.subkind`, `span.attempt`, `span.final` | text, number, `true`/`false` | spans | A request's operation, the provider attempt number, and the attempt that answered |
| `trace.spans`, `trace.llm_calls`, `trace.tool_calls`, `trace.errors` | number | traces | Counts of a trace |
| `trace.tokens` | number | traces | Input plus output tokens of the whole trace (a [monitor](/docs/features/evaluation-monitors/#count-traces-by-their-aggregates) counts traces by it) |
| `tools.available` | number | spans | How many tools the request offered the model, on request spans. Recorded with `query_tool_fields` |
| `issue` | text | spans | An instrumentation issue of an [ingested application span](/docs/features/otlp-ingest/#instrumentation-issues), by code: `issue:invalid_tokens`; `issue:*` for any |
| `service` | text | spans | The `service.name` of the application that sent an ingested span |
| `tools.called` | number | spans | How many tool calls the model's answer made, on request spans. Recorded with `query_tool_fields`; a streamed answer is counted with `stream_usage` on |
| `review.queue` | text | traces, spans, logs | A [review queue](/docs/features/human-review/) the trace is an item of, by queue ID; `review.queue:*` is any queue you may read. Needs `query_review_queue` |

`span.name` and `span.status` are other spellings of `name` and `status`; `trace.cost` and `trace.duration` of `cost` and `duration`.

### Tools a request offered, and review queues

Two operator flags add facts that the gateway does not record by default.

**`query_tool_fields`** makes the request span record two counts, never a tool's name, its arguments or its result: `tools_available`, the tools the request declared (`tools` of Chat Completions, Messages, Responses and Interactions; the `functionDeclarations` of each generateContent tool), and `tools_called`, the tool calls of the model's answer (a chat message's `tool_calls`, a Messages `tool_use` block, a Responses `function_call` or `custom_tool_call` item, a Gemini `functionCall` part). Ask for a request that offered tools and called none with:

```text
tools.available>0 tools.called:0
```

In the traces scope the same query keeps the traces with such a request. Counts start with the requests made after the flag is on. A streamed answer is counted by the stream observer, which reads every event only with `stream_usage` on; without it a streamed request records `tools.available` alone, and `tools.called:0` does not match it. A request the provider refused records no `tools.called`.

**`query_review_queue`** adds `review.queue:<queue ID>`, which keeps the traces that are items of the queue: a trace or span item names its trace, a request item the request's trace, a session item the traces of the session in the time range. `review.queue:*` keeps the traces of any queue. Only the queues of your teams count, and only for a reader of evaluations (`evals` on, `evals:read`); a queue of another team matches nothing. The term names at most 2,000 traces, the newest items first, and says so when it names more (`evaluation_filter_truncated`). `NOT review.queue:<id>` keeps the rest. An evaluator's filter does not take it: queue membership is read when a query runs, and a live filter matches a span as it is stored.

### Evaluation results and feedback

For readers of evaluation results (`evaluation_results` on, `evals:read`), in every scope but `rules`:

| Term | Keeps |
|---|---|
| `evaluation.<name>.assessment:fail` | what an evaluator judged `pass`, `fail` or `none`; `evaluation.any.assessment:fail` for any evaluator |
| `evaluation.<name>.value>=0.5` | a numeric result compared with a number |
| `evaluation.<name>.value:good` | a categorical result equal to a label |
| `evaluation.<name>:*` | what the evaluator judged at all |
| `evaluation.<name>.source:human` | results by who made them: `llm`, `code`, `human`, `feedback`, `api` |
| `evaluation.<name>.version:2` | results of one version of the evaluator |
| `feedback.<name>.value<3`, `feedback.<name>.value:neutral`, `feedback.<name>:*` | the same for human and end-user labels |
| `feedback.<name>.submitter:alice` | feedback one submitter sent |
| `trace.failed_evaluations>=2` | traces by their number of failed results |

In the `sessions` and `users` scopes the same terms select the sessions or end users with such a result (on a session, on its traces, or on the end user's requests), counted per team, with `session.failed_evaluations` and `user.failed_evaluations` in place of `trace.failed_evaluations`; text search and topic terms are not read there.

A result belongs to a trace, whichever of its spans was judged: `evaluation.groundedness.value<0.5` keeps the traces where any span got such a result, and `child.evaluation.groundedness.value<0.5` is the same term in the traces scope. On the request log, such a term keeps the requests of the traces it names. Results are read from the start of the time range to a day after its end, because a judge finishes after the request it judged. Each term names at most 2,000 traces; when more match, the newest are used and the answer says so (`evaluation_filter_truncated`, or the header `X-Query-Evaluation-Truncated` on the request log).

### Topics

For readers of [Topics](/docs/features/topics/) (`topics` on, `topics:read`), in the `traces`, `spans` and `rules` scopes:

| Term | Keeps |
|---|---|
| `topic.<facet>:"<name>"` | the units of the topic with that name, in the live maps the reader sees; the name is quoted, and `*` after the quotes matches the start of a name |
| `topic.<facet>:outlier` | the units assigned to no topic of the facet |
| `topic.<facet>:none` | the units with no assignment in the facet at all |
| `topic.<facet>:*` | the units assigned to some topic of the facet |
| `topic.id:<id>` | the units of one topic by its ID, of a live map or a snapshot |

A topic belongs to a whole trace, session or run, so a topic term takes no `child.` or `parent.` prefix; in the `spans` scope it keeps the spans of the topic's units. Each engine reads the term as a join on the topic assignments, so it is exact for a topic of any size. The request log and alert rules do not take topic terms.

### Spans of a trace, and spans of a request

Some facts sit on one span of a request, not on the trace: the canary arm is on the routing step, a guardrail's outcome on the guardrail step, the provider on each attempt.

- **In the traces scope**, write a span field as it is. `canary.side:canary` keeps the traces that have a span on the canary arm; `provider:openai http_status:500` keeps the traces that have a span of that provider and a span with that status (not necessarily the same one).
- To ask for **one span that is several things**, use `child.`: `child.provider:openai child.http_status:500` keeps the traces where one attempt at that provider answered 500. The `child.` terms joined by AND in one group name the same span.
- **In the spans scope**, `child.field` is about a direct child of the span and `parent.field` about its parent: `span.kind:tool parent.span.kind:request`, `child.status:error`.
- **In the logs scope**, a span field keeps the requests that have such a span: `canary.side:canary status:error`, `cache.kind:semantic customer.org:acme`. It needs `span_store`, and each such term names at most 2,000 requests (`X-Query-Span-Truncated` otherwise). `child.` and `parent.` are not read there.

Related spans are read within the time range and one day around it, and only among the spans your teams may read.

### What is not a field

A field exists when the gateway records the fact on a span or on a request. These are asked another way:

| Looking for | Write | Why there is no field |
| --- | --- | --- |
| An environment (production, staging) | `tag.env:prod`, with `env` in the [tag taxonomy](/docs/features/usage-tags/) | An environment is a request tag here, not an object of its own: the tag is already filtered, counted as a facet (`tag.env`), read by evaluator rules and by alert rules |
| A tool that was called | `child.span.kind:tool`, `child.span.name:<tool>`, `trace.tool_calls>0` | A tool call is a span |
| Traces with comments | the comment count on the row, and the Annotations tab | Comments are text written by people and are not searched by the language |
| The requests of an experiment run | `origin:experiment`, and `run_id` on `GET /api/v1/admin/evaluations` | A run is read from its own screen and API |

## Search the captured text

A bare word or a quoted phrase searches the text of [captured content](/docs/features/content-capture/): the sent view of the request (the messages after the input guardrails) and the answer. It is case-insensitive and matches anywhere in the text.

```text
refund status:error
"charged twice" OR "double charge"
-refund model:chat*
```

Three rules bound it:

1. **Only content readers.** It takes `content_sent:read` of the team, the permission that shows the same text in the trace detail ([who reads captured content](/docs/features/content-capture/)). In the built-in roles that is the administrator, the organization admin and the team administrator, each within its teams. Anyone else gets a refusal, nothing is read and nothing is audited. Media and reasoning are never searched, and the received view (the request before the guardrails masked it) is not searched either: a secret a guardrail masked is not found.
2. **Bounded.** One search reads the newest `trace_query.text_search_max_records` captured records of the time range (default 500), and the first `trace_query.text_search_max_record_bytes` bytes of each side of each record (default 65,536). When older records were left unread the answer says so (`text_search.truncated`; the Traces screen shows a note): shorten the time range to search them. Content is encrypted at rest, so there is no index: the gateway decrypts and scans, which is why the bound exists.
3. **Audited.** Each search writes one audit row, `content_capture.search`, with who searched, which teams and how many records were read. The phrase is never stored or logged. The reads one question makes (the list, its facets, its chart) share one search for 30 seconds; a query searches at most 8 distinct words or phrases; and the Traces screen suspends auto-refresh while the query searches text, so a timer never writes audit rows.

Text search needs `content_capture`. Set `text_search_max_records` to `0` to turn it off while keeping the rest of the language. Rules and alert rules never read content: a text term there is refused.

## Who may filter on what

A filter never answers what a column would not show.

| You do not see | So you cannot filter on |
|---|---|
| tags (`usage_tags` off) | `tag.<name>` |
| sessions (`session_tracking` off) | `session`, `parent_session` |
| end users (`end_user_tracking` off) | `end_user`, `customer.member` |
| cost (`usage_valuation` off, or no `analytics:read`) | `cost`, `valuation` |
| evaluation results | the `evaluation.`, `feedback.` and `trace.failed_evaluations` terms |
| captured content | text search |
| a request log column behind a feature that is off | `owner`, `ttfb`, `attempts`, `http_status` (`request_log_status_code`) on logs |
| tool counts (`query_tool_fields` off) | `tools.available`, `tools.called` |
| review queues (`query_review_queue` or `evals` off, or no `evals:read`) | `review.queue` |

A refused field is reported as unknown, and is absent from `/query/fields` and from the completion.

Every read filters by your teams before anything is counted or paged, and of each team only what it recorded in its current organization. A term cannot widen that: `team:other-team` matches nothing for a reader who is not in it.

## In the Traces screen

The query bar completes field names as you type, proposes the values of a field after its colon, and proposes `OR`, `NOT` and `AND` (type them in capitals to get them before a field of the same letters). After a short pause it asks the gateway whether the draft would be read and shows the refusal under the bar, before you run it. **Run** or Enter runs it. The parts joined by AND at the top of the query are shown as chips, each with a remove button; a group in parentheses is one chip.

With the bar empty and focused, it offers terms taken from the rows on screen: a frequent value of a facet, the slowest twentieth (`duration>=` the p95), an evaluator with failed results, and a tag in use. Each is a valid term; click one to add it.

**Add a facet** in the facet rail counts the rows by any text, enum or boolean field of the scope (`valuation`, `key`, `shadow`, ...), beside the default facets. In the spans scope, **Count by a tag** counts a tag's values (`tag.env`); the API also counts `guardrail.<id>.outcome`, and `field=tag` lists the tag names in use.

**Create alert rule**, in the spans scope with a query, opens a new scheduled [alert rule](#in-an-alert-rule) on the spans the query matches, for a session that may manage alert rules.

`GET /api/v1/admin/traces?fields=` chooses what each row carries beside its core: `core`, `cost`, `evaluations`, `content`. Without it a row has cost and evaluations; `fields=core` is the lightest page and reads no evaluation results. A facet shows the 50 most frequent values; rows without a value are not counted. Clicking a value adds the term.

## In the Request logs screen

A query bar sits above the filter form. Both narrow the same rows, and each form filter is the same thing as a term:

| Form filter (API parameter) | Term |
|---|---|
| Model (`model`) | `model:<value>` |
| Status errors / successes (`status`) | `status:error` / `status:ok` |
| HTTP status (`status_code`) | `http_status:<code>` |
| Key (`key_id`) | `key:<id>` |
| Principal, team, provider, operation, request ID, trace ID | the field of the same name |
| End user (`end_user`), session (`session_id`) | `end_user:<value>`, `session:<value>` |
| Owner (`owner`) | `owner:<kind>:<id>` |
| Origin (`origin`) | `origin:<value>` |

The screen shows the form filters that are set as terms, and **Move into the query** writes them into the query and clears the form. The older parameters stay accepted by the API for good, so links and scripts keep working: `?model=a` and `?q=model:a` return the same page.

**Count these rows by** shows the values of one field, or of one tag in use, for the rows on screen (`GET /api/v1/admin/logs/facets`); a value adds its term. The CSV export takes the same `q`. **Create alert rule** opens a new scheduled alert rule on the requests the query matches.

A team role reads the log of one team at a time, as before.

## In an evaluator

An [evaluator](/docs/features/evaluators/#the-filter-and-sampling)'s filter is one query of the spans scope (a span evaluator) or of the traces scope (a trace or session evaluator), matched in memory when a span is stored; evaluation results and text search are not available in it. A query of the Traces screen also chooses the targets of a [run on chosen traffic](/docs/features/evaluators/#run-on-chosen-traffic).

## In an alert rule

A scheduled [alert rule](/docs/features/alert-rules/) may carry a `query` beside its filters:

```yaml
alerting:
  rules:
    items:
      - id: canary-errors
        name: Errors on the canary arm
        scope: global
        mode: scheduled
        signal: requests
        query: "canary.side:canary status:error -origin:evaluation"
        metric: count
        operator: gte
        threshold: 5
        window: 10m
```

`signal: requests` reads the logs scope, `signal: spans` the spans scope. Span fields and evaluation terms work on the requests signal as on the request log; a span rule counts spans by their own fields (`child.` and `parent.` are refused). A rule has no reader, so it never searches captured content. **Preview** shows what the query matches before you save. When a span field or an evaluation term of the query names more than 2,000 requests or traces in the rule's window, the scan fails with `query_truncated` instead of counting a part of them: narrow the term or shorten the window. If `trace_query` is turned off while such a rule exists, the rule is not scanned (its state shows `query_disabled`) rather than run without its query.

## Saved views

A saved view keeps what a screen shows: the query, the scope, the time range, the lens, the columns, the sort, the added facets and, on the request log, the form filters. Open **Views** on the Traces or the Request logs screen to open, save, update or remove one.

- **Built-in** views are ready-made questions that write their query into the bar: errors, slow (at or above the p95 of the rows on screen), costly, fell back, guardrail flagged and failed evaluations on Traces; errors, slow, rate limited, timeouts, guardrail blocked and failed evaluations on Request logs.
- The **star** makes a view your default: the view the screen opens with when you arrive without a question in the address.
- The **people** button, for whoever manages a team's or an organization's views, makes a shared view the **team's or organization's default** (`shared_default`): its members' screens open with it unless they chose a default of their own. A team or an organization has one per screen; setting another replaces it.
- The **link** button copies an address that opens the view (`#/traces?view=sv_...`, `#/logs?view=sv_...`). It works for whoever may see the view; anyone else is told the view is not shared with them.

A view holds no data. Opening one runs its query **as you**, so a shared view never shows anyone more than the screen would; if it uses a field you may not filter on, you get the refusal.

A view is yours alone, a team's or an organization's:

| Visibility | Who sees it | Who edits and removes it |
|---|---|---|
| `private` | its owner | its owner |
| `team` | the members of the team | whoever may share with that team; its author may still remove it |
| `organization` | the members of the organization's teams | whoever may share with that organization; its author may still remove it |

A shared view belongs to the team or the organization, not to the person who made it.

### Permissions

| Permission | Lets you | Built-in roles |
|---|---|---|
| `saved_views:read` | see the views of your teams and organizations, and your own | administrator, viewer, team administrator, team viewer, member |
| `saved_views:save` | keep, update and remove your own views, and choose your default | the same; not the shared viewer token and not a scoped credential, which name nobody |
| `saved_views:share` | share a view with a team you administer, and manage that team's views; an organization admin, with its organization; the administrator, with any | administrator, organization admin, team administrator |

The operator role reads no request data and holds none of them. A [custom role](/docs/features/custom-roles/) is a ceiling: it keeps or withholds each of the three, and never adds one its base role lacks.

### Organizations

The organization is the tenant. A team's view records the organization the team was in when the view was shared. If the team later moves to another organization, its members no longer see that view; the view is not deleted. An organization's view is never visible outside it. With `organizations` off, views are private or a team's.

A view keeps a query, never rows, and who may read a row is decided when the query runs. An organization administrator can therefore save a view over what a team recorded in its organization before the team left: on **Logs** with that team chosen in the form (the view is applied to the team the form names), or with `team:<team>` in the query over the whole organization (`?organization=`). The request log's `q`, its facets, and the evaluation, span and text terms inside them all read the rows the list itself shows for that reader ([when a team moves](/docs/features/organizations/#when-a-team-moves)). In the console, a team role and an organization administrator always read one team, so **Move into the query** leaves the form's team where it is.

### Limits

Each person keeps at most `trace_query.saved_views_per_owner` views (default 50) and the gateway `trace_query.saved_views_total` (default 10,000); the total is one bound for the whole gateway, not per organization. A definition is at most 16 KiB, with at most 40 columns, 12 facets and 16 filters.

## Configuration

The `trace_query` block of the configuration file bounds text search and saved views. It is read when the gateway starts; the API (`GET /api/v1/admin/query/settings`) and the query bar's help show the effective values and where each comes from, and do not change them.

```yaml
trace_query:
  text_search_max_records: 500        # 0 to 5000; 0 turns text search off
  text_search_max_record_bytes: 65536 # 1024 to 1048576
  saved_views_per_owner: 50           # 1 to 500
  saved_views_total: 10000            # 1 to 100000, at least saved_views_per_owner
```

| Parameter | Default | Bounds | What it does |
|---|---|---|---|
| `text_search_max_records` | 500 | 0 to 5,000 | Captured records one text search reads, newest first. Raise it to search further back at the cost of a slower search; `0` turns text search off |
| `text_search_max_record_bytes` | 65,536 | 1,024 to 1,048,576 | Text read of each side (sent view, answer) of one record |
| `saved_views_per_owner` | 50 | 1 to 500 | Saved views one person keeps |
| `saved_views_total` | 10,000 | 1 to 100,000 | Saved views the gateway keeps |

## API

Every endpoint takes a console session. Reads need `logs:read`; saved views their own permissions.

| Endpoint | What it does |
|---|---|
| `GET /api/v1/admin/query/fields?scope=` | The fields of a scope (`traces`, `spans`, `logs`, `rules`, `sessions`, `users`; default `traces`) this session may use, with the syntax, examples, limits and whether text search is available |
| `POST /api/v1/admin/query/validate` | Body `{"scope": "logs", "q": "..."}`. Answers `valid` with the canonical `query`, or the refusal in `error`. Reads no data |
| `GET /api/v1/admin/query/settings` | The effective `trace_query` block with the source of each value |
| `GET /api/v1/admin/traces?scope=&q=&from=&to=` | The trace explorer's list ([trace explorer](/docs/features/trace-explorer/#api)) |
| `GET /api/v1/admin/traces/facets?field=` | Facets; `field` may be any countable field of the scope |
| `GET /api/v1/admin/logs?q=`, `GET /api/v1/admin/logs/export?q=` | The request log and its CSV export |
| `GET /api/v1/admin/logs/facets?field=&q=&from=&to=&team=&status=` | The values of one field for a request log filter. `field` is required; `q`, `from`, `to`, `team` and `status` are the request log's own |
| `GET /api/v1/admin/views?surface=` | The views this session sees (`surface`: `traces` or `logs`), with what it may do (`access`) |
| `POST /api/v1/admin/views` | Keep what a screen shows as a view: private, or for a team or an organization you may share with |
| `GET`, `PUT /api/v1/admin/views/{id}` | Read a view; replace it at its `version` |
| `DELETE /api/v1/admin/views/{id}?version=` | Remove a view at its `version` |
| `PUT /api/v1/admin/views/default` | Body `{"surface": "traces", "id": "sv_..."}`; `"id": null` clears the default |

Keep a view for a team:

```bash
curl -sS -X POST "$GATEWAY/api/v1/admin/views" \
  -b cookies.txt -H "X-CSRF-Token: $CSRF" -H "Content-Type: application/json" \
  -d '{"name": "Canary errors", "visibility": "team", "team": "team_a",
       "definition": {"schema": 1, "scope": "traces", "range": "24h", "lens": "overview",
                      "query": "canary.side:canary status:error",
                      "columns": ["first_at", "root_name", "duration", "cost"]}}'
```

```json
{"id": "sv_3f6c0d9a41b27e85c0d1a2b3", "surface": "traces", "name": "Canary errors",
 "visibility": "team", "team": "team_a", "organization": "org_acme", "owner": "usr_...",
 "own": true, "version": 1, "default": false, "can_edit": true, "can_delete": true,
 "definition": {"schema": 1, "scope": "traces", "query": "canary.side:canary status:error", "range": "24h", "lens": "overview", "columns": ["first_at", "root_name", "duration", "cost"]}}
```

`range` is `15m`, `1h`, `6h`, `24h`, `7d`, `30d` or `custom` with `from` and `to`. Answers: `403 role_forbidden` (no permission for that team or organization), `404 view_not_found` (a view you do not see), `409 version_conflict`, `409 view_capacity`, `422 invalid_view_definition` (with the reason: an unknown field, a bad range).

A validation:

```bash
curl -sS -X POST "$GATEWAY/api/v1/admin/query/validate" -b cookies.txt -H "X-CSRF-Token: $CSRF" \
  -H "Content-Type: application/json" -d '{"scope": "traces", "q": "status:error AND (model:a or model:b)"}'
```

```json
{"valid": true, "scope": "traces", "query": "status:error model:(a OR b)",
 "fields": ["model", "status"], "uses": {"evaluation": false, "text": false, "spans": false}}
```

## Both storage engines

The language reads the same on PostgreSQL and on the analytics store. One compiler turns a parsed query into a statement for either, from the same table of fields, and a suite of over 200 queries checks that both answer exactly what a reference matcher in the gateway answers, for an administrator and for readers of each of two organizations.

What to expect of cost: a term on a trace or a span reads the same tables the list reads. A span field in the traces scope, and `child.` and `parent.`, add one lookup of the spans of the time range per term; narrow the time range first when the range is wide. Reads that take more than 10 seconds answer `503 query_timeout` and ask for a shorter range or one more filter. See [query language reads](/docs/performance/query-language/) for the measured cost of each form on both engines, and [trace explorer performance](/docs/performance/trace-explorer/) for the explorer's own reads.

## Upgrade

`trace_query` adds two tables for saved views (`gateway_saved_views`, `gateway_saved_view_defaults`) in the migration that ships with it; nothing else changes shape. The request log's older parameters are unchanged. Queries the trace explorer already ran (terms joined by AND) mean the same with the feature on or off.

The Usage screen has its own [saved analytics views](/docs/features/analytics-saved-views/) (dashboards of widgets); they are a different thing and stay as they are.
