Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
77ab107
feat(traceql): split search into a search and a data statement
44YHC Oct 29, 2026
18f1d31
test(traceql): check search results on single node and two shards
44YHC Oct 29, 2026
ddaf4db
feat(traceql): split metrics and compare() into ids and metrics state…
44YHC Oct 30, 2026
ecd565c
test(traceql): check metrics and compare() results on single node and…
44YHC Oct 30, 2026
3221b1f
feat(traceql): split tag keys and values into ids and attributes stat…
44YHC Oct 30, 2026
17051d2
test(traceql): check tag keys and values on single node and two shards
44YHC Oct 30, 2026
37002da
perf(traceql): relate structural operators over one pointer jump per …
44YHC Oct 30, 2026
66ec682
perf(traceql): run structural search statement 1 on each shard
44YHC Oct 30, 2026
5611805
perf(traceql): evaluate a repeated operand and the pivot read once
44YHC Oct 30, 2026
0380a2f
docs(traceql): document ClickHouse jemalloc decay and 24h memory sizing
44YHC Oct 30, 2026
df3996d
perf(traceql): run structural ids statements on each shard
44YHC Oct 30, 2026
13257f5
perf(traceql): count compare() attribute values on each shard
44YHC Oct 30, 2026
2f9f806
Merge feat/traceql-nested-set into feat/traceql-drilldown-reads
44YHC Nov 6, 2026
e26e9c9
fix(traceql): skip unresolved traces on the one-jump walk
44YHC Nov 6, 2026
0c77086
feat(traceql): warn at start-up when a read suffix has no read cluster
44YHC Nov 6, 2026
8403865
feat(traceql): count the portion budget on each shard
44YHC Nov 6, 2026
3ffeb12
feat(traceql): default the per-shard portion budget to 7500000
44YHC Nov 6, 2026
da7abb8
test(traceql): cover the rank on shard-local searches
44YHC Nov 6, 2026
4f06f47
fix(traceql): fail a request when its estimate read fails
44YHC Nov 6, 2026
38abd16
docs(traceql): document reader memory, ClickHouse access and shard re…
44YHC Nov 6, 2026
c8a6bf0
test(traceql): cover read-only reader users on search and autocomplete
44YHC Nov 6, 2026
ad47505
test(traceql): cover read-only reader users on metrics and compare()
44YHC Nov 6, 2026
2cab6a0
docs(traceql): state what a refused unresolved-trace probe prints and…
44YHC Nov 6, 2026
307891e
feat(traceql): default the per-shard portion budget to 5000000
44YHC Nov 8, 2026
5389420
feat(traceql): portion autocomplete by trace_id range on every read
44YHC Nov 8, 2026
ce6d32a
test(traceql): portioned autocomplete equals the unportioned statement
44YHC Nov 8, 2026
cf8bd29
docs(traceql): state that autocomplete portions are trace_id ranges
44YHC Nov 8, 2026
97845dd
test(traceql): seed spread trace ids where autocomplete runs in portions
44YHC Nov 8, 2026
7568d9d
test(traceql): check on ClickHouse that portions partition stored tra…
44YHC Nov 8, 2026
a361f82
fix(traceql): bound the ancestry tree read an hour past the query range
44YHC Nov 8, 2026
2d71ec7
feat(traceql): answer duration-only autocomplete in one statement
44YHC Nov 8, 2026
779a11a
Merge branch 'feat/traceql-nested-set' into feat/traceql-drilldown-reads
44YHC Nov 9, 2026
db4b57a
Merge branch 'feat/traceql-nested-set' into feat/traceql-drilldown-reads
44YHC Nov 9, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 88 additions & 13 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ The container image is published to `ghcr.io/metrico/gigapipe:latest` with multi

