From ed1b79012fce84ba47d98710f9053aab9f23602c Mon Sep 17 00:00:00 2001 From: Sven Rebhan <36194019+srebhan@users.noreply.github.com> Date: Wed, 29 Jun 2022 23:30:58 +0200 Subject: [PATCH] fix: Bring back old xpath section names (#11335) --- config/deprecation.go | 82 +++++++-------------------------- models/log.go | 33 +++++++++++++ plugin.go | 22 +++++++++ plugins/parsers/xpath/parser.go | 49 +++++++++++++++++++- 4 files changed, 120 insertions(+), 66 deletions(-) diff --git a/config/deprecation.go b/config/deprecation.go index ab5d2a0caba46..f8c13904b5a3a 100644 --- a/config/deprecation.go +++ b/config/deprecation.go @@ -11,45 +11,24 @@ import ( "github.com/fatih/color" "github.com/influxdata/telegraf" + "github.com/influxdata/telegraf/models" "github.com/influxdata/telegraf/plugins/aggregators" "github.com/influxdata/telegraf/plugins/inputs" "github.com/influxdata/telegraf/plugins/outputs" "github.com/influxdata/telegraf/plugins/processors" ) -// Escalation level for the plugin or option -type Escalation int - -func (e Escalation) String() string { - switch e { - case Warn: - return "WARN" - case Error: - return "ERROR" - } - return "NONE" -} - -const ( - // None means no deprecation - None Escalation = iota - // Warn means deprecated but still within the grace period - Warn - // Error means deprecated and beyond grace period - Error -) - // deprecationInfo contains all important information to describe a deprecated entity type deprecationInfo struct { // Name of the plugin or plugin option Name string // LogLevel is the level of deprecation which currently corresponds to a log-level - LogLevel Escalation + LogLevel telegraf.Escalation info telegraf.DeprecationInfo } func (di *deprecationInfo) determineEscalation(telegrafVersion *semver.Version) error { - di.LogLevel = None + di.LogLevel = telegraf.None if di.info.Since == "" { return nil } @@ -78,9 +57,9 @@ func (di *deprecationInfo) determineEscalation(telegrafVersion *semver.Version) Patch: telegrafVersion.Patch, } if !version.LessThan(*removal) { - di.LogLevel = Error + di.LogLevel = telegraf.Error } else if !version.LessThan(*since) { - di.LogLevel = Warn + di.LogLevel = telegraf.Warn } return nil } @@ -113,7 +92,7 @@ func (c *Config) collectDeprecationInfo(category, name string, plugin interface{ info := pluginDeprecationInfo{ deprecationInfo: deprecationInfo{ Name: category + "." + name, - LogLevel: None, + LogLevel: telegraf.None, }, } @@ -139,7 +118,7 @@ func (c *Config) collectDeprecationInfo(category, name string, plugin interface{ if err := info.determineEscalation(c.version); err != nil { panic(fmt.Errorf("plugin %q: %v", info.Name, err)) } - if info.LogLevel != None { + if info.LogLevel != telegraf.None { c.incrementPluginDeprecations(category) } @@ -172,7 +151,7 @@ func (c *Config) collectDeprecationInfo(category, name string, plugin interface{ panic(fmt.Errorf("plugin %q option %q: %v", info.Name, field.Name, err)) } - if optionInfo.LogLevel != None { + if optionInfo.LogLevel != telegraf.None { c.incrementPluginOptionDeprecations(category) } @@ -189,30 +168,17 @@ func (c *Config) collectDeprecationInfo(category, name string, plugin interface{ func (c *Config) printUserDeprecation(category, name string, plugin interface{}) error { info := c.collectDeprecationInfo(category, name, plugin, false) + models.PrintPluginDeprecationNotice(info.LogLevel, info.Name, info.info) - switch info.LogLevel { - case Warn: - prefix := "W! " + color.YellowString("DeprecationWarning") - printPluginDeprecationNotice(prefix, info.Name, info.info) - // We will not check for any deprecated options as the whole plugin is deprecated anyway. - return nil - case Error: - prefix := "E! " + color.RedString("DeprecationError") - printPluginDeprecationNotice(prefix, info.Name, info.info) - // We are past the grace period + if info.LogLevel == telegraf.Error { return fmt.Errorf("plugin deprecated") } // Print deprecated options deprecatedOptions := make([]string, 0) for _, option := range info.Options { - switch option.LogLevel { - case Warn: - prefix := "W! " + color.YellowString("DeprecationWarning") - printOptionDeprecationNotice(prefix, info.Name, option.Name, option.info) - case Error: - prefix := "E! " + color.RedString("DeprecationError") - printOptionDeprecationNotice(prefix, info.Name, option.Name, option.info) + models.PrintOptionDeprecationNotice(option.LogLevel, info.Name, option.Name, option.info) + if option.LogLevel == telegraf.Error { deprecatedOptions = append(deprecatedOptions, option.Name) } } @@ -236,7 +202,7 @@ func (c *Config) CollectDeprecationInfos(inFilter, outFilter, aggFilter, procFil plugin := creator() info := c.collectDeprecationInfo("inputs", name, plugin, true) - if info.LogLevel != None || len(info.Options) > 0 { + if info.LogLevel != telegraf.None || len(info.Options) > 0 { infos["inputs"] = append(infos["inputs"], info) } } @@ -250,7 +216,7 @@ func (c *Config) CollectDeprecationInfos(inFilter, outFilter, aggFilter, procFil plugin := creator() info := c.collectDeprecationInfo("outputs", name, plugin, true) - if info.LogLevel != None || len(info.Options) > 0 { + if info.LogLevel != telegraf.None || len(info.Options) > 0 { infos["outputs"] = append(infos["outputs"], info) } } @@ -264,7 +230,7 @@ func (c *Config) CollectDeprecationInfos(inFilter, outFilter, aggFilter, procFil plugin := creator() info := c.collectDeprecationInfo("processors", name, plugin, true) - if info.LogLevel != None || len(info.Options) > 0 { + if info.LogLevel != telegraf.None || len(info.Options) > 0 { infos["processors"] = append(infos["processors"], info) } } @@ -278,7 +244,7 @@ func (c *Config) CollectDeprecationInfos(inFilter, outFilter, aggFilter, procFil plugin := creator() info := c.collectDeprecationInfo("aggregators", name, plugin, true) - if info.LogLevel != None || len(info.Options) > 0 { + if info.LogLevel != telegraf.None || len(info.Options) > 0 { infos["aggregators"] = append(infos["aggregators"], info) } } @@ -291,7 +257,7 @@ func (c *Config) PrintDeprecationList(plugins []pluginDeprecationInfo) { for _, plugin := range plugins { switch plugin.LogLevel { - case Warn, Error: + case telegraf.Warn, telegraf.Error: _, _ = fmt.Printf( " %-40s %-5s since %-5s removal in %-5s %s\n", plugin.Name, plugin.LogLevel, plugin.info.Since, plugin.info.RemovalIn, plugin.info.Notice, @@ -319,20 +285,6 @@ func printHistoricPluginDeprecationNotice(category, name string, info telegraf.D ) } -func printPluginDeprecationNotice(prefix, name string, info telegraf.DeprecationInfo) { - log.Printf( - "%s: Plugin %q deprecated since version %s and will be removed in %s: %s", - prefix, name, info.Since, info.RemovalIn, info.Notice, - ) -} - -func printOptionDeprecationNotice(prefix, plugin, option string, info telegraf.DeprecationInfo) { - log.Printf( - "%s: Option %q of plugin %q deprecated since version %s and will be removed in %s: %s", - prefix, option, plugin, info.Since, info.RemovalIn, info.Notice, - ) -} - // walkPluginStruct iterates over the fields of a structure in depth-first search (to cover nested structures) // and calls the given function for every visited field. func walkPluginStruct(value reflect.Value, fn func(f reflect.StructField, fv reflect.Value)) { diff --git a/models/log.go b/models/log.go index cad98f789afe6..c401d1b4601c2 100644 --- a/models/log.go +++ b/models/log.go @@ -4,6 +4,7 @@ import ( "log" "reflect" + "github.com/fatih/color" "github.com/influxdata/telegraf" ) @@ -101,3 +102,35 @@ func SetLoggerOnPlugin(i interface{}, logger telegraf.Logger) { valI.Type().Name(), field.Type().String()) } } + +func PrintPluginDeprecationNotice(level telegraf.Escalation, name string, info telegraf.DeprecationInfo) { + var prefix string + + switch level { + case telegraf.Warn: + prefix = "W! " + color.YellowString("DeprecationWarning") + case telegraf.Error: + prefix = "E! " + color.RedString("DeprecationError") + } + + log.Printf( + "%s: Plugin %q deprecated since version %s and will be removed in %s: %s", + prefix, name, info.Since, info.RemovalIn, info.Notice, + ) +} + +func PrintOptionDeprecationNotice(level telegraf.Escalation, plugin, option string, info telegraf.DeprecationInfo) { + var prefix string + + switch level { + case telegraf.Warn: + prefix = "W! " + color.YellowString("DeprecationWarning") + case telegraf.Error: + prefix = "E! " + color.RedString("DeprecationError") + } + + log.Printf( + "%s: Option %q of plugin %q deprecated since version %s and will be removed in %s: %s", + prefix, option, plugin, info.Since, info.RemovalIn, info.Notice, + ) +} diff --git a/plugin.go b/plugin.go index 72421a5301276..d20d057b51e5d 100644 --- a/plugin.go +++ b/plugin.go @@ -2,6 +2,28 @@ package telegraf var Debug bool +// Escalation level for the plugin or option +type Escalation int + +func (e Escalation) String() string { + switch e { + case Warn: + return "WARN" + case Error: + return "ERROR" + } + return "NONE" +} + +const ( + // None means no deprecation + None Escalation = iota + // Warn means deprecated but still within the grace period + Warn + // Error means deprecated and beyond grace period + Error +) + // DeprecationInfo contains information for marking a plugin deprecated. type DeprecationInfo struct { // Since specifies the version since when the plugin is deprecated diff --git a/plugins/parsers/xpath/parser.go b/plugins/parsers/xpath/parser.go index 91e9872061503..361d3e1f18660 100644 --- a/plugins/parsers/xpath/parser.go +++ b/plugins/parsers/xpath/parser.go @@ -12,6 +12,7 @@ import ( "github.com/influxdata/telegraf" "github.com/influxdata/telegraf/internal" "github.com/influxdata/telegraf/metric" + "github.com/influxdata/telegraf/models" "github.com/influxdata/telegraf/plugins/parsers" "github.com/influxdata/telegraf/plugins/parsers/temporary/xpath" ) @@ -38,6 +39,12 @@ type Parser struct { DefaultTags map[string]string `toml:"-"` Log telegraf.Logger `toml:"-"` + // Required for backward compatibility + ConfigsXML []xpath.Config `toml:"xml" deprecated:"1.23.1;use 'xpath' instead"` + ConfigsJSON []xpath.Config `toml:"xpath_json"` + ConfigsMsgPack []xpath.Config `toml:"xpath_msgpack"` + ConfigsProto []xpath.Config `toml:"xpath_protobuf"` + document dataDocument } @@ -45,10 +52,40 @@ func (p *Parser) Init() error { switch p.Format { case "", "xml": p.document = &xmlDocument{} + + // Required for backward compatibility + if len(p.ConfigsXML) > 0 { + p.Configs = append(p.Configs, p.ConfigsXML...) + models.PrintOptionDeprecationNotice(telegraf.Warn, "parsers.xpath", "xml", telegraf.DeprecationInfo{ + Since: "1.23.1", + RemovalIn: "2.0.0", + Notice: "use 'xpath' instead", + }) + } case "xpath_json": p.document = &jsonDocument{} + + // Required for backward compatibility + if len(p.ConfigsJSON) > 0 { + p.Configs = append(p.Configs, p.ConfigsJSON...) + models.PrintOptionDeprecationNotice(telegraf.Warn, "parsers.xpath", "xpath_json", telegraf.DeprecationInfo{ + Since: "1.23.1", + RemovalIn: "2.0.0", + Notice: "use 'xpath' instead", + }) + } case "xpath_msgpack": p.document = &msgpackDocument{} + + // Required for backward compatibility + if len(p.ConfigsMsgPack) > 0 { + p.Configs = append(p.Configs, p.ConfigsMsgPack...) + models.PrintOptionDeprecationNotice(telegraf.Warn, "parsers.xpath", "xpath_msgpack", telegraf.DeprecationInfo{ + Since: "1.23.1", + RemovalIn: "2.0.0", + Notice: "use 'xpath' instead", + }) + } case "xpath_protobuf": pbdoc := protobufDocument{ MessageDefinition: p.ProtobufMessageDef, @@ -60,6 +97,16 @@ func (p *Parser) Init() error { return err } p.document = &pbdoc + + // Required for backward compatibility + if len(p.ConfigsProto) > 0 { + p.Configs = append(p.Configs, p.ConfigsProto...) + models.PrintOptionDeprecationNotice(telegraf.Warn, "parsers.xpath", "xpath_proto", telegraf.DeprecationInfo{ + Since: "1.23.1", + RemovalIn: "2.0.0", + Notice: "use 'xpath' instead", + }) + } default: return fmt.Errorf("unknown data-format %q for xpath parser", p.Format) } @@ -534,7 +581,7 @@ func init() { ) } -// InitFromConfig is a compatibitlity function to construct the parser the old way +// InitFromConfig is a compatibility function to construct the parser the old way func (p *Parser) InitFromConfig(config *parsers.Config) error { p.Format = config.DataFormat if p.Format == "xpath_protobuf" {