test(processors.override): Add unit-test for tracking metrics (#14781)

This commit is contained in:
Sven Rebhan 2024-02-13 14:41:57 +01:00 committed by GitHub
parent c0f8647eb1
commit b3cd1fde05
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 87 additions and 4 deletions

View File

@ -12,10 +12,10 @@ import (
var sampleConfig string
type Override struct {
NameOverride string
NamePrefix string
NameSuffix string
Tags map[string]string
NameOverride string `toml:"name_override"`
NamePrefix string `toml:"name_prefix"`
NameSuffix string `toml:"name_suffix"`
Tags map[string]string `toml:"tag"`
}
func (*Override) SampleConfig() string {

View File

@ -1,6 +1,7 @@
package override
import (
"sync"
"testing"
"time"
@ -8,6 +9,7 @@ import (
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/metric"
"github.com/influxdata/telegraf/testutil"
)
func createTestMetric() telegraf.Metric {
@ -79,3 +81,84 @@ func TestNameSuffix(t *testing.T) {
require.Equal(t, "m1-suff", processed[0].Name(), "Suffix was not applied")
}
func TestTracking(t *testing.T) {
// Setup raw input and expected output
inputRaw := []telegraf.Metric{
metric.New(
"zero_uint64",
map[string]string{},
map[string]interface{}{"value": uint64(3)},
time.Unix(0, 0),
),
metric.New(
"zero_int64",
map[string]string{},
map[string]interface{}{"value": int64(4)},
time.Unix(0, 0),
),
metric.New(
"zero_float",
map[string]string{},
map[string]interface{}{"value": float64(5.5)},
time.Unix(0, 0),
),
}
expected := []telegraf.Metric{
metric.New(
"test",
map[string]string{},
map[string]interface{}{"value": uint64(3)},
time.Unix(0, 0),
),
metric.New(
"test",
map[string]string{},
map[string]interface{}{"value": int64(4)},
time.Unix(0, 0),
),
metric.New(
"test",
map[string]string{},
map[string]interface{}{"value": float64(5.5)},
time.Unix(0, 0),
),
}
// Create fake notification for testing
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)
}
// Convert raw input to tracking metric
input := make([]telegraf.Metric, 0, len(inputRaw))
for _, m := range inputRaw {
tm, _ := metric.WithTracking(m, notify)
input = append(input, tm)
}
// Prepare and start the plugin
plugin := &Override{
NameOverride: "test",
}
// 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(input) == len(delivered)
}, time.Second, 100*time.Millisecond, "%d delivered but %d expected", len(delivered), len(expected))
}