Skip to content

percentile - Calculate percentiles of numerical values

The percentile function will calculate the approximate n-th percentile from a data of numbers that are passed into it.

Syntax

percentile(percentile: number, expression: number, error_threshold: number?)

Arguments

NameTypeRequiredDescription
percentilenumbertrueThe percentile value we wish to find, between 0 and 1
expressionnumbertrueThe expression whose percentile we seek
error_thresholdnumberfalseThe accuracy we should use. Lower numbers are more accurate. Defaults to 0.01

Controlling accuracy

Since the percentile calculation is approximate, the accuracy may be controlled with the error_threshold parameter which ranges from 0 to 1 (defaults to 0.01). A lower value will result in better accuracy at the cost of longer query times.

For example, the following query will be very fast but possibly inaccurate:

groupby $m.severity aggregate percentile(0.99, $d.duration, 0.5) as p99_latency

Whereas the following query will be much more accurate, but may slow down your query time:

groupby $m.severity aggregate percentile(0.99, $d.duration, 0.001) as p99_latency

It's important to draw a balance between performance and precision, depending on your use case.

Example - Calculating 99th percentile from trace data

Let's say we're interested in the 99th percentile of a lambda function. We have trace data, and the lambda function's operation name is Lambda.Invoke. We can then run the following query to compute its 99th percentile:

source spans
| filter $l.operationName == 'Lambda.Invoke'
| groupby $l.operationName agg percentile(0.99, $m.duration)