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

test(processors.tag_limit): Add unit-tests for tracking metrics #14812

Merged
merged 1 commit into from
Feb 14, 2024
Merged
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
66 changes: 66 additions & 0 deletions plugins/processors/tag_limit/tag_limit_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package tag_limit

import (
"sync"
"testing"
"time"

"github.com/stretchr/testify/require"

"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/metric"
"github.com/influxdata/telegraf/testutil"
)

func MustMetric(name string, tags map[string]string, fields map[string]interface{}, metricTime time.Time) telegraf.Metric {
Expand Down Expand Up @@ -85,3 +87,67 @@ func TestTrim(t *testing.T) {
require.Equal(t, "foo", trimmedTags["a"], "preserved: a")
require.Equal(t, "bar", trimmedTags["b"], "preserved: b")
}

func TestTracking(t *testing.T) {
inputRaw := []telegraf.Metric{
metric.New("foo", map[string]string{"tag": "testing"}, map[string]interface{}{"value": 42}, time.Unix(0, 0)),
metric.New("bar", map[string]string{"tag": "other", "host": "locahost"}, map[string]interface{}{"value": 23}, time.Unix(0, 0)),
metric.New("baz", map[string]string{"tag": "value", "host": "locahost", "module": "main"}, map[string]interface{}{"value": 99}, time.Unix(0, 0)),
}

var mu sync.Mutex
delivered := make([]telegraf.DeliveryInfo, 0, len(inputRaw))
notify := func(di telegraf.DeliveryInfo) {
mu.Lock()
defer mu.Unlock()
delivered = append(delivered, di)
}

input := make([]telegraf.Metric, 0, len(inputRaw))
for _, m := range inputRaw {
tm, _ := metric.WithTracking(m, notify)
input = append(input, tm)
}

expected := []telegraf.Metric{
metric.New(
"foo",
map[string]string{"tag": "testing"},
map[string]interface{}{"value": 42},
time.Unix(0, 0),
),
metric.New(
"bar",
map[string]string{"tag": "other", "host": "locahost"},
map[string]interface{}{"value": 23},
time.Unix(0, 0),
),
metric.New(
"baz",
map[string]string{"tag": "value", "host": "locahost"},
map[string]interface{}{"value": 99},
time.Unix(0, 0),
),
}

plugin := &TagLimit{
Limit: 2,
Keep: []string{"tag", "host"},
}

// Process expected metrics and compare with resulting metrics
actual := plugin.Apply(input...)
testutil.RequireMetricsEqual(t, expected, actual)

// Simulate output acknowledging delivery
for _, m := range actual {
m.Accept()
}

// Check delivery
require.Eventuallyf(t, func() bool {
mu.Lock()
defer mu.Unlock()
return len(expected) == len(delivered)
}, time.Second, 100*time.Millisecond, "%d delivered but %d expected", len(delivered), len(expected))
}
Loading