Reference to the orElse operator

Summary

.orElse(<tsExpression>)

You can use the .orElse() operator to force a query to return a default value.

Parameters

ParameterDescription
tsExpression Expression describing the time series to be tested for existence.

Description

You can use the .orElse() operator to force the query to return a default value. For example orElse(5) returns 5. You can also enter a timeseries for chained .orElse statements, for example .orElse(ts('my.metric')).

If the time series does not exist in the time window, for example, if it’s not reporting in the past four weeks, NO DATA is shown in charts by default. However, if .orElse specifies a value of 25 in case of NO DATA (ts('NO-DATA').orElse(25)), then 25 is shown.

Examples

When a query returns some data, you can use a query of the type to return the same data:

ts(~sample.disk.bytes.read, source="app-1").orElse(1)

When a query returns some data and you want to return some other time series, use a query of the type:

if(ts(~sample.disk.bytes.read, source="app-1"), ts(~sample.mem.page.reads, source="app-1*")).orElse(1)

Considering you don’t know whether the query returns data, and you want to return some other value in the gaps, use a query of the type:

default(0, ts(~sample.mem.page.reads, source="app-1*")).orElse(0)

The default(0,<tsExpression>) part of the query returns the values when the query has reported them. The .orElse portion takes care of the time series when the query returns NO DATA.

Considering the query returns no data, and you want to display a constant, use a query of the type:

ts(nonexistent).orElse(0)

If you want to display another series, use a query of the type:

ts(nonexistent).orElse(ts(~sample.disk.bytes.read, source=app-1))

You can chain multiple .orElse operators. For example:

ts(<metric_not_there>).orElse(ts(<metric_sometimes_there>)).orElse(25)

For this example:

  • If metric_not_there has no value, the function returns the value of (ts(<metric_sometimes_there>)).
  • If (ts(<metric_sometimes_there>)) also has no value, the function returns 25.

Caveats

See Also

The default() function lets you fill in gaps in the time series described by tsExpression.