Debian Codegraph @

memmetrics/anomaly.go

raw
1 package memmetrics
2
3 import (
4 "math"
5 "sort"
6 "time"
7 )
8
9 // SplitLatencies provides simple anomaly detection for requests latencies.
10 // it splits values into good or bad category based on the threshold and the median value.
11 // If all values are not far from the median, it will return all values in 'good' set.
12 // Precision is the smallest value to consider, e.g. if set to millisecond, microseconds will be ignored.
13 func SplitLatencies(values []time.Duration, precision time.Duration) (good map[time.Duration]bool, bad map[time.Duration]bool) {
14 // Find the max latency M and then map each latency L to the ratio L/M and then call SplitFloat64
15 v2r := map[float64]time.Duration{}
16 ratios := make([]float64, len(values))
17 m := maxTime(values)
18 for i, v := range values {
19 ratio := float64(v/precision+1) / float64(m/precision+1) // +1 is to avoid division by 0
20 v2r[ratio] = v
21 ratios[i] = ratio
22 }
23 good, bad = make(map[time.Duration]bool), make(map[time.Duration]bool)
24 // Note that multiplier makes this function way less sensitive than ratios detector, this is to avoid noise.
25 vgood, vbad := SplitFloat64(2, 0, ratios)
26 for r := range vgood {
27 good[v2r[r]] = true
28 }
29 for r := range vbad {
30 bad[v2r[r]] = true
31 }
32 return good, bad
33 }
34
35 // SplitRatios provides simple anomaly detection for ratio values, that are all in the range [0, 1]
36 // it splits values into good or bad category based on the threshold and the median value.
37 // If all values are not far from the median, it will return all values in 'good' set.
38 func SplitRatios(values []float64) (good map[float64]bool, bad map[float64]bool) {
39 return SplitFloat64(1.5, 0, values)
40 }
41
42 // SplitFloat64 provides simple anomaly detection for skewed data sets with no particular distribution.
43 // In essence it applies the formula if(v > median(values) + threshold * medianAbsoluteDeviation) -> anomaly
44 // There's a corner case where there are just 2 values, so by definition there's no value that exceeds the threshold.
45 // This case is solved by introducing additional value that we know is good, e.g. 0. That helps to improve the detection results
46 // on such data sets.
47 func SplitFloat64(threshold, sentinel float64, values []float64) (good map[float64]bool, bad map[float64]bool) {
48 good, bad = make(map[float64]bool), make(map[float64]bool)
49 var newValues []float64
50 if len(values)%2 == 0 {
51 newValues = make([]float64, len(values)+1)
52 copy(newValues, values)
53 // Add a sentinel endpoint so we can distinguish outliers better
54 newValues[len(newValues)-1] = sentinel
55 } else {
56 newValues = values
57 }
58
59 m := median(newValues)
60 mAbs := medianAbsoluteDeviation(newValues)
61 for _, v := range values {
62 if v > (m+mAbs)*threshold {
63 bad[v] = true
64 } else {
65 good[v] = true
66 }
67 }
68 return good, bad
69 }
70
71 func median(values []float64) float64 {
72 vals := make([]float64, len(values))
73 copy(vals, values)
74 sort.Float64s(vals)
75 l := len(vals)
76 if l%2 != 0 {
77 return vals[l/2]
78 }
79 return (vals[l/2-1] + vals[l/2]) / 2.0
80 }
81
82 func medianAbsoluteDeviation(values []float64) float64 {
83 m := median(values)
84 distances := make([]float64, len(values))
85 for i, v := range values {
86 distances[i] = math.Abs(v - m)
87 }
88 return median(distances)
89 }
90
91 func maxTime(vals []time.Duration) time.Duration {
92 val := vals[0]
93 for _, v := range vals {
94 if v > val {
95 val = v
96 }
97 }
98 return val
99 }