test(processors.override): Add unit-test for tracking metrics (#14781)
This commit is contained in:
parent
c0f8647eb1
commit
b3cd1fde05
|
|
@ -12,10 +12,10 @@ import (
|
||||||
var sampleConfig string
|
var sampleConfig string
|
||||||
|
|
||||||
type Override struct {
|
type Override struct {
|
||||||
NameOverride string
|
NameOverride string `toml:"name_override"`
|
||||||
NamePrefix string
|
NamePrefix string `toml:"name_prefix"`
|
||||||
NameSuffix string
|
NameSuffix string `toml:"name_suffix"`
|
||||||
Tags map[string]string
|
Tags map[string]string `toml:"tag"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (*Override) SampleConfig() string {
|
func (*Override) SampleConfig() string {
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
package override
|
package override
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
|
@ -8,6 +9,7 @@ import (
|
||||||
|
|
||||||
"github.com/influxdata/telegraf"
|
"github.com/influxdata/telegraf"
|
||||||
"github.com/influxdata/telegraf/metric"
|
"github.com/influxdata/telegraf/metric"
|
||||||
|
"github.com/influxdata/telegraf/testutil"
|
||||||
)
|
)
|
||||||
|
|
||||||
func createTestMetric() telegraf.Metric {
|
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")
|
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))
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue