1
0
Fork 0

Adding upstream version 1.34.4.

Signed-off-by: Daniel Baumann <daniel@debian.org>
This commit is contained in:
Daniel Baumann 2025-05-24 07:26:29 +02:00
parent e393c3af3f
commit 4978089aab
Signed by: daniel
GPG key ID: FBB4F0E80A80222F
4963 changed files with 677545 additions and 0 deletions

View file

@ -0,0 +1,112 @@
package general_metricfilter
import (
"fmt"
"github.com/influxdata/toml"
"github.com/influxdata/toml/ast"
"github.com/influxdata/telegraf/internal/choice"
"github.com/influxdata/telegraf/migrations"
)
// Migration function
func migrate(category, name string, tbl *ast.Table) ([]byte, string, error) {
// Filter options can only be present in inputs, outputs, processors and
// aggregators. Skip everything else...
switch category {
case "inputs", "outputs", "processors", "aggregators":
default:
return nil, "", migrations.ErrNotApplicable
}
// Decode the old data structure
var plugin map[string]interface{}
if err := toml.UnmarshalTable(tbl, &plugin); err != nil {
return nil, "", err
}
// Check for deprecated option(s) and migrate them
var applied bool
// Get the new field settings to be able to merge it with the deprecated
// settings
var fieldinclude []string
if newFieldInclude, found := plugin["fieldinclude"]; found {
var err error
fieldinclude, err = migrations.AsStringSlice(newFieldInclude)
if err != nil {
return nil, "", fmt.Errorf("setting 'fieldinclude': %w", err)
}
}
for _, option := range []string{"pass", "fieldpass"} {
if rawOld, found := plugin[option]; found {
applied = true
old, err := migrations.AsStringSlice(rawOld)
if err != nil {
return nil, "", fmt.Errorf("setting '%s': %w", option, err)
}
for _, o := range old {
if !choice.Contains(o, fieldinclude) {
fieldinclude = append(fieldinclude, o)
}
}
// Remove the deprecated setting
delete(plugin, option)
}
}
// Add the new option if it has data
if len(fieldinclude) > 0 {
plugin["fieldinclude"] = fieldinclude
}
var fieldexclude []string
if newFieldExclude, found := plugin["fieldexclude"]; found {
var err error
fieldexclude, err = migrations.AsStringSlice(newFieldExclude)
if err != nil {
return nil, "", fmt.Errorf("setting 'fieldexclude': %w", err)
}
}
for _, option := range []string{"drop", "fielddrop"} {
if rawOld, found := plugin[option]; found {
applied = true
old, err := migrations.AsStringSlice(rawOld)
if err != nil {
return nil, "", fmt.Errorf("setting '%s': %w", option, err)
}
for _, o := range old {
if !choice.Contains(o, fieldexclude) {
fieldexclude = append(fieldexclude, o)
}
}
// Remove the deprecated setting
delete(plugin, option)
}
}
// Add the new option if it has data
if len(fieldexclude) > 0 {
plugin["fieldexclude"] = fieldexclude
}
// No options migrated so we can exit early
if !applied {
return nil, "", migrations.ErrNotApplicable
}
// Create the corresponding plugin configurations
cfg := migrations.CreateTOMLStruct(category, name)
cfg.Add(category, name, plugin)
output, err := toml.Marshal(cfg)
return output, "", err
}
// Register the migration function for the plugin type
func init() {
migrations.AddGeneralMigration(migrate)
}

View file

@ -0,0 +1,96 @@
package general_metricfilter_test
import (
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/require"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/config"
_ "github.com/influxdata/telegraf/migrations/general_metricfilter" // register migration
"github.com/influxdata/telegraf/plugins/inputs"
)
func TestNoMigration(t *testing.T) {
cfg := []byte(`
# Dummy plugin
[[inputs.dummy]]
## A dummy server
servers = ["tcp://127.0.0.1:1883"]
## A commented option
# timeout = "10s"
`)
// Migrate and check that nothing changed
output, n, err := config.ApplyMigrations(cfg)
require.NoError(t, err)
require.NotEmpty(t, output)
require.Zero(t, n)
require.Equal(t, string(cfg), string(output))
}
func TestCases(t *testing.T) {
// Get all directories in testdata
folders, err := os.ReadDir("testcases")
require.NoError(t, err)
inputs.Add("dummy", func() telegraf.Input { return &MockupInputPlugin{} })
for _, f := range folders {
// Only handle folders
if !f.IsDir() {
continue
}
t.Run(f.Name(), func(t *testing.T) {
testcasePath := filepath.Join("testcases", f.Name())
inputFile := filepath.Join(testcasePath, "telegraf.conf")
expectedFile := filepath.Join(testcasePath, "expected.conf")
// Read the expected output
expected := config.NewConfig()
require.NoError(t, expected.LoadConfig(expectedFile))
require.NotEmpty(t, expected.Inputs)
// Read the input data
input, remote, err := config.LoadConfigFile(inputFile)
require.NoError(t, err)
require.False(t, remote)
require.NotEmpty(t, input)
// Migrate
output, n, err := config.ApplyMigrations(input)
require.NoError(t, err)
require.NotEmpty(t, output)
require.GreaterOrEqual(t, n, uint64(1))
actual := config.NewConfig()
require.NoError(t, actual.LoadConfigData(output, config.EmptySourcePath))
// Test the output
require.Len(t, actual.Inputs, len(expected.Inputs))
actualIDs := make([]string, 0, len(expected.Inputs))
expectedIDs := make([]string, 0, len(expected.Inputs))
for i := range actual.Inputs {
actualIDs = append(actualIDs, actual.Inputs[i].ID())
expectedIDs = append(expectedIDs, expected.Inputs[i].ID())
}
require.ElementsMatch(t, expectedIDs, actualIDs, string(output))
})
}
}
// Implement a mock input plugin for testing
type MockupInputPlugin struct {
Servers []string `toml:"servers"`
Timeout config.Duration `toml:"timeout"`
}
func (*MockupInputPlugin) SampleConfig() string {
return "Mockup test input plugin"
}
func (*MockupInputPlugin) Gather(telegraf.Accumulator) error {
return nil
}

