Skip to content

Commit

Permalink
fix: Bring back old xpath section names (#11335)
Browse files Browse the repository at this point in the history
  • Loading branch information
srebhan authored and MyaLongmire committed Jul 6, 2022
1 parent 003a45d commit ed1b790
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 66 deletions.
82 changes: 17 additions & 65 deletions config/deprecation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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,
},
}

Expand All @@ -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)
}

Expand Down Expand Up @@ -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)
}

Expand All @@ -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)
}
}
Expand All @@ -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)
}
}
Expand All @@ -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)
}
}
Expand All @@ -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)
}
}
Expand All @@ -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)
}
}
Expand All @@ -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,
Expand Down Expand Up @@ -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)) {
Expand Down
33 changes: 33 additions & 0 deletions models/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"log"
"reflect"

"github.com/fatih/color"
"github.com/influxdata/telegraf"
)

Expand Down Expand Up @@ -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,
)
}
22 changes: 22 additions & 0 deletions plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
49 changes: 48 additions & 1 deletion plugins/parsers/xpath/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -38,17 +39,53 @@ 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
}

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,
Expand All @@ -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)
}
Expand Down Expand Up @@ -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" {
Expand Down

0 comments on commit ed1b790

Please sign in to comment.