Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed percentiles not being able to be ints #9447

Merged
merged 4 commits into from
Jul 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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})
}