Structured Trace Query Filters#
What It Is#
Jaeger's structured query filter is a typed boolean expression AST that replaces the old flat key=value conjunction with a predicate model that can target specific OTLP attribute levels, use richer operators (gt, regex, in, โฆ), and compose predicates with AND/OR/NOT. It is defined in RFC 0005 and shipped incrementally beginning with Jaeger v2.21.0 (Alpha).
The filter is not a text query language โ the wire format is a proto/JSON structure (Expression + Call). A text prefix shorthand (resource.k:v, duration>2s) is a convenience layer that normalizes to the same AST.
AST Structure#
The AST is a single recursive Expression, one of six terms:
| Term | Proto field | Purpose |
|---|---|---|
AttributeRef | attr | An attribute-map entry at an optional level |
FieldRef | field | A built-in span/resource/event field (duration, name, status, โฆ) |
NestedRef | nested | The events or links collection; first operand of some only |
Scalar | scalar | A single constant, optionally typed (string|int|double|bool) |
List | list | A homogeneous list; right operand of in/not_in |
Call | call | An operator applied to argument Expressions |
The top-level filter field on TraceQueryParameters is a Call (not the broader Expression) because a filter is always a boolean operator at the root. A single predicate is the Call directly; a conjunction is an explicit and call. The filter field is mutually exclusive with the legacy predicate fields (service_name, operation_name, duration_min/max, attributes) โ mixing them is rejected with InvalidArgument.
Attribute levels#
There are five levels matching OTLP's hierarchy. An empty level defaults to span-or-resource.
| Level | OTLP location |
|---|---|
span | Span.attributes |
resource | ResourceSpans.resource.attributes |
scope | ScopeSpans.scope.attributes |
event | Span.events[].attributes |
link | Span.links[].attributes |
Operators#
eq, ne, gt, lt, gte, lte, regex (RE2, anywhere-match), exists, in, not_in, some (correlated quantifier over event/link collections), plus boolean and/or/not.
Built-in fields#
The vocabulary is closed, enumerated per level. Examples: span.duration, span.name, span.status, span.kind, resource.service, event.name, event.timeSinceStart. A name outside the registry is refused at the query boundary.
Filter Lifecycle: Validate โ Resolve โ Check Capabilities#
FinalizeFilter runs two mandatory stages in order:
- Validate โ Every
op,level, valuetype, and built-in field name must belong to the defined closed set; quantifier binding rules hold. - Resolve constants โ An unhinted constant beside a built-in field is rewritten to that field's declared type (
"2s"โDurationValueforspan.duration). A value that won't parse is refused here.
Both stages run at every ingress: the query service on what a caller sent, again on what a query interceptor returns, and the remote-storage server on what a client sends it.
A third stage, capability check, gates the filter against what the target reader declared. FilterCapabilities.EnsureSupported walks the tree and returns an error for the first unsupported operator or unindexed level.
Capability Declarations#
Each tracestore.Reader implements SearchCapabilities(ctx) (SearchCapabilities, error). The struct includes:
Filter *FilterCapabilitiesโ nil or empty means "legacy predicate fields only; no structured filter."SameSpanConjunction boolโtruewhen every conjunct is matched within a single span;falsemeans intersection at trace granularity (flat inverted index). This is reported, not enforced.WithoutServiceName boolโ whether search may omit the service name.
FilterCapabilities carries two slices that act as refusal gates:
type FilterCapabilities struct {
Levels []expression.Level // e.g. "span", "resource", "event"
Operators []expression.Operator // e.g. "and", "eq", "ne", "gt", "regex"
}
A filter naming an unlisted level or operator is rejected with Unimplemented (HTTP 501). Boolean combinators are ordinary operators here โ a conjunctive-only backend lists and and omits or/not.
Cassandra and Badger: The Flat-Index Boundary#
Both backends wrap a v1 spanstore.Reader via v1adapter.TraceReader, which returns SearchCapabilities with no Filter field. This means:
- The query service never dispatches a
filterto them โ it down-converts to legacy fields or refuses predicates that can't be expressed in them. - Their flat tag index covers only
span,resource(process), andevent(log-field) attributes, merged into one undifferentiated index.linkattributes are dropped on write;scopeattributes are collapsed into span tags. SameSpanConjunctionisfalse: a conjunction may match across spans of the same trace.
The Cassandra v2 reader explicitly returns SearchCapabilities{WithoutServiceName: false} with no Filter field. Badger reaches the same state via v1adapter since its factory delegates to v1adapter.NewTraceReader.
The integration test suite captures this boundary in capabilities.Cassandra() and capabilities.Badger(), both of which skip FindTracesWithFilter, scopeAttributesTest, and linkAttributesTest.
Backend Filter Support Summary#
| Backend | Levels declared | Operators declared | SameSpanConjunction | Native filter |
|---|---|---|---|---|
| Elasticsearch / OpenSearch | span, resource, event | and/or/not + eq/ne/gt/lt/gte/lte/regex/exists/in/not_in | true | โ (M4 delivered) |
| ClickHouse | all 5 | full set | true | pending (M3) |
| Cassandra | โ (legacy only) | โ (legacy only) | false | โ |
| Badger | โ (legacy only) | โ (legacy only) | false | โ |
| Memory | all (no refusals) | all (no refusals) | true | โ |
Key Source Files#
| File | Purpose |
|---|---|
docs/rfc/0005-structured-query-filters.md | Full design rationale, AST spec, operator and field rules |
internal/storage/v2/api/tracestore/capabilities.go | SearchCapabilities and FilterCapabilities structs |
internal/storage/v2/api/tracestore/admission.go | EnsureFilterStandsAlone, FilterCapabilities.EnsureSupported |
internal/storage/v2/api/tracestore/filter_finalize.go | FinalizeFilter (validate + resolve constants) |
internal/storage/v2/cassandra/tracestore/reader.go | Cassandra's SearchCapabilities (no filter caps) |
internal/storage/v2/badger/factory.go | Badger factory (delegates to v1adapter) |
internal/storage/integration/capabilities/capabilities.go | Per-backend integration test capability opt-outs |
internal/storage/v2/elasticsearch/tracestore/core/filter.go | ES/OS filter lowering |
jaeger-idl/query/expression/v1 | Go AST types (expression.Level, Operator, Call, Expression, โฆ) |
Feature Gate#
The filter is gated by jaeger.query.structuredFilters (Beta, enabled by default). While the gate is off, caller-supplied filters are refused outright; internal query interceptors still receive filter-shaped queries regardless of the gate. A per-backend gate jaeger.<backend>.structuredFilters controls whether a backend evaluates a filter natively.