- **`CLUSTER_NAME`** - Enables clustered mode and sets the cluster name. When set, gigapipe operates in distributed mode.
- **`CLICKHOUSE_READ_DIST_SUFFIX`** - Suffix for read-path distributed tables (default: `_dist`). Used for cross-cluster reads in multi-cluster deployments. See [Cross-Cluster Deployment](#cross-cluster-deployment) below.
- **`CLICKHOUSE_READ_CLUSTER`** - Cluster the read-path distributed tables span. It applies only when it differs from `CLUSTER_NAME` and `CLICKHOUSE_READ_DIST_SUFFIX` is set to a suffix other than `_dist`. Set it on readers too: TraceQL structural queries and `compare()` then run on each shard of this cluster, else of `CLUSTER_NAME` (see [Requirements](#requirements)).

## Environment variable names

Expand Down Expand Up @@ -105,7 +106,7 @@ Variables that are not Gigapipe-specific — `CLICKHOUSE_*`, `PORT`, `HOST`,
## TraceQL

- **`GIGAPIPE_TRACEQL_ANCESTRY_ROUNDS`** - Number of rounds used to resolve span ancestry for TraceQL structural operators, an integer from `1` to `32` (default: `20`). With `R` rounds, traces up to `2^R` spans deep resolve. Spans of deeper traces, and of traces whose parent links form a cycle, are treated as unreachable, and a warning names those traces. Each round adds work to every query using `>>` or `<<`. An invalid value stops Gigapipe at start-up. See [Depth limit](#depth-limit).
- **`GIGAPIPE_TRACEQL_PORTION_SPANS`** - Most spans a TraceQL search or tag autocomplete reads per portion, a positive integer (default: `2500000`). A request runs in `ceil(estimate / setting)` portions. Queries with a structural operator estimate every span in range; a lone `{}` estimates the search limit; others estimate the spans their selectors match. Memory grows with spans per portion, and every portion rescans the range, so a lower value costs time. Metrics queries are not portioned. Set ClickHouse `max_memory_usage` as the backstop. An invalid value stops Gigapipe at start-up. See [Large queries](#large-queries).
- **`GIGAPIPE_TRACEQL_PORTION_SPANS`** - Most spans a TraceQL search or tag autocomplete reads per shard in one portion, a positive integer (default: `5000000`, which assumes [`MALLOC_CONF`](#clickhouse-memory)). A request runs in `ceil(estimate / setting)` portions. Queries with a structural operator estimate every span in range on the fullest shard, a single node being one shard; a lone `{}` estimates the search limit; others estimate the spans their selectors match across the cluster. Memory grows with spans per portion, and every portion rescans the range, so a lower value costs time. Metrics queries are not portioned. Set ClickHouse `max_memory_usage` as the backstop. An invalid value stops Gigapipe at start-up. See [Large queries](#large-queries).
- **`GIGAPIPE_TRACEQL_SEARCH_MAX_DURATION`** - Longest time range of a TraceQL search or tag autocomplete, a Go duration such as `168h` (default: `168h`). A longer request gets HTTP 400 with the plain-text body `range specified by start and end exceeds 168h0m0s. received start=<s> end=<e>`, as Tempo sends it. `0` turns the cap off. Applies to `/api/search` with `q`, and to `/api/v2/search/tags` and `/api/v2/search/tag/{tag}/values` with a non-zero `start`. Metrics queries, `/api/search` with `tags`, and tag lookups that read no time range are not capped. An invalid or negative value stops Gigapipe at start-up. See [Large queries](#large-queries).

Structural TraceQL queries need ClickHouse 24.9 or newer; see [Requirements](#requirements).
Expand All @@ -122,7 +123,19 @@ span tree is resolved at query time from the `parent_id` of the stored spans.
Clustered (Distributed) deployments return the same results as single-node
ones, provided **a trace's spans and attribute rows live on one shard**.
Writers inserting through the `_dist` tables place them so: the sharding key is
`sipHash64(oid, trace_id)`, and a trace has one `oid`.
`sipHash64(oid, trace_id)`, and a trace has one `oid`. A structural query
(search, metrics, `compare()`, tag autocomplete) resolves relations on each
shard over that shard's spans; a split trace loses the relations between its
shards' spans. Search keeps the newest traces of the shards' results and lists
a split trace once, with one shard's spansets. `compare()`, structural or not,
counts attribute values on each shard over that shard's traces, and the
connected ClickHouse node sums the shards' counts. The nested-set numberer has
the same requirement.
For the ClickHouse memory setting, see [ClickHouse memory](#clickhouse-memory).

On a cluster, TraceQL queries need the ClickHouse analyzer: keep
`enable_analyzer` at its default of 1 for the reader's user, including through
`compatibility`.

The nested-set intrinsics read values that a background numberer stores, which
needs ClickHouse 24.9 or newer; see [The numberer](#the-numberer).
Expand All @@ -139,9 +152,13 @@ The span tree is built at query time from stored `parent_id`s:
- A span whose ancestor hasn't been ingested yet starts matching once that
ancestor arrives.
- When a trace has several roots, they are all siblings under `~`.
- The span tree is read up to 1 hour past the end of the query range, or of a
`compare()` baseline ending later, so a parent starting more than 1 hour
after that end counts as missing.

Ancestry is resolved in a fixed number of rounds, set by
`GIGAPIPE_TRACEQL_ANCESTRY_ROUNDS`. With `R` rounds, traces up to `2^R` spans
Ancestry is resolved in up to `R` rounds, set by
`GIGAPIPE_TRACEQL_ANCESTRY_ROUNDS`. Each trace takes one round per doubling of
its spans found so far. With `R` rounds, traces up to `2^R` spans
deep resolve. The default of 20 covers traces about a million spans deep.

A span whose ancestry is still unresolved after the last round is either in
Expand Down Expand Up @@ -184,7 +201,7 @@ The nested-set numberer computes `nestedSetLeft`, `nestedSetRight` and `nestedSe

- **`GIGAPIPE_NESTED_SET_NUMBERER_IDLE`** - How long a trace must receive no spans before it is numbered, as a Go duration (default: `10s`).
- **`GIGAPIPE_NESTED_SET_NUMBERER_INTERVAL`** - Time between numbering passes, as a Go duration (default: `5s`).
- **`GIGAPIPE_NESTED_SET_NUMBERER_MAX_SPANS`** - Maximum number of spans one pass numbers, a positive integer (default: `200000`). The oldest traces go first and the rest wait for the next pass; a single larger trace is numbered alone. On a cluster each shard numbers its own traces and the cap applies per shard.
- **`GIGAPIPE_NESTED_SET_NUMBERER_MAX_SPANS`** - Maximum number of spans one pass numbers, a positive integer (default: `200000`). The oldest traces go first and the rest wait for the next pass; a single larger trace is numbered alone. On a cluster each shard numbers its own traces and the cap applies per shard, so a trace's spans must live on one shard (see [Requirements](#requirements)).

- **Who takes part.** Every instance in `all` or `writer` mode takes part, and ClickHouse elects one of them to run the passes through a lease in the `tempo_traces_nested_set_lease` table. If the leader stops, or its passes keep failing, another takes over within 4 × `GIGAPIPE_NESTED_SET_NUMBERER_INTERVAL`, so values can lag that much longer. `reader` instances never take part.
- **Clocks.** Each instance judges the lease by the clock of the ClickHouse node it connects to, so on a cluster node clocks must agree to well within 3 × the interval. On a replicated cluster they must also agree within 2s and well within the idle period. NTP is enough.
Expand Down Expand Up @@ -214,12 +231,31 @@ LIMIT 1
### Large queries

A TraceQL search or tag autocomplete whose estimate exceeds
`GIGAPIPE_TRACEQL_PORTION_SPANS` runs in portions. Each portion reads one hash
bucket of traces over the whole requested range. For a search, each portion
returns its bucket's top `limit` traces and Gigapipe merges them by newest
matched span, so the result is the same at any portion count. Each portioned
request logs one info-level `traceql portioned` line with the query, range,
estimate, setting and portion count.
`GIGAPIPE_TRACEQL_PORTION_SPANS` runs in portions. Each search portion reads one
hash bucket of traces over the whole requested range. Each autocomplete portion
reads a slice of the `trace_id` space in every read of its statements, so
`tempo_traces` reads narrow by the sort key; attribute index reads still scan
the whole requested range. Autocomplete portions balance when trace ids are
uniformly distributed; ids with a time prefix concentrate in few portions. For a
search, each portion returns its bucket's top `limit` traces and Gigapipe merges
them by newest matched span, so the result is the same at any portion count.
Each portioned request logs one info-level `traceql portioned` line with the
query, range, estimate, setting and portion count. For a structural query, the
estimate is the fullest shard's.

Tag autocomplete with a lone selector whose filter compares only `duration` or
`span:duration` (combined with `&&` and `||`, and any `select()` stages), such
as `{duration > 100ms}`, is one statement over the attribute index, with no
estimate and no portions. The filter applies to each attribute row, so the
statement holds in memory as many values as match it: up to one per distinct
key for tags, or per distinct value of the requested key for tag values. All
of a range's distinct values are held at once, on the initiator on a cluster,
so a tag-value request for a high-cardinality key, such as a URL or an id, can
fail on `max_memory_usage`. A span stored more than once with different
durations suggests only the attributes of the copies whose duration passes, and
with `&&` over terms that different copies meet it can suggest none. Other
filters, including `{.a = 1 || duration > 0}`, keep their ids stage and
portions.

A search response reports its portions in a `metrics` block after the trace
array: `"metrics":{"totalJobs":N,"completedJobs":N}`, with `N` the portion count,
Expand All @@ -234,12 +270,50 @@ of exhausting the node. The accepted risks are:

- A huge single trace isn't split. All its spans fall in one portion, whatever
the setting.
- The budget is per query. Concurrent queries each read up to it.
- The default is sized for 7 GiB nodes. Lower it on smaller ones.
- The budget is per query, and per shard for structural queries. Concurrent
queries each read up to it.
- The default is sized for 7 GiB nodes and assumes
[`MALLOC_CONF`](#clickhouse-memory). Lower it on smaller nodes or without the
variable.
- Cost grows with range × portions, because every portion rescans the range.
- The reader holds up to `2 × limit` traces while it merges portions, which
`max_memory_usage` doesn't cover.

### ClickHouse memory

Run the ClickHouse server with this variable in its environment (an environment variable, not a config key):

```
MALLOC_CONF=background_thread:true,dirty_decay_ms:1000,muzzy_decay_ms:0
```

It makes ClickHouse return freed memory to the OS within about a second, so repeated large queries start from a low RSS instead of tripping the server memory limit (code 241).

It matters most for 24h structural TraceQL queries (search including Grafana Traces Drilldown, metrics, `compare()`, autocomplete). Without it, plan more memory per node.

The reader holds the IDs of a TraceQL metrics, `compare()` or filtered tag autocomplete request in memory (`readIds`): `24 + 8 x spans` bytes per trace, uncapped, for the whole request, or for each portion of a portioned autocomplete. `compare()` holds its baseline and selection tables at once. Reading and sending them allocates more on top.

`max_result_rows` on the reader's ClickHouse user caps the IDs statement, and it counts traces, not span IDs. With `result_overflow_mode=throw` (the default) a request over the cap gets a 500. **Don't use `break`:** it truncates the IDs silently, so the request returns 200 with wrong results: wrong counts from `compare()` and metrics, missing suggestions from autocomplete.

### ClickHouse access

On a cluster, the reader's ClickHouse user needs `SELECT` on the database, including the local tables, not only `_dist`, plus `REMOTE ON *.*`:

```sql
GRANT SELECT ON <db>.* TO <user>;
GRANT REMOTE ON *.* TO <user>;
```

Without a `<secret>` in `remote_servers`, shards run the query as that cluster's `<user>`. With `<secret>`, the initial user runs on every shard, so it must exist on every shard with `SELECT`.

For a `readonly=1` user on a cluster:

Structural TraceQL metrics, autocomplete and `compare()` fail with code 164 (`Cannot modify 'prefer_localhost_replica' setting in readonly mode`), and where the read tables skip unavailable shards, so do structural searches and every `compare()`.

Without `prefer_localhost_replica=0`, a structural search that succeeds logs no [unresolved-trace warning](#depth-limit): the warning's probe changes that setting. Each refused probe prints the code 164 error to stdout and resets the reader's connection pool, at most once a minute.

Set `prefer_localhost_replica=0`, and `skip_unavailable_shards=1` if the read tables skip unavailable shards, in the user's settings, or allow them with `CHANGEABLE_IN_READONLY`, or use `readonly=2`. Setting them on the user applies to all of that user's Distributed reads. Over HTTP, also set `cancel_http_readonly_queries_on_client_close=1` on the user, so the server stops a read when the reader stops waiting.

## Recording Rules (Ruler)

The ruler evaluates LogQL and PromQL recording rules on a schedule and writes
Expand Down Expand Up @@ -284,6 +358,7 @@ MODE=reader
CLUSTER_NAME=local_cluster
CLICKHOUSE_SERVER=clickhouse-local.example.com
CLICKHOUSE_READ_DIST_SUFFIX=_dist_cross_cluster
CLICKHOUSE_READ_CLUSTER=cross_cluster_name
```

In this setup:
Expand Down
22 changes: 17 additions & 5 deletions reader/logql/logql_transpiler/shared/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,30 @@ type RequestProcessor interface {
Process(*PlannerContext, chan []LogEntry) (chan []LogEntry, error)
}

// RandomFilter is portion I of Max portions of the traces; Max 0 is every trace.
type RandomFilter struct {
Max int
I int
// TraceIDRanges cuts the portions into trace_id ranges, not hash buckets.
TraceIDRanges bool
}

type PlannerContext struct {
IsCluster bool
From time.Time
To time.Time
OrderASC bool
Limit int64
Offset time.Duration
// ClusterName is the cluster the Distributed tables span; empty on a single node.
ClusterName string
// SkipUnavailableShards is whether the read Distributed tables skip unavailable shards.
SkipUnavailableShards bool
// DistSuffix ends the name of each Distributed table on a cluster.
DistSuffix string
From time.Time
To time.Time
// QueryTo is the end of the request's range, set once per request. Planners
// that narrow From and To leave it unchanged. Zero means it is unknown.
QueryTo time.Time
OrderASC bool
Limit int64
Offset time.Duration
// SpansPerSpanSet caps the spans listed per TraceQL search spanset; 0 means the default.
SpansPerSpanSet int64
// AncestryRounds is the pointer-jumping rounds of TraceQL structural
Expand Down
10 changes: 8 additions & 2 deletions reader/plugins/traces_planners.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ func GetTracesDataPlugin() *TracesDataPlugin {

// AttrlessConditionPlannerPlugin and InitIndexPlannerPlugin replace only the
// selector reads in attrless.go and init.go. all_spans.go, field_filter.go,
// the traceTree edge read and selectAttrs read the tables directly, so these
// hooks don't scope the rows of those reads. An AttrlessConditionPlannerPlugin
// the traceTree edge read, selectAttrs and RowAttrsPlanner read the tables directly,
// so these hooks don't scope the rows of those reads. RowAttrsPlanner runs only
// when no InitIndexPlannerPlugin is registered. An AttrlessConditionPlannerPlugin
// reads only the traces of ctx.RandomFilter: portions are merged without a dedupe.
type AttrlessConditionPlannerPlugin func() shared.SQLRequestPlanner

Expand Down Expand Up @@ -71,7 +72,12 @@ type InitIndexPlannerPlugin func() shared.SQLRequestPlanner

var initIndexPlannerPlugin *InitIndexPlannerPlugin

// RegisterInitIndexPlannerPlugin sets the hook; nil clears it.
func RegisterInitIndexPlannerPlugin(plugin InitIndexPlannerPlugin) {
if plugin == nil {
initIndexPlannerPlugin = nil
return
}
initIndexPlannerPlugin = &plugin
}

Expand Down
5 changes: 5 additions & 0 deletions reader/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/metrico/qryn/v5/reader/utils/logger"
"github.com/metrico/qryn/v5/reader/utils/middleware"
"github.com/metrico/qryn/v5/reader/watchdog"
"github.com/metrico/qryn/v5/shared/distconfig"
)

// Init registers the reader routes on app. With a nil app the reader builds
Expand All @@ -34,6 +35,10 @@ func Init(cnf *clconfig.ClokiConfig, app *http.ServeMux) {
//
logger.InitLogger()

if w := distconfig.CrossClusterWarning(distconfig.Suffix(), distconfig.ReadCluster()); w != "" {
logger.Warn(w)
}

if app != nil {
performV1APIRouting(app)
return
Expand Down
Loading
Loading