Count Total Events

Count total events using the stats() function

Query

flowchart LR; %%{init: {"flowchart": {"defaultRenderer": "elk"}} }%% repo{{Events}} 1{{Aggregate}} result{{Result Set}} repo --> 1 1 --> result
logscale
stats(function=count())

Introduction

The stats() function can be used to perform aggregate calculations across all events, with count() being commonly used to count the total number of events.

In this example, the stats() is used with count() to calculate the total number of events in the result set.

Example incoming data might look like this:

@timestampstatus_codeendpointresponse_time
1686837825000200/api/users145
1686837826000404/api/products89
1686837827000200/api/orders167
1686837828000500/api/payment890
1686837829000200/api/users156
1686837830000404/api/items78

Step-by-Step

  1. Starting with the source repository events.

  2. flowchart LR; %%{init: {"flowchart": {"defaultRenderer": "elk"}} }%% repo{{Events}} 1{{Aggregate}} result{{Result Set}} repo --> 1 1 --> result style 1 fill:#ff0000,stroke-width:4px,stroke:#000;
    logscale
    stats(function=count())

    Counts the total number of events in the result set. The count() function is passed as an argument to stats() and returns the count in a field named _count.

    The query is actually equivalent to just count().

  3. Event Result set.

Summary and Results

The query is used to get a simple count of the total number of events matching the query.

This query is useful, for example, to monitor event volumes, verify data ingestion, or get quick counts of specific event types when combined with filters.

Sample output from the incoming example data:

_count
6

Note that only one row is returned containing the total count