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 1 commit
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
20 changes: 17 additions & 3 deletions plugins/inputs/statsd/statsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,20 @@ const (

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

type Number struct {
Value float64
}

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

n.Value = 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 +65,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 @@ -306,8 +320,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
6 changes: 3 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{{Value: 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{{Value: 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{{Value: 90.0}}
acc := &testutil.Accumulator{}

validLines := []string{
Expand Down