chore: improve processor ordering (#12308)
This commit is contained in:
parent
c4dc104bb0
commit
337e4e34bc
|
|
@ -21,7 +21,6 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/coreos/go-semver/semver"
|
"github.com/coreos/go-semver/semver"
|
||||||
"github.com/google/uuid"
|
|
||||||
"github.com/influxdata/toml"
|
"github.com/influxdata/toml"
|
||||||
"github.com/influxdata/toml/ast"
|
"github.com/influxdata/toml/ast"
|
||||||
|
|
||||||
|
|
@ -69,8 +68,11 @@ type Config struct {
|
||||||
Outputs []*models.RunningOutput
|
Outputs []*models.RunningOutput
|
||||||
Aggregators []*models.RunningAggregator
|
Aggregators []*models.RunningAggregator
|
||||||
// Processors have a slice wrapper type because they need to be sorted
|
// Processors have a slice wrapper type because they need to be sorted
|
||||||
Processors models.RunningProcessors
|
Processors models.RunningProcessors
|
||||||
AggProcessors models.RunningProcessors
|
AggProcessors models.RunningProcessors
|
||||||
|
fileProcessors OrderedPlugins
|
||||||
|
fileAggProcessors OrderedPlugins
|
||||||
|
|
||||||
// Parsers are created by their inputs during gather. Config doesn't keep track of them
|
// Parsers are created by their inputs during gather. Config doesn't keep track of them
|
||||||
// like the other plugins because they need to be garbage collected (See issue #11809)
|
// like the other plugins because they need to be garbage collected (See issue #11809)
|
||||||
|
|
||||||
|
|
@ -78,6 +80,17 @@ type Config struct {
|
||||||
version *semver.Version
|
version *semver.Version
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Ordered plugins used to keep the order in which they appear in a file
|
||||||
|
type OrderedPlugin struct {
|
||||||
|
Line int
|
||||||
|
plugin any
|
||||||
|
}
|
||||||
|
type OrderedPlugins []*OrderedPlugin
|
||||||
|
|
||||||
|
func (op OrderedPlugins) Len() int { return len(op) }
|
||||||
|
func (op OrderedPlugins) Swap(i, j int) { op[i], op[j] = op[j], op[i] }
|
||||||
|
func (op OrderedPlugins) Less(i, j int) bool { return op[i].Line < op[j].Line }
|
||||||
|
|
||||||
// NewConfig creates a new struct to hold the Telegraf config.
|
// NewConfig creates a new struct to hold the Telegraf config.
|
||||||
// For historical reasons, It holds the actual instances of the running plugins
|
// For historical reasons, It holds the actual instances of the running plugins
|
||||||
// once the configuration is parsed.
|
// once the configuration is parsed.
|
||||||
|
|
@ -95,14 +108,16 @@ func NewConfig() *Config {
|
||||||
LogfileRotationMaxArchives: 5,
|
LogfileRotationMaxArchives: 5,
|
||||||
},
|
},
|
||||||
|
|
||||||
Tags: make(map[string]string),
|
Tags: make(map[string]string),
|
||||||
Inputs: make([]*models.RunningInput, 0),
|
Inputs: make([]*models.RunningInput, 0),
|
||||||
Outputs: make([]*models.RunningOutput, 0),
|
Outputs: make([]*models.RunningOutput, 0),
|
||||||
Processors: make([]*models.RunningProcessor, 0),
|
Processors: make([]*models.RunningProcessor, 0),
|
||||||
AggProcessors: make([]*models.RunningProcessor, 0),
|
AggProcessors: make([]*models.RunningProcessor, 0),
|
||||||
InputFilters: make([]string, 0),
|
fileProcessors: make([]*OrderedPlugin, 0),
|
||||||
OutputFilters: make([]string, 0),
|
fileAggProcessors: make([]*OrderedPlugin, 0),
|
||||||
Deprecations: make(map[string][]int64),
|
InputFilters: make([]string, 0),
|
||||||
|
OutputFilters: make([]string, 0),
|
||||||
|
Deprecations: make(map[string][]int64),
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle unknown version
|
// Handle unknown version
|
||||||
|
|
@ -391,14 +406,16 @@ func (c *Config) LoadAll(configFiles ...string) error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Sort the processors according to their `order` setting while
|
||||||
|
// using a stable sort to keep the file loading / file position order.
|
||||||
|
sort.Stable(c.Processors)
|
||||||
|
sort.Stable(c.AggProcessors)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// LoadConfigData loads TOML-formatted config data
|
// LoadConfigData loads TOML-formatted config data
|
||||||
func (c *Config) LoadConfigData(data []byte) error {
|
func (c *Config) LoadConfigData(data []byte) error {
|
||||||
// Create unique identifier for plugins to identify when using multiple configurations
|
|
||||||
id := uuid.New()
|
|
||||||
|
|
||||||
tbl, err := parseConfig(data)
|
tbl, err := parseConfig(data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("error parsing data: %s", err)
|
return fmt.Errorf("error parsing data: %s", err)
|
||||||
|
|
@ -450,6 +467,10 @@ func (c *Config) LoadConfigData(data []byte) error {
|
||||||
return fmt.Errorf("line %d: configuration specified the fields %q, but they weren't used", tbl.Line, keys(c.UnusedFields))
|
return fmt.Errorf("line %d: configuration specified the fields %q, but they weren't used", tbl.Line, keys(c.UnusedFields))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Initialize the file-sorting slices
|
||||||
|
c.fileProcessors = make(OrderedPlugins, 0)
|
||||||
|
c.fileAggProcessors = make(OrderedPlugins, 0)
|
||||||
|
|
||||||
// Parse all the rest of the plugins:
|
// Parse all the rest of the plugins:
|
||||||
for name, val := range tbl.Fields {
|
for name, val := range tbl.Fields {
|
||||||
subTable, ok := val.(*ast.Table)
|
subTable, ok := val.(*ast.Table)
|
||||||
|
|
@ -510,7 +531,7 @@ func (c *Config) LoadConfigData(data []byte) error {
|
||||||
switch pluginSubTable := pluginVal.(type) {
|
switch pluginSubTable := pluginVal.(type) {
|
||||||
case []*ast.Table:
|
case []*ast.Table:
|
||||||
for _, t := range pluginSubTable {
|
for _, t := range pluginSubTable {
|
||||||
if err = c.addProcessor(id.String(), pluginName, t); err != nil {
|
if err = c.addProcessor(pluginName, t); err != nil {
|
||||||
return fmt.Errorf("error parsing %s, %w", pluginName, err)
|
return fmt.Errorf("error parsing %s, %w", pluginName, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -555,8 +576,16 @@ func (c *Config) LoadConfigData(data []byte) error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(c.Processors) > 1 {
|
// Sort the processor according to the order they appeared in this file
|
||||||
sort.Sort(c.Processors)
|
// In a later stage, we sort them using the `order` option.
|
||||||
|
sort.Sort(c.fileProcessors)
|
||||||
|
for _, op := range c.fileProcessors {
|
||||||
|
c.Processors = append(c.Processors, op.plugin.(*models.RunningProcessor))
|
||||||
|
}
|
||||||
|
|
||||||
|
sort.Sort(c.fileAggProcessors)
|
||||||
|
for _, op := range c.fileAggProcessors {
|
||||||
|
c.AggProcessors = append(c.AggProcessors, op.plugin.(*models.RunningProcessor))
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|
@ -758,7 +787,7 @@ func (c *Config) addParser(parentcategory, parentname string, table *ast.Table)
|
||||||
return running, err
|
return running, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Config) addProcessor(id string, name string, table *ast.Table) error {
|
func (c *Config) addProcessor(name string, table *ast.Table) error {
|
||||||
creator, ok := processors.Processors[name]
|
creator, ok := processors.Processors[name]
|
||||||
if !ok {
|
if !ok {
|
||||||
// Handle removed, deprecated plugins
|
// Handle removed, deprecated plugins
|
||||||
|
|
@ -780,7 +809,7 @@ func (c *Config) addProcessor(id string, name string, table *ast.Table) error {
|
||||||
c.setLocalMissingTomlFieldTracker(missCount)
|
c.setLocalMissingTomlFieldTracker(missCount)
|
||||||
defer c.resetMissingTomlFieldTracker()
|
defer c.resetMissingTomlFieldTracker()
|
||||||
|
|
||||||
processorConfig, err := c.buildProcessor(id, name, table)
|
processorConfig, err := c.buildProcessor(name, table)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
@ -791,7 +820,7 @@ func (c *Config) addProcessor(id string, name string, table *ast.Table) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
rf := models.NewRunningProcessor(processorBefore, processorConfig)
|
rf := models.NewRunningProcessor(processorBefore, processorConfig)
|
||||||
c.Processors = append(c.Processors, rf)
|
c.fileProcessors = append(c.fileProcessors, &OrderedPlugin{table.Line, rf})
|
||||||
|
|
||||||
// Setup another (new) processor instance running after the aggregator
|
// Setup another (new) processor instance running after the aggregator
|
||||||
processorAfter, _, err := c.setupProcessor(processorConfig.Name, creator, table)
|
processorAfter, _, err := c.setupProcessor(processorConfig.Name, creator, table)
|
||||||
|
|
@ -799,7 +828,7 @@ func (c *Config) addProcessor(id string, name string, table *ast.Table) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
rf = models.NewRunningProcessor(processorAfter, processorConfig)
|
rf = models.NewRunningProcessor(processorAfter, processorConfig)
|
||||||
c.AggProcessors = append(c.AggProcessors, rf)
|
c.fileAggProcessors = append(c.fileAggProcessors, &OrderedPlugin{table.Line, rf})
|
||||||
|
|
||||||
// Check the number of misses against the threshold
|
// Check the number of misses against the threshold
|
||||||
if hasParser {
|
if hasParser {
|
||||||
|
|
@ -1074,12 +1103,8 @@ func (c *Config) buildParser(name string, tbl *ast.Table) *models.ParserConfig {
|
||||||
// buildProcessor parses Processor specific items from the ast.Table,
|
// buildProcessor parses Processor specific items from the ast.Table,
|
||||||
// builds the filter and returns a
|
// builds the filter and returns a
|
||||||
// models.ProcessorConfig to be inserted into models.RunningProcessor
|
// models.ProcessorConfig to be inserted into models.RunningProcessor
|
||||||
func (c *Config) buildProcessor(id string, name string, tbl *ast.Table) (*models.ProcessorConfig, error) {
|
func (c *Config) buildProcessor(name string, tbl *ast.Table) (*models.ProcessorConfig, error) {
|
||||||
conf := &models.ProcessorConfig{
|
conf := &models.ProcessorConfig{Name: name}
|
||||||
ID: id,
|
|
||||||
Name: name,
|
|
||||||
Line: tbl.Line,
|
|
||||||
}
|
|
||||||
|
|
||||||
c.getFieldInt64(tbl, "order", &conf.Order)
|
c.getFieldInt64(tbl, "order", &conf.Order)
|
||||||
c.getFieldString(tbl, "alias", &conf.Alias)
|
c.getFieldString(tbl, "alias", &conf.Alias)
|
||||||
|
|
|
||||||
|
|
@ -825,9 +825,11 @@ func TestConfig_MultipleProcessorsOrder(t *testing.T) {
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
t.Run(test.name, func(t *testing.T) {
|
t.Run(test.name, func(t *testing.T) {
|
||||||
c := NewConfig()
|
c := NewConfig()
|
||||||
for _, f := range test.filename {
|
filenames := make([]string, 0, len(test.filename))
|
||||||
require.NoError(t, c.LoadConfig(filepath.Join("./testdata/processor_order", f)))
|
for _, fn := range test.filename {
|
||||||
|
filenames = append(filenames, filepath.Join("./testdata/processor_order", fn))
|
||||||
}
|
}
|
||||||
|
require.NoError(t, c.LoadAll(filenames...))
|
||||||
|
|
||||||
require.Equal(t, len(test.expectedOrder), len(c.Processors))
|
require.Equal(t, len(test.expectedOrder), len(c.Processors))
|
||||||
|
|
||||||
|
|
@ -862,7 +864,7 @@ func TestConfig_ProcessorsWithParsers(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
c := NewConfig()
|
c := NewConfig()
|
||||||
require.NoError(t, c.LoadConfig("./testdata/processors_with_parsers.toml"))
|
require.NoError(t, c.LoadAll("./testdata/processors_with_parsers.toml"))
|
||||||
require.Len(t, c.Processors, len(formats))
|
require.Len(t, c.Processors, len(formats))
|
||||||
|
|
||||||
override := map[string]struct {
|
override := map[string]struct {
|
||||||
|
|
|
||||||
|
|
@ -16,48 +16,15 @@ type RunningProcessor struct {
|
||||||
|
|
||||||
type RunningProcessors []*RunningProcessor
|
type RunningProcessors []*RunningProcessor
|
||||||
|
|
||||||
func (rp RunningProcessors) Len() int {
|
func (rp RunningProcessors) Len() int { return len(rp) }
|
||||||
return len(rp)
|
func (rp RunningProcessors) Swap(i, j int) { rp[i], rp[j] = rp[j], rp[i] }
|
||||||
}
|
func (rp RunningProcessors) Less(i, j int) bool { return rp[i].Config.Order < rp[j].Config.Order }
|
||||||
func (rp RunningProcessors) Swap(i, j int) {
|
|
||||||
rp[i], rp[j] = rp[j], rp[i]
|
|
||||||
}
|
|
||||||
func (rp RunningProcessors) Less(i, j int) bool {
|
|
||||||
// If the processors are defined in separate files only sort based on order
|
|
||||||
if rp[i].Config.ID != rp[j].Config.ID {
|
|
||||||
return rp[i].Config.Order < rp[j].Config.Order
|
|
||||||
}
|
|
||||||
|
|
||||||
// If Order is defined for both processors, sort according to the number set
|
|
||||||
if rp[i].Config.Order != 0 && rp[j].Config.Order != 0 {
|
|
||||||
// If both orders are equal, ensure config order is maintained
|
|
||||||
if rp[i].Config.Order == rp[j].Config.Order {
|
|
||||||
return rp[i].Config.Line < rp[j].Config.Line
|
|
||||||
}
|
|
||||||
|
|
||||||
return rp[i].Config.Order < rp[j].Config.Order
|
|
||||||
}
|
|
||||||
|
|
||||||
// If "Order" is defined for one processor but not another,
|
|
||||||
// the processor without an "Order" will always take precedence.
|
|
||||||
// This adheres to the original implementation.
|
|
||||||
if rp[i].Config.Order != 0 {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
if rp[j].Config.Order != 0 {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
return rp[i].Config.Line < rp[j].Config.Line
|
|
||||||
}
|
|
||||||
|
|
||||||
// ProcessorConfig containing a name and filter
|
// ProcessorConfig containing a name and filter
|
||||||
type ProcessorConfig struct {
|
type ProcessorConfig struct {
|
||||||
ID string
|
|
||||||
Name string
|
Name string
|
||||||
Alias string
|
Alias string
|
||||||
Order int64
|
Order int64
|
||||||
Line int
|
|
||||||
Filter Filter
|
Filter Filter
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue