Skip to content

avg - Compute the average of a numerical field

The avg function computes the average (or mean) value for a given numerical field.

NOTE: The input must be a number. If it is a string, it can be parsed into a number using a cast.

Syntax

avg(expression: number): number

Arguments

NameTypeRequiredDescription
expressionnumbertrueAn expression that returns a number

Example - Computing average HTTP request latency for each path

In this example, we're going to group our HTTP traffic logs by a given URL, which is available in a field path. We will then perform an aggregation to calculate the average duration for each of the path values.

groupby $d.path aggregate avg($d.duration) as average_duration_for_path

NOTE: This type of aggregation removes all fields besides the grouping key and any calculated aggregations. In effect, our resultant document looks like this:

{
    "path": "/home",
    "average_duration_for_path": 500
}