Skip to content

Commit

Permalink
fixed percentiles not being able to be ints (#9447)
Browse files Browse the repository at this point in the history
  • Loading branch information
MyaLongmire authored Jul 19, 2021
1 parent 2a72295 commit ff8ed37
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
17 changes: 15 additions & 2 deletions plugins/inputs/statsd/statsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,19 @@ const (

var errParsing = errors.New("error parsing statsd line")

// Number will get parsed as an int or float depending on what is passed
type Number float64

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

*n = Number(value)
return nil
}

// Statsd allows the importing of statsd and dogstatsd data.
type Statsd struct {
// Protocol used on listener - udp or tcp
Expand All @@ -51,7 +64,7 @@ type Statsd struct {

// Percentiles specifies the percentiles that will be calculated for timing
// and histogram stats.
Percentiles []float64
Percentiles []Number
PercentileLimit int

DeleteGauges bool
Expand Down Expand Up @@ -307,7 +320,7 @@ func (s *Statsd) Gather(acc telegraf.Accumulator) error {
fields[prefix+"count"] = stats.Count()
for _, percentile := range s.Percentiles {
name := fmt.Sprintf("%s%v_percentile", prefix, percentile)
fields[name] = stats.Percentile(percentile)
fields[name] = stats.Percentile(float64(percentile))
}
}

Expand Down
15 changes: 12 additions & 3 deletions plugins/inputs/statsd/statsd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ func TestParse_Counters(t *testing.T) {
// Tests low-level functionality of timings
func TestParse_Timings(t *testing.T) {
s := NewTestStatsd()
s.Percentiles = []float64{90.0}
s.Percentiles = []Number{90.0}
acc := &testutil.Accumulator{}

// Test that timings work
Expand Down Expand Up @@ -1186,7 +1186,7 @@ func TestParse_MeasurementsWithMultipleValues(t *testing.T) {
func TestParse_TimingsMultipleFieldsWithTemplate(t *testing.T) {
s := NewTestStatsd()
s.Templates = []string{"measurement.field"}
s.Percentiles = []float64{90.0}
s.Percentiles = []Number{90.0}
acc := &testutil.Accumulator{}

validLines := []string{
Expand Down Expand Up @@ -1234,7 +1234,7 @@ func TestParse_TimingsMultipleFieldsWithTemplate(t *testing.T) {
func TestParse_TimingsMultipleFieldsWithoutTemplate(t *testing.T) {
s := NewTestStatsd()
s.Templates = []string{}
s.Percentiles = []float64{90.0}
s.Percentiles = []Number{90.0}
acc := &testutil.Accumulator{}

validLines := []string{
Expand Down Expand Up @@ -1664,3 +1664,12 @@ func TestUdp(t *testing.T) {
testutil.IgnoreTime(),
)
}

func TestParse_Ints(t *testing.T) {
s := NewTestStatsd()
s.Percentiles = []Number{90}
acc := &testutil.Accumulator{}

require.NoError(t, s.Gather(acc))
require.Equal(t, s.Percentiles, []Number{90.0})
}

0 comments on commit ff8ed37

Please sign in to comment.