Browse docs
On this page

eval command

Compute a new column from the ones a row already has - arithmetic, text, and conditionals.

All ObiQL commands

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

ObiQL
| eval <name> = <expression> [, <name> = <expression> ...]

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

Arguments

ArgumentRequiredDescription
<name>RequiredThe column to create. A name that matches an existing field replaces it for the rest of the query.
<expression>RequiredField 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

FunctionReturnsSummary
roundnumberRound a number, optionally to a number of decimal places.
floornumberRound down to a whole number.
ceilnumberRound up to a whole number.
absnumberDistance from zero.
lennumberNumber of characters in a value.
lowertextLowercase a value.
uppertextUppercase a value.
concattextJoin two or more values into one string.
substrtextPart of a value, from a 1-based start position.
coalescesame as its argumentsThe first value that is present and not empty.
ifsame as its argumentsPick between two values based on a condition.
nownumberThe time the search started, in whole seconds since the Unix epoch.
timenumberThe current clock time in seconds since the Unix epoch, with fractions of a second.
strftimetextFormat a time as text in UTC, using %Y, %m, %d, %H, %M, %S and the other time directives.
strptimenumberRead a time from text in the given format, as seconds since the Unix epoch.
relative_timenumberMove a time by a modifier such as -1d, +2h or -7d@d, as seconds since the Unix epoch.
to_timestampnumberA time as seconds since the Unix epoch, from a timestamp, a number or ISO 8601 text.
to_datetimetextA 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

ObiQL
round(duration_ms / 1000, 2)

floor(<number>)

Round down to a whole number.

Returns: number

ObiQL
floor(duration_ms / 100)

ceil(<number>)

Round up to a whole number.

Returns: number

ObiQL
ceil(duration_ms / 100)

abs(<number>)

Distance from zero.

Returns: number

ObiQL
abs(expected_ms - duration_ms)

len(<value>)

Number of characters in a value.

Returns: number

ObiQL
len(message)

lower(<value>)

Lowercase a value.

Returns: text

ObiQL
lower(level)

upper(<value>)

Uppercase a value.

Returns: text

ObiQL
upper(level)

concat(<value>, <value>, ...)

Join two or more values into one string.

Returns: text

ObiQL
concat(service, " - ", level)

substr(<value>, <start>[, <length>])

Part of a value, from a 1-based start position.

Returns: text

ObiQL
substr(request_id, 1, 8)

coalesce(<value>, <value>, ...)

The first value that is present and not empty.

Returns: same as its arguments

ObiQL
coalesce(log_group, "unknown")

if(<condition>, <then>, <else>)

Pick between two values based on a condition.

Returns: same as its arguments

ObiQL
if(duration_ms > 1000, "slow", "fast")

now()

The time the search started, in whole seconds since the Unix epoch.

Returns: number

ObiQL
now() - to_timestamp(created_at)

time()

The current clock time in seconds since the Unix epoch, with fractions of a second.

Returns: number

ObiQL
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

ObiQL
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

ObiQL
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

ObiQL
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

ObiQL
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

ObiQL
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 + " " + currency joins, while errors + warnings adds.
  • A comparison on its own gives 1 or 0, 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.
  • round and substr need whole-number literals for their position and precision arguments.

Examples

1. Convert units

ObiQL
| eval seconds = duration_ms / 1000

A new seconds column from duration_ms.

2. Percentage, rounded

ObiQL
| 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

ObiQL
| eval tier = if(duration_ms > 1000, "slow", "fast") | stats count() by tier

Classifies each request, then counts each class.

4. Join fields into one label

ObiQL
| eval item = "{country_name} {currency}"

Braces inside a quoted string insert field values.

5. Several columns, each building on the last

ObiQL
| eval seconds = duration_ms / 1000, minutes = seconds / 60

Each assignment can use the columns created before it.

6. Errors per host

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

A ratio of two aggregates, calculated after stats.