Provides the number of elements in an array.
Omitted Argument NamesThe argument name for
array
can be omitted; the following forms of this function are equivalent:logscalearray:length("value[]")
and:
logscalearray:length(array="value[]")
These examples show basic structure only; full examples are provided below.
Given an event with an array field like animals[]:
animals[0]: horse
animals[1]: duck
animals[2]: bunny
and the following query:
array:length("animals[]")
The result is the number of elements found in the
animals[] array field
(horse
,
duck
and
bunny
) for that event:
_length: 3
Only array elements in sequential order are counted — if there is a gap in the array, the function assumes it is the end of the array.
For example, given the event:
animals[0]: horse
animals[1]: duck
animals[3]: bunny
The query counts the first two elements only, thus it will output:
_length: 2
If applied to non-array input fields, the function returns
0
.
Event:
#kind: logs
Query:
array:length("#kind")
Output:
_length: 0
array:length()
Examples
Counting Array Elements - Example 1
array:length("queryParserMetrics.function[]", as="_numberOfFunctions")
Given an event that has multiple queryParserMetrics.function[] array fields (a list of the functions used in a query):
Event 1
|----------------------------------|-----------|
| queryParserMetrics.function [0] | head |
| queryParserMetrics.function [1] | bucket |
| queryParserMetrics.functions[2] | groupBy |
|---------------------------------- |----------|
We want to get the number of functions listed in the
queryParserMetrics.function[]
arrays for that
event.
Starting with the source repository events
- flowchart LR; %%{init: {"flowchart": {"defaultRenderer": "elk"}} }%% repo{{Events}} 0>Augment Data] result{{Result Set}} repo --> 0 0 --> result style 0 fill:#ff0000,stroke-width:4px,stroke:#000;
Count the elements (the functions in the array) in the
queryParserMetrics.function[]
array field.logscalearray:length("queryParserMetrics.function[]", as="_numberOfFunctions")
Event Result set
The returned value is the number of functions found in the
array, and it will be output into the
_numberOfFunctions
field, which is set by the argument
as
.
_numberOfFunctions | 3 |
---|
Counting Array Elements - Example 2
queryParserMetrics.function[0] = "head"
array:length("queryParserMetrics.function[]", as="_numberOfFunctions" )
Given an event that has the queryParserMetrics.function[] array fields (a list of the functions used in a query):
Event 1
|----------------------------------|-----------|
| queryParserMetrics.function[0] | head |
| queryParserMetrics.function[1] | bucket |
| queryParserMetrics.function[2] | groupBy |
|----------------------------------|----------|
Filter only events that has head
as the value of
queryParserMetrics.function[]
array field, and then get the number of functions listed in the
queryParserMetrics.function[]
array for that event.
Starting with the source repository events
- flowchart LR; %%{init: {"flowchart": {"defaultRenderer": "elk"}} }%% repo{{Events}} 0[/Filter/] 1>Augment Data] result{{Result Set}} repo --> 0 0 --> 1 1 --> result style 0 fill:#ff0000,stroke-width:4px,stroke:#000;
Filter events that has
head
as the value of queryParserMetrics.function[0] array field.logscalequeryParserMetrics.function[0] = "head"
- flowchart LR; %%{init: {"flowchart": {"defaultRenderer": "elk"}} }%% repo{{Events}} 0[/Filter/] 1>Augment Data] result{{Result Set}} repo --> 0 0 --> 1 1 --> result style 1 fill:#ff0000,stroke-width:4px,stroke:#000;
Count the elements (the functions used) in all the
queryParserMetrics.function[]
arrays for that event.logscalearray:length("queryParserMetrics.function[]", as="_numberOfFunctions" )
Event Result set
You will get an array of all the functions used in just one
event where head
is the first
function used. The result will be output into the
_numberOfFunctions field
set by the argument
as
.