fix: calculate conservative SLO percentiles

This commit is contained in:
Josh Creek
2026-08-31 21:08:49 +01:00
parent bc7136b2cb
commit 58e8a5c523
2 changed files with 14 additions and 1 deletions
+8 -1
View File
@@ -1,6 +1,7 @@
package observability
import (
"math"
"sort"
"time"
)
@@ -52,7 +53,13 @@ func percentile(values []time.Duration, p float64) time.Duration {
}
ordered := append([]time.Duration(nil), values...)
sort.Slice(ordered, func(i, j int) bool { return ordered[i] < ordered[j] })
index := int(float64(len(ordered)-1) * p)
index := int(math.Ceil(p*float64(len(ordered)))) - 1
if index < 0 {
index = 0
}
if index >= len(ordered) {
index = len(ordered) - 1
}
return ordered[index]
}
+6
View File
@@ -25,3 +25,9 @@ func TestEvaluateSLODoesNotInventFailureForEmptyOptionalWindows(t *testing.T) {
t.Fatalf("empty window violations = %+v", violations)
}
}
func TestPercentileUsesConservativeNearestRankForSmallWindows(t *testing.T) {
if got := percentile([]time.Duration{time.Millisecond, 101 * time.Millisecond}, .95); got != 101*time.Millisecond {
t.Fatalf("p95 underreported small window: %s", got)
}
}