fixed percentiles not being able to be ints (#9447)

This commit is contained in:
Mya 2021-07-19 08:53:07 -06:00 committed by GitHub
parent 2a72295734
commit ff8ed37762
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 5 deletions

View File

@ -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
@ -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
@ -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))
}
}

View File

@ -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
@ -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{
@ -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{
@ -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})
}