Skip to content

Commit

Permalink
fix(processors.execd): Accept tracking metrics instead of dropping th…
Browse files Browse the repository at this point in the history
…em (#14770)

(cherry picked from commit 616ad30)
  • Loading branch information
srebhan authored and powersj committed Feb 20, 2024
1 parent 2bd8a83 commit 27b1ce9
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 1 deletion.
2 changes: 1 addition & 1 deletion plugins/processors/execd/execd.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func (e *Execd) Add(m telegraf.Metric, _ telegraf.Accumulator) error {
// We cannot maintain tracking metrics at the moment because input/output
// is done asynchronously and we don't have any metric metadata to tie the
// output metric back to the original input metric.
m.Drop()
m.Accept()
return nil
}

Expand Down
119 changes: 119 additions & 0 deletions plugins/processors/execd/execd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"os"
"path/filepath"
"sync"
"testing"
"time"

Expand Down Expand Up @@ -275,3 +276,121 @@ func TestCases(t *testing.T) {
})
}
}

func TestTracking(t *testing.T) {
now := time.Now()

// Setup the raw input and expected output data
inputRaw := []telegraf.Metric{
metric.New(
"test",
map[string]string{
"city": "Toronto",
},
map[string]interface{}{
"population": 6000000,
"count": 1,
},
now,
),
metric.New(
"test",
map[string]string{
"city": "Tokio",
},
map[string]interface{}{
"population": 14000000,
"count": 8,
},
now,
),
}

expected := []telegraf.Metric{
metric.New(
"test",
map[string]string{
"city": "Toronto",
},
map[string]interface{}{
"population": 6000000,
"count": 2,
},
now,
),
metric.New(
"test",
map[string]string{
"city": "Tokio",
},
map[string]interface{}{
"population": 14000000,
"count": 16,
},
now,
),
}

// Create a testing notifier
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 metrics
input := make([]telegraf.Metric, 0, len(inputRaw))
for _, m := range inputRaw {
tm, _ := metric.WithTracking(m, notify)
input = append(input, tm)
}

// Setup the plugin
exe, err := os.Executable()
require.NoError(t, err)

plugin := &Execd{
Command: []string{exe, "-countmultiplier"},
Environment: []string{"PLUGINS_PROCESSORS_EXECD_MODE=application", "FIELD_NAME=count"},
RestartDelay: config.Duration(5 * time.Second),
Log: testutil.Logger{},
}
require.NoError(t, plugin.Init())

parser := &influx.Parser{}
require.NoError(t, parser.Init())
plugin.SetParser(parser)

serializer := &influxSerializer.Serializer{}
require.NoError(t, serializer.Init())
plugin.SetSerializer(serializer)

var acc testutil.Accumulator
require.NoError(t, plugin.Start(&acc))
defer plugin.Stop()

// Process expected metrics and compare with resulting metrics
for _, in := range input {
require.NoError(t, plugin.Add(in, &acc))
}
require.Eventually(t, func() bool {
return int(acc.NMetrics()) >= len(expected)
}, 3*time.Second, 100*time.Millisecond)

actual := acc.GetTelegrafMetrics()
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))
}

0 comments on commit 27b1ce9

Please sign in to comment.