Browse docs
On this page

stats command

Aggregate matching events - count, sum, avg, min, max, latest value, percentiles, distinct counts, distinct values.

All ObiQL commands

Calculates aggregate statistics, such as a count, sum or percentile, over the events that match the search. Without a by clause it returns a single row. With one, it returns one row for each distinct combination of the by fields.

After stats, the result is the aggregate table rather than events. Later stages work with its columns: the by fields and one column per aggregation.

Syntax

ObiQL
| stats <function>(<field>) [AS <name>] [, <function>(<field>) [AS <name>] ...] [by <field> [, <field> ...]]
| stats <function>(<field>) <operator> <value>

Square brackets mark an optional part, and angle brackets a value you supply.

Arguments

ArgumentRequiredDescription
<function>(<field>)RequiredAn aggregation from the list below. count() takes no field, and every other function needs one. Separate several aggregations with commas.
AS <name>OptionalNames the result column. Without it the column is named after the call, such as count or avg_duration_ms. A quoted name may contain spaces: AS "Total Errors".
by <field>, ...OptionalGroups the results by one or more fields, separated by commas or spaces.
<operator> <value>OptionalA shorthand threshold on the first aggregation. | stats count() > 50 is the same as | stats count() | where count > 50, and is the usual shape for an alert condition.

Aggregation functions

FunctionReturnsSummary
countnumberNumber of matching events in each group.
sumnumberTotal of a numeric field.
avgnumberMean of a numeric field.
minnumberSmallest value of a field.
maxnumberLargest value of a field.
latestnumberMost recent numeric value of a field.
p50numberMedian (50th percentile) of a numeric field.
p95number95th percentile of a numeric field.
p99number99th percentile of a numeric field.
distinct_countnumberNumber of distinct values of a field.
valuestextThe distinct values of a field, sorted.

count()

Counts the events, or rows, in each group. It takes no field: to count the different values a field holds, use distinct_count(field).

Returns: number

ObiQL
| stats count() by service

sum(<field>)

Adds up a numeric field across each group. Values that are not numbers are skipped rather than counted as zero.

Returns: number

ObiQL
| stats sum(bytes) AS total_bytes by host

avg(<field>)

The arithmetic mean of a numeric field across each group, ignoring events that do not carry a number for it.

Returns: number

ObiQL
| stats avg(duration_ms) by route

min(<field>)

The smallest value of a numeric field in each group. On the timestamp it gives the earliest event's time.

Returns: number

ObiQL
| stats min(duration_ms) by service

max(<field>)

The largest value of a numeric field in each group. On the timestamp it gives the newest event's time.

Returns: number

ObiQL
| stats max(memory_used_mb) by host

latest(<field>)

The field's numeric value on the newest event that carries one. Use it for a gauge, such as queue depth or free disk, where the current reading matters more than the peak.

Returns: number

ObiQL
| stats latest(queue_depth) by queue

p50(<field>)

The median: half the values in each group are at or below it. Less affected by a few extreme values than avg(). Percentiles are calculated as close estimates.

Returns: number

ObiQL
| stats p50(duration_ms) by route

p95(<field>)

The value 95% of the group is at or below. The usual measure of what a slow but not unusual request looks like.

Returns: number

ObiQL
| stats p95(duration_ms) by route

p99(<field>)

The value 99% of the group is at or below. Shows the tail latency a small share of requests still sees.

Returns: number

ObiQL
| stats p99(duration_ms) by service

distinct_count(<field>)

How many different values a field takes in each group, such as the number of users or hosts affected. The count is a close estimate on very large groups.

Returns: number

ObiQL
| stats distinct_count(user_id) AS users by country

values(<field>)

The group's distinct values of a field as one comma-separated piece of text, deduplicated and sorted, up to 100 values per group. Events without the field are skipped. The only aggregation that returns text rather than a number.

Returns: text

ObiQL
| stats values(host) by service

Usage

  • Every aggregation returns a number except values(), which returns text.
  • Numeric functions skip values that are not numbers rather than treating them as zero.
  • After stats, refer to an aggregate by its AS name, its column name (count) or the way it was written (count()). For example: | stats count() by repository | fields repository, count().
  • extract and json must come before stats. Once events are aggregated, their raw text is no longer available.
  • To aggregate into time buckets for a chart, use timechart instead.

Examples

1. Count errors per service

ObiQL
level="error" | stats count() by service

Returns one row per service with the number of error events it logged.

2. Several statistics in one pass

ObiQL
service=checkout-api | stats count(), avg(duration_ms), p95(duration_ms) by route

Request count, mean latency and 95th percentile latency for every route, side by side.

3. Give a column a readable name

ObiQL
level="error" | stats count() AS "Total Errors" by service | sort -"Total Errors"

Names the count column and sorts by it, busiest first.

4. How many users each country had

ObiQL
event=login | stats distinct_count(user_id) AS users by country

Counts distinct users rather than login events.

5. Alert when errors pass a threshold

ObiQL
service=checkout-api level="error" | stats count() > 50

Returns a row only when there are more than 50 matching errors in the time range.

6. Compute a ratio of two aggregates

ObiQL
level="error" | stats count() AS errors, distinct_count(host) AS hosts | eval per_host = errors / hosts

An eval after stats works on the aggregate's own columns.