fix: Bring back old xpath section names (#11335)

This commit is contained in:
Sven Rebhan 2022-06-29 23:30:58 +02:00 committed by GitHub
parent e710192dc9
commit e5bcc274ca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 120 additions and 66 deletions

View File

@ -11,45 +11,24 @@ import (
"github.com/fatih/color" "github.com/fatih/color"
"github.com/influxdata/telegraf" "github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/models"
"github.com/influxdata/telegraf/plugins/aggregators" "github.com/influxdata/telegraf/plugins/aggregators"
"github.com/influxdata/telegraf/plugins/inputs" "github.com/influxdata/telegraf/plugins/inputs"
"github.com/influxdata/telegraf/plugins/outputs" "github.com/influxdata/telegraf/plugins/outputs"
"github.com/influxdata/telegraf/plugins/processors" "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 // deprecationInfo contains all important information to describe a deprecated entity
type deprecationInfo struct { type deprecationInfo struct {
// Name of the plugin or plugin option // Name of the plugin or plugin option
Name string Name string
// LogLevel is the level of deprecation which currently corresponds to a log-level // LogLevel is the level of deprecation which currently corresponds to a log-level
LogLevel Escalation LogLevel telegraf.Escalation
info telegraf.DeprecationInfo info telegraf.DeprecationInfo
} }
func (di *deprecationInfo) determineEscalation(telegrafVersion *semver.Version) error { func (di *deprecationInfo) determineEscalation(telegrafVersion *semver.Version) error {
di.LogLevel = None di.LogLevel = telegraf.None
if di.info.Since == "" { if di.info.Since == "" {
return nil return nil
} }
@ -78,9 +57,9 @@ func (di *deprecationInfo) determineEscalation(telegrafVersion *semver.Version)
Patch: telegrafVersion.Patch, Patch: telegrafVersion.Patch,
} }
if !version.LessThan(*removal) { if !version.LessThan(*removal) {
di.LogLevel = Error di.LogLevel = telegraf.Error
} else if !version.LessThan(*since) { } else if !version.LessThan(*since) {
di.LogLevel = Warn di.LogLevel = telegraf.Warn
} }
return nil return nil
} }
@ -113,7 +92,7 @@ func (c *Config) collectDeprecationInfo(category, name string, plugin interface{
info := pluginDeprecationInfo{ info := pluginDeprecationInfo{
deprecationInfo: deprecationInfo{ deprecationInfo: deprecationInfo{
Name: category + "." + name, 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 { if err := info.determineEscalation(c.version); err != nil {
panic(fmt.Errorf("plugin %q: %v", info.Name, err)) panic(fmt.Errorf("plugin %q: %v", info.Name, err))
} }
if info.LogLevel != None { if info.LogLevel != telegraf.None {
c.incrementPluginDeprecations(category) 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)) 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) 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 { func (c *Config) printUserDeprecation(category, name string, plugin interface{}) error {
info := c.collectDeprecationInfo(category, name, plugin, false) info := c.collectDeprecationInfo(category, name, plugin, false)
models.PrintPluginDeprecationNotice(info.LogLevel, info.Name, info.info)
switch info.LogLevel { if info.LogLevel == telegraf.Error {
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
return fmt.Errorf("plugin deprecated") return fmt.Errorf("plugin deprecated")
} }
// Print deprecated options // Print deprecated options
deprecatedOptions := make([]string, 0) deprecatedOptions := make([]string, 0)
for _, option := range info.Options { for _, option := range info.Options {
switch option.LogLevel { models.PrintOptionDeprecationNotice(option.LogLevel, info.Name, option.Name, option.info)
case Warn: if option.LogLevel == telegraf.Error {
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)
deprecatedOptions = append(deprecatedOptions, option.Name) deprecatedOptions = append(deprecatedOptions, option.Name)
} }
} }
@ -236,7 +202,7 @@ func (c *Config) CollectDeprecationInfos(inFilter, outFilter, aggFilter, procFil
plugin := creator() plugin := creator()
info := c.collectDeprecationInfo("inputs", name, plugin, true) 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) infos["inputs"] = append(infos["inputs"], info)
} }
} }
@ -250,7 +216,7 @@ func (c *Config) CollectDeprecationInfos(inFilter, outFilter, aggFilter, procFil
plugin := creator() plugin := creator()
info := c.collectDeprecationInfo("outputs", name, plugin, true) 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) infos["outputs"] = append(infos["outputs"], info)
} }
} }
@ -264,7 +230,7 @@ func (c *Config) CollectDeprecationInfos(inFilter, outFilter, aggFilter, procFil
plugin := creator() plugin := creator()
info := c.collectDeprecationInfo("processors", name, plugin, true) 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) infos["processors"] = append(infos["processors"], info)
} }
} }
@ -278,7 +244,7 @@ func (c *Config) CollectDeprecationInfos(inFilter, outFilter, aggFilter, procFil
plugin := creator() plugin := creator()
info := c.collectDeprecationInfo("aggregators", name, plugin, true) 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) infos["aggregators"] = append(infos["aggregators"], info)
} }
} }
@ -291,7 +257,7 @@ func (c *Config) PrintDeprecationList(plugins []pluginDeprecationInfo) {
for _, plugin := range plugins { for _, plugin := range plugins {
switch plugin.LogLevel { switch plugin.LogLevel {
case Warn, Error: case telegraf.Warn, telegraf.Error:
_, _ = fmt.Printf( _, _ = fmt.Printf(
" %-40s %-5s since %-5s removal in %-5s %s\n", " %-40s %-5s since %-5s removal in %-5s %s\n",
plugin.Name, plugin.LogLevel, plugin.info.Since, plugin.info.RemovalIn, plugin.info.Notice, 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) // 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. // and calls the given function for every visited field.
func walkPluginStruct(value reflect.Value, fn func(f reflect.StructField, fv reflect.Value)) { func walkPluginStruct(value reflect.Value, fn func(f reflect.StructField, fv reflect.Value)) {

View File

@ -4,6 +4,7 @@ import (
"log" "log"
"reflect" "reflect"
"github.com/fatih/color"
"github.com/influxdata/telegraf" "github.com/influxdata/telegraf"
) )
@ -101,3 +102,35 @@ func SetLoggerOnPlugin(i interface{}, logger telegraf.Logger) {
valI.Type().Name(), field.Type().String()) 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,
)
}

View File

@ -2,6 +2,28 @@ package telegraf
var Debug bool 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. // DeprecationInfo contains information for marking a plugin deprecated.
type DeprecationInfo struct { type DeprecationInfo struct {
// Since specifies the version since when the plugin is deprecated // Since specifies the version since when the plugin is deprecated

View File

@ -12,6 +12,7 @@ import (
"github.com/influxdata/telegraf" "github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/internal" "github.com/influxdata/telegraf/internal"
"github.com/influxdata/telegraf/metric" "github.com/influxdata/telegraf/metric"
"github.com/influxdata/telegraf/models"
"github.com/influxdata/telegraf/plugins/parsers" "github.com/influxdata/telegraf/plugins/parsers"
"github.com/influxdata/telegraf/plugins/parsers/temporary/xpath" "github.com/influxdata/telegraf/plugins/parsers/temporary/xpath"
) )
@ -38,6 +39,12 @@ type Parser struct {
DefaultTags map[string]string `toml:"-"` DefaultTags map[string]string `toml:"-"`
Log telegraf.Logger `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 document dataDocument
} }
@ -45,10 +52,40 @@ func (p *Parser) Init() error {
switch p.Format { switch p.Format {
case "", "xml": case "", "xml":
p.document = &xmlDocument{} 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": case "xpath_json":
p.document = &jsonDocument{} 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": case "xpath_msgpack":
p.document = &msgpackDocument{} 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": case "xpath_protobuf":
pbdoc := protobufDocument{ pbdoc := protobufDocument{
MessageDefinition: p.ProtobufMessageDef, MessageDefinition: p.ProtobufMessageDef,
@ -60,6 +97,16 @@ func (p *Parser) Init() error {
return err return err
} }
p.document = &pbdoc 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: default:
return fmt.Errorf("unknown data-format %q for xpath parser", p.Format) 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 { func (p *Parser) InitFromConfig(config *parsers.Config) error {
p.Format = config.DataFormat p.Format = config.DataFormat
if p.Format == "xpath_protobuf" { if p.Format == "xpath_protobuf" {