$ s2b aggregate --help
Stream aggregation into your own bucket
Calculate counts, sums, averages and percentiles as records arrive. For example, a rule can write one row per route per minute with its p99 latency. Choose whether to keep the raw records alongside the results.
- [ok] counted in the same durable write as the batch, before 2xx
- [ok] windows of 10s, 30s, 1m, 5m, 15m
- [ok] percentiles within 1% of the exact value
- [ok] on every plan, built in the dashboard
{"event": "order.paid", "country": "SG", "plan": "pro", "cents": 6000}
{"event": "order.paid", "country": "SG", "plan": "pro", "cents": 4250}
{"event": "order.paid", "country": "US", "plan": "free", "cents": 5000}
{"event": "order.paid", "country": "SG", "plan": "free", "cents": 4250}
{"event": "order.paid", "country": "US", "plan": "pro", "cents": 500}
{"event": "order.paid", "country": "SG", "plan": "pro", "cents": 1000}
{"event": "order.paid", "country": "US", "plan": "pro", "cents": 3500}
{"rule": "revenue_by_country", "source_stream_id": "4f1c2b9e-8d3a-4e7b-9c51-0a6f2d8e3b17", "window_start": "2026-11-23T08:52:00Z", "window_end": "2026-11-23T08:53:00Z", "group": {"country": "SG"}, "count": 4, "sum": 15500, "avg": 3875}
{"rule": "revenue_by_country", "source_stream_id": "4f1c2b9e-8d3a-4e7b-9c51-0a6f2d8e3b17", "window_start": "2026-11-23T08:52:00Z", "window_end": "2026-11-23T08:53:00Z", "group": {"country": "US"}, "count": 3, "sum": 9000, "avg": 3000}how it works
Rules run on each request before Send2Bucket answers, and rows ship like any other batch.
request ──▶ rules ──▶ window state ──▶ aggregate rows ──▶ bucket or table │ (with the batch) (window ended) └──▶ raw records, unless every rule they match drops them
- match each record against the stream's rules: a signal, field values and the fields to group by.
- count it into its window in the same durable write as its batch, so an acknowledged record counts once.
- deliver one row per window and group about a second after the window ends.
what lands in your bucket
Keep every record and add the rollups you query most, or keep only the rollups of high-volume records. Usage is metered after rules run, so dropped records are not billed.
| rules a record matches | raw record |
|---|---|
| no rule | delivered |
| only rules with keep_input: false | dropped |
| at least one rule with keep_input: true | delivered |
# a rule with output_stream_id writes under that stream
<metrics prefix>/s2b/stream=<metrics stream>/
date=YYYY-MM-DD/hour=HH/
<delivery_batch_id>.ndjson.gzquery the rows
Rows are records with signal aggregate, so the engine that reads your raw records reads them too. See the Stream aggregation reference for every field.
SELECT (data->>'$.window_start')::TIMESTAMPTZ AS window_start,
data->'$.group' AS grp,
(data->>'$.count')::INTEGER AS count
FROM records
WHERE signal = 'aggregate'
AND data->>'$.rule' = 'revenue_by_country'
ORDER BY window_start;frequently asked questions
What does stream aggregation do?
Rules count the records of a stream into fixed time windows as Send2Bucket accepts them, grouped by fields you name, and deliver one row per window and group to your bucket or table: count, sum, min, max, avg, last and percentiles.
Which window lengths can a rule use?
10s, 30s, 1m, 5m, 15m. Windows are aligned to the interval in UTC, follow acceptance time and are at most 15 minutes long.
Which records can a rule aggregate?
JSON events, OpenTelemetry logs and spans, and gauge and sum metric points. Histograms, exponential histograms and summaries, and records sent to the log drain endpoints, are always delivered raw.
Can I keep only the aggregates and drop the raw records?
Yes, per rule. A record is dropped only when every rule it matches sets keep_input to false. A record that no rule matches is always delivered.
Does stream aggregation lower my Send2Bucket bill?
Yes. Usage is counted after rules run, on the raw records left for delivery, so dropping raw records lowers your Send2Bucket bill as well as storage and query costs at your destination. Aggregate rows are billed by the size of their data when their window closes.
How accurate are the counts and percentiles?
An acknowledged record is counted once, also across restarts, and each percentile is an estimate within 1% of the exact value. A client that resends a request after a timeout is counted twice, as its records are stored twice.
Can aggregate rows go to a different bucket?
Yes. A rule can send its rows to another stream of the workspace, and so to that stream’s bucket or table, for example one with a longer retention.