In SPL, what is the difference between eval and stats, and which functions does each typically use?
eval works on one event at a time and adds a computed field to it; stats works across many events and collapses them into summary rows. eval uses functions like if, case, cidrmatch, len; stats uses count, dc, avg, values.
* eval widens the table, stats shrinks it. *
The two answer different kinds of question. "What is this event?" needs a value derived from that event alone, so eval calculates it and the number of results stays the same. "How many, how often, how much?" needs many events at once, so stats aggregates them and the result shrinks to one row per group.
eval |
stats |
|
|---|---|---|
| Works on | Each event separately | All events, optionally grouped by a field |
| Output | Same events, one more field | One summary row per group |
| Typical functions | if(X,Y,Z), case(X,"Y",…), cidrmatch("10.0.0.0/8", ip), coalesce, len, lower, round, tostring |
count, dc (distinct count), avg, min/max, sum, values, perc95, stdev |
| Example | … | eval status_text=if(status==200,"OK","Error") |
… | stats count, dc(clientip) by status |
They are usually chained: eval first labels each event (internal vs external address with cidrmatch), then stats counts the labels. A common mistake is using count inside eval; counting needs several events, so it only exists as a stats function (or in eventstats/streamstats, which add the aggregate back onto each event).
Tip: dc(user) answers "how many different users?", count answers "how many events?". Ten failed logins by one user are count=10, dc(user)=1, a pattern that separates brute force against one account from password spraying across many.
Go deeper:
Splunk — eval — the command and its expression syntax.
Splunk — stats — aggregation with and without a BY clause, compared to SQL.
Splunk — Statistical and charting functions — every function stats, chart and timechart accept.