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

feat(inputs.unbound): Collect histogram statistics #16452

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
24 changes: 11 additions & 13 deletions plugins/inputs/unbound/unbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (

"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/config"
"github.com/influxdata/telegraf/filter"
"github.com/influxdata/telegraf/internal"
"github.com/influxdata/telegraf/plugins/inputs"
)
Expand Down Expand Up @@ -47,13 +46,6 @@ func (*Unbound) SampleConfig() string {

// All the dots in stat name will replaced by underscores. Histogram statistics will not be collected.
func (s *Unbound) Gather(acc telegraf.Accumulator) error {
// Always exclude histogram statistics
statExcluded := []string{"histogram.*"}
filterExcluded, err := filter.Compile(statExcluded)
if err != nil {
return err
}

out, err := s.run(*s)
if err != nil {
return fmt.Errorf("error gathering metrics: %w", err)
Expand All @@ -75,11 +67,6 @@ func (s *Unbound) Gather(acc telegraf.Accumulator) error {
stat := cols[0]
value := cols[1]

// Filter value
if filterExcluded.Match(stat) {
continue
}

fieldValue, err := strconv.ParseFloat(value, 64)
if err != nil {
acc.AddError(fmt.Errorf("expected a numerical value for %s = %v", stat, value))
Expand All @@ -106,6 +93,17 @@ func (s *Unbound) Gather(acc telegraf.Accumulator) error {
fieldsThreads[threadID][field] = fieldValue
}
}
} else if strings.HasPrefix(stat, "histogram") {
statTokens := strings.Split(stat, ".")
if len(statTokens) > 1 {
lbound, err := strconv.ParseFloat(strings.Join(statTokens[1:3], "."), 64)
if err != nil {
acc.AddError(fmt.Errorf("expected a numeric value for the histogram bucket lower bound: %s", strings.Join(statTokens[1:3], ".")))
continue
}
field := fmt.Sprintf("%s_%f", statTokens[0], lbound)
fields[field] = fieldValue
}
} else {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about

Suggested change
} else if strings.HasPrefix(stat, "histogram") {
statTokens := strings.Split(stat, ".")
if len(statTokens) > 1 {
lbound, err := strconv.ParseFloat(strings.Join(statTokens[1:3], "."), 64)
if err != nil {
acc.AddError(fmt.Errorf("expected a numeric value for the histogram bucket lower bound: %s", strings.Join(statTokens[1:3], ".")))
continue
}
field := fmt.Sprintf("%s_%f", statTokens[0], lbound)
fields[field] = fieldValue
}
} else {
} else if suffix, found := strings.CutPrefix(x, "histogram."); found {
statTokens := strings.Split(stat, ".")
fields["histogram_"+suffix] = fieldValue
} else {

Copy link
Author

@zeloff zeloff Jan 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was my first stab at Go, so apologies for style and efficiency issues.

Your suggestion is indeed simpler but - and this is a personal opinion - I think field names become unnecessarily large, and harder to parse and post-process (histogram_000064.000000.to.000128.000000 vs histogram_64.000000). The upper bounds of the histogram are redundant, as they'll always be the lower bound of the next bucket. So the longer field names bring no additional information, but are harder to analyze. But again, this is an opinion, I can understand if you want to keep things closer to the original data. I'll change the PR if needed.

(I also found two issues with your suggested code: x on line 96 should be stat, and line 97 can be dropped, as it raises an error about statTokens being defined but not uses, but that's a detail)

The option for collecting these stats sounds like a good idea. I'll work on that.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I pushed two additional commits adding an option for collecting histogram statistics, and documenting it. Let me know how you stand on the field names issue. I'm also considering dropping this PR, which is getting a bit messy, and make a new cleaner one, once it is all settled.

field := strings.ReplaceAll(stat, ".", "_")
fields[field] = fieldValue
Expand Down
84 changes: 82 additions & 2 deletions plugins/inputs/unbound/unbound_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestParseFullOutput(t *testing.T) {
require.True(t, acc.HasMeasurement("unbound"))

require.Len(t, acc.Metrics, 1)
require.Equal(t, 63, acc.NFields())
require.Equal(t, 103, acc.NFields())

acc.AssertContainsFields(t, "unbound", parsedFullOutput)
}
Expand All @@ -46,7 +46,7 @@ func TestParseFullOutputThreadAsTag(t *testing.T) {
require.True(t, acc.HasMeasurement("unbound_threads"))

require.Len(t, acc.Metrics, 2)
require.Equal(t, 63, acc.NFields())
require.Equal(t, 103, acc.NFields())

acc.AssertContainsFields(t, "unbound", parsedFullOutputThreadAsTagMeasurementUnbound)
acc.AssertContainsFields(t, "unbound_threads", parsedFullOutputThreadAsTagMeasurementUnboundThreads)
Expand Down Expand Up @@ -87,6 +87,46 @@ var parsedFullOutput = map[string]interface{}{
"mem_cache_message": float64(320000),
"mem_mod_iterator": float64(16532),
"mem_mod_validator": float64(112097),
"histogram_0.000000": float64(20),
"histogram_0.000001": float64(5),
"histogram_0.000002": float64(13),
"histogram_0.000004": float64(18),
"histogram_0.000008": float64(67),
"histogram_0.000016": float64(94),
"histogram_0.000032": float64(113),
"histogram_0.000064": float64(190),
"histogram_0.000128": float64(369),
"histogram_0.000256": float64(1034),
"histogram_0.000512": float64(5503),
"histogram_0.001024": float64(155724),
"histogram_0.002048": float64(107623),
"histogram_0.004096": float64(17739),
"histogram_0.008192": float64(4177),
"histogram_0.016384": float64(82021),
"histogram_0.032768": float64(33772),
"histogram_0.065536": float64(7159),
"histogram_0.131072": float64(1109),
"histogram_0.262144": float64(295),
"histogram_0.524288": float64(890),
"histogram_1.000000": float64(136),
"histogram_1024.000000": float64(0),
"histogram_128.000000": float64(0),
"histogram_131072.000000": float64(0),
"histogram_16.000000": float64(2),
"histogram_16384.000000": float64(0),
"histogram_2.000000": float64(233),
"histogram_2048.000000": float64(0),
"histogram_256.000000": float64(0),
"histogram_262144.000000": float64(0),
"histogram_32.000000": float64(0),
"histogram_32768.000000": float64(0),
"histogram_4.000000": float64(2),
"histogram_4096.000000": float64(0),
"histogram_512.000000": float64(0),
"histogram_64.000000": float64(0),
"histogram_65536.000000": float64(0),
"histogram_8.000000": float64(0),
"histogram_8192.000000": float64(0),
"num_query_type_A": float64(7062688),
"num_query_type_PTR": float64(43097),
"num_query_type_TXT": float64(2998),
Expand Down Expand Up @@ -156,6 +196,46 @@ var parsedFullOutputThreadAsTagMeasurementUnbound = map[string]interface{}{
"mem_cache_message": float64(320000),
"mem_mod_iterator": float64(16532),
"mem_mod_validator": float64(112097),
"histogram_0.000000": float64(20),
"histogram_0.000001": float64(5),
"histogram_0.000002": float64(13),
"histogram_0.000004": float64(18),
"histogram_0.000008": float64(67),
"histogram_0.000016": float64(94),
"histogram_0.000032": float64(113),
"histogram_0.000064": float64(190),
"histogram_0.000128": float64(369),
"histogram_0.000256": float64(1034),
"histogram_0.000512": float64(5503),
"histogram_0.001024": float64(155724),
"histogram_0.002048": float64(107623),
"histogram_0.004096": float64(17739),
"histogram_0.008192": float64(4177),
"histogram_0.016384": float64(82021),
"histogram_0.032768": float64(33772),
"histogram_0.065536": float64(7159),
"histogram_0.131072": float64(1109),
"histogram_0.262144": float64(295),
"histogram_0.524288": float64(890),
"histogram_1.000000": float64(136),
"histogram_1024.000000": float64(0),
"histogram_128.000000": float64(0),
"histogram_131072.000000": float64(0),
"histogram_16.000000": float64(2),
"histogram_16384.000000": float64(0),
"histogram_2.000000": float64(233),
"histogram_2048.000000": float64(0),
"histogram_256.000000": float64(0),
"histogram_262144.000000": float64(0),
"histogram_32.000000": float64(0),
"histogram_32768.000000": float64(0),
"histogram_4.000000": float64(2),
"histogram_4096.000000": float64(0),
"histogram_512.000000": float64(0),
"histogram_64.000000": float64(0),
"histogram_65536.000000": float64(0),
"histogram_8.000000": float64(0),
"histogram_8192.000000": float64(0),
"num_query_type_A": float64(7062688),
"num_query_type_PTR": float64(43097),
"num_query_type_TXT": float64(2998),
Expand Down
Loading