Calculates the average for a field over a set of events. The result is returned in a field named _avg. You can use this field name to pipe the results to other query functions for further processing, as shown in the example below.

ParameterTypeRequiredDefault ValueDescription
asstringoptional[a]_avg The optional name of the output field.
field[b]stringrequired  The field from which to extract a number and calculate the average.

[a] Optional parameters use their default value unless explicitly set.

[b] The parameter name field can be omitted.

Hide omitted argument names for this function

Show omitted argument names for this function

avg() Examples

Click + next to an example below to get the full details.

Calculate Average of Field Values in an Array

Calculate Average of Field Values in a flat array using the array:reduceRow() function

Query
logscale
array:reduceRow("ages[]", var=x, function=avg(x))
Introduction

The array:reduceRow() function can be used together with the aggregate function avg() as the function argument to calculate the average of field values in a flat array. In this example, the array:reduceRow() function is used to calculate the average age of the field ages and return the result in a field named _reduceRow._avg.

Example incoming data might look like this:

ages[0]ages[1]ages[2]
163264
153045
124
895767

Step-by-Step
  1. Starting with the source repository events.

  2. logscale
    array:reduceRow("ages[]", var=x, function=avg(x))

    Produces two events, calculating the average value across the ages[] array for each event. The results are placed into the _avg field for each new event.

  3. Event Result set.

Summary and Results

The query is used to calculate averages for a given array for each event and is a shorthand version of using array:eval() specifically for processing each event.

Sample output from the incoming example data:

ages[0]ages[1]ages[2]_avg
16326437.333
15304530
1242.67
89576771

Note that the evaluation is per event, for example per row of the overall table of values across the array over all events. To calculate values across the column of values, use array:reduceColumn().

Calculate the Mean of CPU Time

Calculate the sum of all numbers (mean) of the CPU time

Query
logscale
avg(field=cputimeNanos)| cputime := (_avg/1000000)| format("%,.2f", field=_avg, as=_avg)
Introduction

CPU time is the exact amount of time that the CPU has spent processing data for a specific program or process. In this query the avg() function is used to calculate the sum of all numbers; the mean of the CPU Time.

Step-by-Step
  1. Starting with the source repository events.

  2. logscale
    avg(field=cputimeNanos)

    Calculates the mean of the field cputimeNanos. This can be run in the humio system repository to get the average time spent in nanoseconds for different activities. The mean is calculated by summing all of the values in the cputimeNanos field and dividing by the number of values across all events.

  3. logscale
    | cputime := (_avg/1000000)

    Calculates the average CPU time to milliseconds to make it easier to read.

  4. logscale
    | format("%,.2f", field=_avg, as=_avg)

    Overwrites the field _avg to contain the _avg field to show only two decimals.

  5. Event Result set.

Summary and Results

The query is used to averaging the field containing the CPU value. This value is then piped to the format() function, which provides a formatting code — how the field value should be formatted. In this example, it formats the value to two decimal. Calculation of CPU times is useful to determine processing power - for example if troubleshooting a system with high CPU usage.

Sample output from the incoming example data:

_avg
0.14

Compute Average Value for Each Array Element With Same Index

Compute an average value for each array element with the same index across multiple events using the array:reduceColumn()

Query
logscale
maxTimes := array:reduceColumn("ages[]", var=x, function=avg(x))
Introduction

The array:reduceColumn() function can be used to compute an average value for each array element with the same index. In this example, the array:reduceColumn() function is used to find the maximum time for each array element with same index in a flat array.

Example incoming data might look like this:

ages[0]ages[1]ages[2]
163264
153045
124
895767
Step-by-Step
  1. Starting with the source repository events.

  2. logscale
    maxTimes := array:reduceColumn("ages[]", var=x, function=avg(x))

    Computes the average for each array element with same index in the array and reduces it to one value, placing the result for each index into a new field _reduceColumn.

  3. Event Result set.

Summary and Results

The query is used to find the maximum time for each array element with same index in a flat array.

_reduceColumn[0]_reduceColumn[1]_reduceColumn[2]_reduceColumn[3]
40.340.363.3