test(processors.clone): Add unit-test for tracking metrics (#14737)
This commit is contained in:
parent
98ec91478a
commit
a01d9624e2
|
|
@ -23,25 +23,26 @@ func (*Clone) SampleConfig() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Clone) Apply(in ...telegraf.Metric) []telegraf.Metric {
|
func (c *Clone) Apply(in ...telegraf.Metric) []telegraf.Metric {
|
||||||
cloned := []telegraf.Metric{}
|
out := make([]telegraf.Metric, 0, 2*len(in))
|
||||||
|
|
||||||
for _, metric := range in {
|
|
||||||
cloned = append(cloned, metric.Copy())
|
|
||||||
|
|
||||||
|
for _, original := range in {
|
||||||
|
m := original.Copy()
|
||||||
if len(c.NameOverride) > 0 {
|
if len(c.NameOverride) > 0 {
|
||||||
metric.SetName(c.NameOverride)
|
m.SetName(c.NameOverride)
|
||||||
}
|
}
|
||||||
if len(c.NamePrefix) > 0 {
|
if len(c.NamePrefix) > 0 {
|
||||||
metric.AddPrefix(c.NamePrefix)
|
m.AddPrefix(c.NamePrefix)
|
||||||
}
|
}
|
||||||
if len(c.NameSuffix) > 0 {
|
if len(c.NameSuffix) > 0 {
|
||||||
metric.AddSuffix(c.NameSuffix)
|
m.AddSuffix(c.NameSuffix)
|
||||||
}
|
}
|
||||||
for key, value := range c.Tags {
|
for key, value := range c.Tags {
|
||||||
metric.AddTag(key, value)
|
m.AddTag(key, value)
|
||||||
}
|
}
|
||||||
|
out = append(out, m)
|
||||||
}
|
}
|
||||||
return append(in, cloned...)
|
|
||||||
|
return append(out, in...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
package clone
|
package clone
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
|
@ -8,77 +9,234 @@ 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 TestRetainsTags(t *testing.T) {
|
||||||
m := metric.New("m1",
|
input := metric.New(
|
||||||
|
"m1",
|
||||||
map[string]string{"metric_tag": "from_metric"},
|
map[string]string{"metric_tag": "from_metric"},
|
||||||
map[string]interface{}{"value": int64(1)},
|
map[string]interface{}{"value": int64(1)},
|
||||||
time.Now(),
|
time.Unix(0, 0),
|
||||||
)
|
)
|
||||||
return m
|
|
||||||
}
|
|
||||||
|
|
||||||
func calculateProcessedTags(processor Clone, m telegraf.Metric) map[string]string {
|
expected := []telegraf.Metric{
|
||||||
processed := processor.Apply(m)
|
metric.New(
|
||||||
return processed[0].Tags()
|
"m1",
|
||||||
}
|
map[string]string{"metric_tag": "from_metric"},
|
||||||
|
map[string]interface{}{"value": int64(1)},
|
||||||
|
time.Unix(0, 0),
|
||||||
|
),
|
||||||
|
metric.New(
|
||||||
|
"m1",
|
||||||
|
map[string]string{"metric_tag": "from_metric"},
|
||||||
|
map[string]interface{}{"value": int64(1)},
|
||||||
|
time.Unix(0, 0),
|
||||||
|
),
|
||||||
|
}
|
||||||
|
|
||||||
func TestRetainsTags(t *testing.T) {
|
plugin := &Clone{}
|
||||||
processor := Clone{}
|
actual := plugin.Apply(input)
|
||||||
|
testutil.RequireMetricsEqual(t, expected, actual)
|
||||||
tags := calculateProcessedTags(processor, createTestMetric())
|
|
||||||
|
|
||||||
value, present := tags["metric_tag"]
|
|
||||||
require.True(t, present, "Tag of metric was not present")
|
|
||||||
require.Equal(t, "from_metric", value, "Value of Tag was changed")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAddTags(t *testing.T) {
|
func TestAddTags(t *testing.T) {
|
||||||
processor := Clone{Tags: map[string]string{"added_tag": "from_config", "another_tag": ""}}
|
input := metric.New(
|
||||||
|
"m1",
|
||||||
|
map[string]string{"metric_tag": "from_metric"},
|
||||||
|
map[string]interface{}{"value": int64(1)},
|
||||||
|
time.Unix(0, 0),
|
||||||
|
)
|
||||||
|
|
||||||
tags := calculateProcessedTags(processor, createTestMetric())
|
expected := []telegraf.Metric{
|
||||||
|
metric.New(
|
||||||
|
"m1",
|
||||||
|
map[string]string{
|
||||||
|
"metric_tag": "from_metric",
|
||||||
|
"added_tag": "from_config",
|
||||||
|
"another_tag": "",
|
||||||
|
},
|
||||||
|
map[string]interface{}{"value": int64(1)},
|
||||||
|
time.Unix(0, 0),
|
||||||
|
),
|
||||||
|
metric.New(
|
||||||
|
"m1",
|
||||||
|
map[string]string{"metric_tag": "from_metric"},
|
||||||
|
map[string]interface{}{"value": int64(1)},
|
||||||
|
time.Unix(0, 0),
|
||||||
|
),
|
||||||
|
}
|
||||||
|
|
||||||
value, present := tags["added_tag"]
|
plugin := &Clone{
|
||||||
require.True(t, present, "Additional Tag of metric was not present")
|
Tags: map[string]string{
|
||||||
require.Equal(t, "from_config", value, "Value of Tag was changed")
|
"added_tag": "from_config",
|
||||||
require.Len(t, tags, 3, "Should have one previous and two added tags.")
|
"another_tag": "",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
actual := plugin.Apply(input)
|
||||||
|
testutil.RequireMetricsEqual(t, expected, actual)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestOverwritesPresentTagValues(t *testing.T) {
|
func TestOverwritesPresentTagValues(t *testing.T) {
|
||||||
processor := Clone{Tags: map[string]string{"metric_tag": "from_config"}}
|
input := metric.New(
|
||||||
|
"m1",
|
||||||
|
map[string]string{"metric_tag": "from_metric"},
|
||||||
|
map[string]interface{}{"value": int64(1)},
|
||||||
|
time.Unix(0, 0),
|
||||||
|
)
|
||||||
|
|
||||||
tags := calculateProcessedTags(processor, createTestMetric())
|
expected := []telegraf.Metric{
|
||||||
|
metric.New(
|
||||||
|
"m1",
|
||||||
|
map[string]string{"metric_tag": "from_config"},
|
||||||
|
map[string]interface{}{"value": int64(1)},
|
||||||
|
time.Unix(0, 0),
|
||||||
|
),
|
||||||
|
metric.New(
|
||||||
|
"m1",
|
||||||
|
map[string]string{"metric_tag": "from_metric"},
|
||||||
|
map[string]interface{}{"value": int64(1)},
|
||||||
|
time.Unix(0, 0),
|
||||||
|
),
|
||||||
|
}
|
||||||
|
|
||||||
value, present := tags["metric_tag"]
|
plugin := &Clone{
|
||||||
require.True(t, present, "Tag of metric was not present")
|
Tags: map[string]string{"metric_tag": "from_config"},
|
||||||
require.Len(t, tags, 1, "Should only have one tag.")
|
}
|
||||||
require.Equal(t, "from_config", value, "Value of Tag was not changed")
|
actual := plugin.Apply(input)
|
||||||
|
testutil.RequireMetricsEqual(t, expected, actual)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestOverridesName(t *testing.T) {
|
func TestOverridesName(t *testing.T) {
|
||||||
processor := Clone{NameOverride: "overridden"}
|
input := metric.New(
|
||||||
|
"m1",
|
||||||
|
map[string]string{"metric_tag": "from_metric"},
|
||||||
|
map[string]interface{}{"value": int64(1)},
|
||||||
|
time.Unix(0, 0),
|
||||||
|
)
|
||||||
|
|
||||||
processed := processor.Apply(createTestMetric())
|
expected := []telegraf.Metric{
|
||||||
|
metric.New(
|
||||||
|
"overridden",
|
||||||
|
map[string]string{"metric_tag": "from_metric"},
|
||||||
|
map[string]interface{}{"value": int64(1)},
|
||||||
|
time.Unix(0, 0),
|
||||||
|
),
|
||||||
|
metric.New(
|
||||||
|
"m1",
|
||||||
|
map[string]string{"metric_tag": "from_metric"},
|
||||||
|
map[string]interface{}{"value": int64(1)},
|
||||||
|
time.Unix(0, 0),
|
||||||
|
),
|
||||||
|
}
|
||||||
|
|
||||||
require.Equal(t, "overridden", processed[0].Name(), "Name was not overridden")
|
plugin := &Clone{NameOverride: "overridden"}
|
||||||
require.Equal(t, "m1", processed[1].Name(), "Original metric was modified")
|
actual := plugin.Apply(input)
|
||||||
|
testutil.RequireMetricsEqual(t, expected, actual)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestNamePrefix(t *testing.T) {
|
func TestNamePrefix(t *testing.T) {
|
||||||
processor := Clone{NamePrefix: "Pre-"}
|
input := metric.New(
|
||||||
|
"m1",
|
||||||
|
map[string]string{"metric_tag": "from_metric"},
|
||||||
|
map[string]interface{}{"value": int64(1)},
|
||||||
|
time.Unix(0, 0),
|
||||||
|
)
|
||||||
|
|
||||||
processed := processor.Apply(createTestMetric())
|
expected := []telegraf.Metric{
|
||||||
|
metric.New(
|
||||||
|
"Pre-m1",
|
||||||
|
map[string]string{"metric_tag": "from_metric"},
|
||||||
|
map[string]interface{}{"value": int64(1)},
|
||||||
|
time.Unix(0, 0),
|
||||||
|
),
|
||||||
|
metric.New(
|
||||||
|
"m1",
|
||||||
|
map[string]string{"metric_tag": "from_metric"},
|
||||||
|
map[string]interface{}{"value": int64(1)},
|
||||||
|
time.Unix(0, 0),
|
||||||
|
),
|
||||||
|
}
|
||||||
|
|
||||||
require.Equal(t, "Pre-m1", processed[0].Name(), "Prefix was not applied")
|
plugin := &Clone{NamePrefix: "Pre-"}
|
||||||
require.Equal(t, "m1", processed[1].Name(), "Original metric was modified")
|
actual := plugin.Apply(input)
|
||||||
|
testutil.RequireMetricsEqual(t, expected, actual)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestNameSuffix(t *testing.T) {
|
func TestNameSuffix(t *testing.T) {
|
||||||
processor := Clone{NameSuffix: "-suff"}
|
input := metric.New(
|
||||||
|
"m1",
|
||||||
|
map[string]string{"metric_tag": "from_metric"},
|
||||||
|
map[string]interface{}{"value": int64(1)},
|
||||||
|
time.Unix(0, 0),
|
||||||
|
)
|
||||||
|
|
||||||
processed := processor.Apply(createTestMetric())
|
expected := []telegraf.Metric{
|
||||||
|
metric.New(
|
||||||
|
"m1-suff",
|
||||||
|
map[string]string{"metric_tag": "from_metric"},
|
||||||
|
map[string]interface{}{"value": int64(1)},
|
||||||
|
time.Unix(0, 0),
|
||||||
|
),
|
||||||
|
metric.New(
|
||||||
|
"m1",
|
||||||
|
map[string]string{"metric_tag": "from_metric"},
|
||||||
|
map[string]interface{}{"value": int64(1)},
|
||||||
|
time.Unix(0, 0),
|
||||||
|
),
|
||||||
|
}
|
||||||
|
|
||||||
require.Equal(t, "m1-suff", processed[0].Name(), "Suffix was not applied")
|
plugin := &Clone{NameSuffix: "-suff"}
|
||||||
require.Equal(t, "m1", processed[1].Name(), "Original metric was modified")
|
actual := plugin.Apply(input)
|
||||||
|
testutil.RequireMetricsEqual(t, expected, actual)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestTracking(t *testing.T) {
|
||||||
|
inputRaw := []telegraf.Metric{
|
||||||
|
metric.New(
|
||||||
|
"m1",
|
||||||
|
map[string]string{"metric_tag": "from_metric"},
|
||||||
|
map[string]interface{}{"value": int64(1)},
|
||||||
|
time.Now(),
|
||||||
|
),
|
||||||
|
metric.New(
|
||||||
|
"m2",
|
||||||
|
map[string]string{"metric_tag": "foo_metric"},
|
||||||
|
map[string]interface{}{"value": int64(2)},
|
||||||
|
time.Now(),
|
||||||
|
),
|
||||||
|
}
|
||||||
|
|
||||||
|
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))
|
||||||
|
expected := make([]telegraf.Metric, 0, 2*len(input))
|
||||||
|
for _, m := range inputRaw {
|
||||||
|
tm, _ := metric.WithTracking(m, notify)
|
||||||
|
input = append(input, tm)
|
||||||
|
expected = append(expected, m)
|
||||||
|
}
|
||||||
|
expected = append(expected, input...)
|
||||||
|
|
||||||
|
// Process expected metrics and compare with resulting metrics
|
||||||
|
plugin := &Clone{}
|
||||||
|
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