Support floats in statsd percentiles (#5572)
This commit is contained in:
@@ -34,8 +34,8 @@
|
||||
## Reset timings & histograms every interval (default=true)
|
||||
delete_timings = true
|
||||
|
||||
## Percentiles to calculate for timing & histogram stats
|
||||
percentiles = [90]
|
||||
## Percentiles to calculate for timing & histogram stats.
|
||||
percentiles = [50.0, 90.0, 99.0, 99.9, 99.95, 100.0]
|
||||
|
||||
## separator to use between elements of a statsd metric
|
||||
metric_separator = "_"
|
||||
|
||||
@@ -99,7 +99,7 @@ func (rs *RunningStats) Count() int64 {
|
||||
return rs.n
|
||||
}
|
||||
|
||||
func (rs *RunningStats) Percentile(n int) float64 {
|
||||
func (rs *RunningStats) Percentile(n float64) float64 {
|
||||
if n > 100 {
|
||||
n = 100
|
||||
}
|
||||
@@ -109,16 +109,16 @@ func (rs *RunningStats) Percentile(n int) float64 {
|
||||
rs.sorted = true
|
||||
}
|
||||
|
||||
i := int(float64(len(rs.perc)) * float64(n) / float64(100))
|
||||
i := float64(len(rs.perc)) * n / float64(100)
|
||||
return rs.perc[clamp(i, 0, len(rs.perc)-1)]
|
||||
}
|
||||
|
||||
func clamp(i int, min int, max int) int {
|
||||
if i < min {
|
||||
func clamp(i float64, min int, max int) int {
|
||||
if i < float64(min) {
|
||||
return min
|
||||
}
|
||||
if i > max {
|
||||
if i > float64(max) {
|
||||
return max
|
||||
}
|
||||
return i
|
||||
return int(i)
|
||||
}
|
||||
|
||||
@@ -26,6 +26,9 @@ func TestRunningStats_Single(t *testing.T) {
|
||||
if rs.Percentile(100) != 10.1 {
|
||||
t.Errorf("Expected %v, got %v", 10.1, rs.Percentile(100))
|
||||
}
|
||||
if rs.Percentile(99.95) != 10.1 {
|
||||
t.Errorf("Expected %v, got %v", 10.1, rs.Percentile(99.95))
|
||||
}
|
||||
if rs.Percentile(90) != 10.1 {
|
||||
t.Errorf("Expected %v, got %v", 10.1, rs.Percentile(90))
|
||||
}
|
||||
@@ -67,6 +70,9 @@ func TestRunningStats_Duplicate(t *testing.T) {
|
||||
if rs.Percentile(100) != 10.1 {
|
||||
t.Errorf("Expected %v, got %v", 10.1, rs.Percentile(100))
|
||||
}
|
||||
if rs.Percentile(99.95) != 10.1 {
|
||||
t.Errorf("Expected %v, got %v", 10.1, rs.Percentile(99.95))
|
||||
}
|
||||
if rs.Percentile(90) != 10.1 {
|
||||
t.Errorf("Expected %v, got %v", 10.1, rs.Percentile(90))
|
||||
}
|
||||
@@ -108,12 +114,21 @@ func TestRunningStats(t *testing.T) {
|
||||
if rs.Percentile(100) != 45 {
|
||||
t.Errorf("Expected %v, got %v", 45, rs.Percentile(100))
|
||||
}
|
||||
if rs.Percentile(99.98) != 45 {
|
||||
t.Errorf("Expected %v, got %v", 45, rs.Percentile(99.98))
|
||||
}
|
||||
if rs.Percentile(90) != 32 {
|
||||
t.Errorf("Expected %v, got %v", 32, rs.Percentile(90))
|
||||
}
|
||||
if rs.Percentile(50.1) != 11 {
|
||||
t.Errorf("Expected %v, got %v", 11, rs.Percentile(50.1))
|
||||
}
|
||||
if rs.Percentile(50) != 11 {
|
||||
t.Errorf("Expected %v, got %v", 11, rs.Percentile(50))
|
||||
}
|
||||
if rs.Percentile(49.9) != 10 {
|
||||
t.Errorf("Expected %v, got %v", 10, rs.Percentile(49.9))
|
||||
}
|
||||
if rs.Percentile(0) != 5 {
|
||||
t.Errorf("Expected %v, got %v", 5, rs.Percentile(0))
|
||||
}
|
||||
|
||||
@@ -55,7 +55,7 @@ type Statsd struct {
|
||||
|
||||
// Percentiles specifies the percentiles that will be calculated for timing
|
||||
// and histogram stats.
|
||||
Percentiles []int
|
||||
Percentiles []internal.Number
|
||||
PercentileLimit int
|
||||
|
||||
DeleteGauges bool
|
||||
@@ -217,7 +217,7 @@ const sampleConfig = `
|
||||
delete_timings = true
|
||||
|
||||
## Percentiles to calculate for timing & histogram stats
|
||||
percentiles = [90]
|
||||
percentiles = [50.0, 90.0, 99.0, 99.9, 99.95, 100.0]
|
||||
|
||||
## separator to use between elements of a statsd metric
|
||||
metric_separator = "_"
|
||||
@@ -271,8 +271,8 @@ func (s *Statsd) Gather(acc telegraf.Accumulator) error {
|
||||
fields[prefix+"lower"] = stats.Lower()
|
||||
fields[prefix+"count"] = stats.Count()
|
||||
for _, percentile := range s.Percentiles {
|
||||
name := fmt.Sprintf("%s%v_percentile", prefix, percentile)
|
||||
fields[name] = stats.Percentile(percentile)
|
||||
name := fmt.Sprintf("%s%v_percentile", prefix, percentile.Value)
|
||||
fields[name] = stats.Percentile(percentile.Value)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/influxdata/telegraf"
|
||||
"github.com/influxdata/telegraf/internal"
|
||||
"github.com/influxdata/telegraf/testutil"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
@@ -397,7 +398,7 @@ func TestParse_Counters(t *testing.T) {
|
||||
// Tests low-level functionality of timings
|
||||
func TestParse_Timings(t *testing.T) {
|
||||
s := NewTestStatsd()
|
||||
s.Percentiles = []int{90}
|
||||
s.Percentiles = []internal.Number{{Value: 90.0}}
|
||||
acc := &testutil.Accumulator{}
|
||||
|
||||
// Test that counters work
|
||||
@@ -1181,7 +1182,7 @@ func TestParse_MeasurementsWithMultipleValues(t *testing.T) {
|
||||
func TestParse_TimingsMultipleFieldsWithTemplate(t *testing.T) {
|
||||
s := NewTestStatsd()
|
||||
s.Templates = []string{"measurement.field"}
|
||||
s.Percentiles = []int{90}
|
||||
s.Percentiles = []internal.Number{{Value: 90.0}}
|
||||
acc := &testutil.Accumulator{}
|
||||
|
||||
validLines := []string{
|
||||
@@ -1232,7 +1233,7 @@ func TestParse_TimingsMultipleFieldsWithTemplate(t *testing.T) {
|
||||
func TestParse_TimingsMultipleFieldsWithoutTemplate(t *testing.T) {
|
||||
s := NewTestStatsd()
|
||||
s.Templates = []string{}
|
||||
s.Percentiles = []int{90}
|
||||
s.Percentiles = []internal.Number{{Value: 90.0}}
|
||||
acc := &testutil.Accumulator{}
|
||||
|
||||
validLines := []string{
|
||||
|
||||
Reference in New Issue
Block a user