Skip to content

Commit

Permalink
Support floats in statsd percentiles (influxdata#5572)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pitxyoki authored and idohalevi committed Sep 23, 2020
1 parent d229811 commit 9f6d414
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 15 deletions.
14 changes: 14 additions & 0 deletions internal/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ type Size struct {
Size int64
}

type Number struct {
Value float64
}

// SetVersion sets the telegraf agent version
func SetVersion(v string) error {
if version != "" {
Expand Down Expand Up @@ -124,6 +128,16 @@ func (s *Size) UnmarshalTOML(b []byte) error {
return nil
}

func (n *Number) UnmarshalTOML(b []byte) error {
value, err := strconv.ParseFloat(string(b), 64)
if err != nil {
return err
}

n.Value = value
return nil
}

// ReadLines reads contents from a file and splits them by new lines.
// A convenience wrapper to ReadLinesOffsetN(filename, 0, -1).
func ReadLines(filename string) ([]string, error) {
Expand Down
4 changes: 2 additions & 2 deletions plugins/inputs/statsd/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "_"
Expand Down
12 changes: 6 additions & 6 deletions plugins/inputs/statsd/running_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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)
}
15 changes: 15 additions & 0 deletions plugins/inputs/statsd/running_stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down Expand Up @@ -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))
}
Expand Down Expand Up @@ -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))
}
Expand Down
8 changes: 4 additions & 4 deletions plugins/inputs/statsd/statsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = "_"
Expand Down Expand Up @@ -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)
}
}

Expand Down
7 changes: 4 additions & 3 deletions plugins/inputs/statsd/statsd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand Down

0 comments on commit 9f6d414

Please sign in to comment.