View file

@ -0,0 +1,3 @@
[[inputs.dummy]]
servers = ["tcp://127.0.0.1:1883"]
fieldexclude = ["value"]

View file

@ -0,0 +1,10 @@
# A dummy plugin
[[inputs.dummy]]
## A server
servers = ["tcp://127.0.0.1:1883"]
## Default timestamp
# timestamp = "10s"
## Deprecated drop
drop = ["value"]

View file

@ -0,0 +1,3 @@
[[inputs.dummy]]
servers = ["tcp://127.0.0.1:1883"]
fieldexclude = ["value"]

View file

@ -0,0 +1,10 @@
# A dummy plugin
[[inputs.dummy]]
## A server
servers = ["tcp://127.0.0.1:1883"]
## Default timestamp
# timestamp = "10s"
## Deprecated fielddrop
fielddrop = ["value"]

View file

@ -0,0 +1,3 @@
[[inputs.dummy]]
servers = ["tcp://127.0.0.1:1883"]
fieldinclude = ["value"]

View file

@ -0,0 +1,10 @@
# A dummy plugin
[[inputs.dummy]]
## A server
servers = ["tcp://127.0.0.1:1883"]
## Default timestamp
# timestamp = "10s"
## Deprecated fieldpass
fieldpass = ["value"]

View file

@ -0,0 +1,3 @@
[[inputs.dummy]]
servers = ["tcp://127.0.0.1:1883"]
fieldinclude = ["value"]

View file

@ -0,0 +1,10 @@
# A dummy plugin
[[inputs.dummy]]
## A server
servers = ["tcp://127.0.0.1:1883"]
## Default timestamp
# timestamp = "10s"
## Deprecated pass
pass = ["value"]

View file

@ -0,0 +1,4 @@
[[inputs.dummy]]
servers = ["tcp://127.0.0.1:1883"]
fieldexclude = ["bugA", "bugX", "bugB", "bugY", "bugC"]
fieldinclude = ["valueA", "valueX", "valueB", "valueY", "valueC"]

View file

@ -0,0 +1,15 @@
# A dummy plugin
[[inputs.dummy]]
## A server
servers = ["tcp://127.0.0.1:1883"]
## Default timestamp
# timestamp = "10s"
## Deprecated field options
fieldinclude = ["valueA", "valueX"]
fieldexclude = ["bugA", "bugX"]
drop = ["bugB", "bugX", "bugY"]
pass = ["valueB", "valueX", "valueY"]
fieldpass = ["valueY", "valueC", "valueX"]
fielddrop = ["bugY", "bugC", "bugX"]

View file

@ -0,0 +1,3 @@
[[inputs.dummy]]
servers = ["tcp://127.0.0.1:1883"]
fieldexclude = ["valueA", "valueB", "valueC"]

View file

@ -0,0 +1,12 @@
# A dummy plugin
[[inputs.dummy]]
## A server
servers = ["tcp://127.0.0.1:1883"]
## Default timestamp
# timestamp = "10s"
## Deprecated field-exclude options
fieldexclude = ["valueA"]
drop = ["valueB"]
fielddrop = ["valueC"]

View file

@ -0,0 +1,3 @@
[[inputs.dummy]]
servers = ["tcp://127.0.0.1:1883"]
fieldexclude = ["valueA", "valueX", "valueB", "valueY", "valueC"]

View file

@ -0,0 +1,12 @@
# A dummy plugin
[[inputs.dummy]]
## A server
servers = ["tcp://127.0.0.1:1883"]
## Default timestamp
# timestamp = "10s"
## Deprecated field-exclude options
fieldexclude = ["valueA", "valueX"]
drop = ["valueB", "valueX", "valueY"]
fielddrop = ["valueY", "valueC", "valueX"]

View file

@ -0,0 +1,3 @@
[[inputs.dummy]]
servers = ["tcp://127.0.0.1:1883"]
fieldinclude = ["valueA", "valueB", "valueC"]

View file

@ -0,0 +1,12 @@
# A dummy plugin
[[inputs.dummy]]
## A server
servers = ["tcp://127.0.0.1:1883"]
## Default timestamp
# timestamp = "10s"
## Deprecated field-include options
fieldinclude = ["valueA"]
pass = ["valueB"]
fieldpass = ["valueC"]

View file

@ -0,0 +1,3 @@
[[inputs.dummy]]
servers = ["tcp://127.0.0.1:1883"]
fieldinclude = ["valueA", "valueX", "valueB", "valueY", "valueC"]

View file

@ -0,0 +1,12 @@
# A dummy plugin
[[inputs.dummy]]
## A server
servers = ["tcp://127.0.0.1:1883"]
## Default timestamp
# timestamp = "10s"
## Deprecated field-include options
fieldinclude = ["valueA", "valueX"]
pass = ["valueB", "valueX", "valueY"]
fieldpass = ["valueY", "valueC", "valueX"]