On this page
eval command
Compute a new column from the ones a row already has - arithmetic, text, and conditionals.
Computes new columns from the values a row already has, using arithmetic, comparisons, text functions and conditionals. The columns exist for this query only.
eval works on both sides of an aggregation. Before stats it calculates per event. After stats it calculates over the aggregate's own columns, which is how you express a ratio of two aggregates.
Syntax
| eval <name> = <expression> [, <name> = <expression> ...]Square brackets mark an optional part, and angle brackets a value you supply.
Arguments
| Argument | Required | Description |
|---|---|---|
<name> | Required | The column to create. A name that matches an existing field replaces it for the rest of the query. |
<expression> | Required | Field names, numbers and quoted strings combined with + - * / % and parentheses, comparisons (= != > >= < <=), AND, OR, NOT, and the functions below. A quoted string can include fields in braces: "{country_name} {currency}". |
Functions
| Function | Returns | Summary |
|---|---|---|
round | number | Round a number, optionally to a number of decimal places. |
floor | number | Round down to a whole number. |
ceil | number | Round up to a whole number. |
abs | number | Distance from zero. |
len | number | Number of characters in a value. |
lower | text | Lowercase a value. |
upper | text | Uppercase a value. |
concat | text | Join two or more values into one string. |
substr | text | Part of a value, from a 1-based start position. |
coalesce | same as its arguments | The first value that is present and not empty. |
if | same as its arguments | Pick between two values based on a condition. |
now | number | The time the search started, in whole seconds since the Unix epoch. |
time | number | The current clock time in seconds since the Unix epoch, with fractions of a second. |
strftime | text | Format a time as text in UTC, using %Y, %m, %d, %H, %M, %S and the other time directives. |
strptime | number | Read a time from text in the given format, as seconds since the Unix epoch. |
relative_time | number | Move a time by a modifier such as -1d, +2h or -7d@d, as seconds since the Unix epoch. |
to_timestamp | number | A time as seconds since the Unix epoch, from a timestamp, a number or ISO 8601 text. |
to_datetime | text | A time as ISO 8601 text in UTC, from a timestamp, a number or text. |
round(<number>[, <places>])
Round a number, optionally to a number of decimal places.
Returns: number
round(duration_ms / 1000, 2)floor(<number>)
Round down to a whole number.
Returns: number
floor(duration_ms / 100)ceil(<number>)
Round up to a whole number.
Returns: number
ceil(duration_ms / 100)abs(<number>)
Distance from zero.
Returns: number
abs(expected_ms - duration_ms)len(<value>)
Number of characters in a value.
Returns: number
len(message)lower(<value>)
Lowercase a value.
Returns: text
lower(level)upper(<value>)
Uppercase a value.
Returns: text
upper(level)concat(<value>, <value>, ...)
Join two or more values into one string.
Returns: text
concat(service, " - ", level)substr(<value>, <start>[, <length>])
Part of a value, from a 1-based start position.
Returns: text
substr(request_id, 1, 8)coalesce(<value>, <value>, ...)
The first value that is present and not empty.
Returns: same as its arguments
coalesce(log_group, "unknown")if(<condition>, <then>, <else>)
Pick between two values based on a condition.
Returns: same as its arguments
if(duration_ms > 1000, "slow", "fast")now()
The time the search started, in whole seconds since the Unix epoch.
Returns: number
now() - to_timestamp(created_at)time()
The current clock time in seconds since the Unix epoch, with fractions of a second.
Returns: number
time() - now()strftime(<time>, <format>)
Format a time as text in UTC, using %Y, %m, %d, %H, %M, %S and the other time directives.
Returns: text
strftime(_time, "%Y-%m-%d %H:%M")strptime(<text>, <format>)
Read a time from text in the given format, as seconds since the Unix epoch.
Returns: number
strptime(started, "%d/%b/%Y:%H:%M:%S %z")relative_time(<time>, <modifier>)
Move a time by a modifier such as -1d, +2h or -7d@d, as seconds since the Unix epoch.
Returns: number
relative_time(now(), "-1d@d")to_timestamp(<value>[, <format>])
A time as seconds since the Unix epoch, from a timestamp, a number or ISO 8601 text.
Returns: number
to_timestamp(created_at)to_datetime(<value>[, <format>])
A time as ISO 8601 text in UTC, from a timestamp, a number or text.
Returns: text
to_datetime(expires_at)Usage
- Dividing by zero, or taking a remainder by zero, gives an empty value rather than an error.
+joins text when either side is plainly text: a quoted string, a text function, or another+that is already joining text.country_name + " " + currencyjoins, whileerrors + warningsadds.- A comparison on its own gives
1or0, so| eval slow = duration_ms > 1000 | stats sum(slow)counts slow requests. - Write a field name that contains - in backticks, such as
status-code, or put spaces around-to subtract. roundandsubstrneed whole-number literals for their position and precision arguments.
Examples
1. Convert units
| eval seconds = duration_ms / 1000A new seconds column from duration_ms.
2. Percentage, rounded
| eval used_pct = round(max_memory_used_mb * 100 / memory_size_mb, 1)Memory usage as a percentage to one decimal place.
3. Label rows by a condition
| eval tier = if(duration_ms > 1000, "slow", "fast") | stats count() by tierClassifies each request, then counts each class.
4. Join fields into one label
| eval item = "{country_name} {currency}"Braces inside a quoted string insert field values.
5. Several columns, each building on the last
| eval seconds = duration_ms / 1000, minutes = seconds / 60Each assignment can use the columns created before it.
6. Errors per host
level="error" | stats count() AS errors, distinct_count(host) AS hosts | eval per_host = errors / hostsA ratio of two aggregates, calculated after stats.