Skip to content

Commit

Permalink
Fix panic in statsd p100 calculation (#3230)
Browse files Browse the repository at this point in the history
  • Loading branch information
tpounds authored and danielnelson committed Sep 14, 2017
1 parent 279fa20 commit 3468ffc
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
13 changes: 10 additions & 3 deletions plugins/inputs/statsd/running_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,15 @@ func (rs *RunningStats) Percentile(n int) float64 {
}

i := int(float64(len(rs.perc)) * float64(n) / float64(100))
if i < 0 {
i = 0
return rs.perc[clamp(i, 0, len(rs.perc)-1)]
}

func clamp(i int, min int, max int) int {
if i < min {
return min
}
if i > max {
return max
}
return rs.perc[i]
return i
}
18 changes: 18 additions & 0 deletions plugins/inputs/statsd/running_stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,18 @@ func TestRunningStats_Single(t *testing.T) {
if rs.Lower() != 10.1 {
t.Errorf("Expected %v, got %v", 10.1, rs.Lower())
}
if rs.Percentile(100) != 10.1 {
t.Errorf("Expected %v, got %v", 10.1, rs.Percentile(100))
}
if rs.Percentile(90) != 10.1 {
t.Errorf("Expected %v, got %v", 10.1, rs.Percentile(90))
}
if rs.Percentile(50) != 10.1 {
t.Errorf("Expected %v, got %v", 10.1, rs.Percentile(50))
}
if rs.Percentile(0) != 10.1 {
t.Errorf("Expected %v, got %v", 10.1, rs.Percentile(0))
}
if rs.Count() != 1 {
t.Errorf("Expected %v, got %v", 1, rs.Count())
}
Expand Down Expand Up @@ -58,12 +64,18 @@ func TestRunningStats_Duplicate(t *testing.T) {
if rs.Lower() != 10.1 {
t.Errorf("Expected %v, got %v", 10.1, rs.Lower())
}
if rs.Percentile(100) != 10.1 {
t.Errorf("Expected %v, got %v", 10.1, rs.Percentile(100))
}
if rs.Percentile(90) != 10.1 {
t.Errorf("Expected %v, got %v", 10.1, rs.Percentile(90))
}
if rs.Percentile(50) != 10.1 {
t.Errorf("Expected %v, got %v", 10.1, rs.Percentile(50))
}
if rs.Percentile(0) != 10.1 {
t.Errorf("Expected %v, got %v", 10.1, rs.Percentile(0))
}
if rs.Count() != 4 {
t.Errorf("Expected %v, got %v", 4, rs.Count())
}
Expand Down Expand Up @@ -93,12 +105,18 @@ func TestRunningStats(t *testing.T) {
if rs.Lower() != 5 {
t.Errorf("Expected %v, got %v", 5, rs.Lower())
}
if rs.Percentile(100) != 45 {
t.Errorf("Expected %v, got %v", 45, rs.Percentile(100))
}
if rs.Percentile(90) != 32 {
t.Errorf("Expected %v, got %v", 32, rs.Percentile(90))
}
if rs.Percentile(50) != 11 {
t.Errorf("Expected %v, got %v", 11, rs.Percentile(50))
}
if rs.Percentile(0) != 5 {
t.Errorf("Expected %v, got %v", 5, rs.Percentile(0))
}
if rs.Count() != 16 {
t.Errorf("Expected %v, got %v", 4, rs.Count())
}
Expand Down

0 comments on commit 3468ffc

Please sign in to comment.