Skip to content

Commit e5bcc27

Browse files
authored
fix: Bring back old xpath section names (#11335)
1 parent e710192 commit e5bcc27

File tree

4 files changed

+120
-66
lines changed

4 files changed

+120
-66
lines changed

config/deprecation.go

Lines changed: 17 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -11,45 +11,24 @@ import (
1111
"github.com/fatih/color"
1212

1313
"github.com/influxdata/telegraf"
14+
"github.com/influxdata/telegraf/models"
1415
"github.com/influxdata/telegraf/plugins/aggregators"
1516
"github.com/influxdata/telegraf/plugins/inputs"
1617
"github.com/influxdata/telegraf/plugins/outputs"
1718
"github.com/influxdata/telegraf/plugins/processors"
1819
)
1920

20-
// Escalation level for the plugin or option
21-
type Escalation int
22-
23-
func (e Escalation) String() string {
24-
switch e {
25-
case Warn:
26-
return "WARN"
27-
case Error:
28-
return "ERROR"
29-
}
30-
return "NONE"
31-
}
32-
33-
const (
34-
// None means no deprecation
35-
None Escalation = iota
36-
// Warn means deprecated but still within the grace period
37-
Warn
38-
// Error means deprecated and beyond grace period
39-
Error
40-
)
41-
4221
// deprecationInfo contains all important information to describe a deprecated entity
4322
type deprecationInfo struct {
4423
// Name of the plugin or plugin option
4524
Name string
4625
// LogLevel is the level of deprecation which currently corresponds to a log-level
47-
LogLevel Escalation
26+
LogLevel telegraf.Escalation
4827
info telegraf.DeprecationInfo
4928
}
5029

5130
func (di *deprecationInfo) determineEscalation(telegrafVersion *semver.Version) error {
52-
di.LogLevel = None
31+
di.LogLevel = telegraf.None
5332
if di.info.Since == "" {
5433
return nil
5534
}
@@ -78,9 +57,9 @@ func (di *deprecationInfo) determineEscalation(telegrafVersion *semver.Version)
7857
Patch: telegrafVersion.Patch,
7958
}
8059
if !version.LessThan(*removal) {
81-
di.LogLevel = Error
60+
di.LogLevel = telegraf.Error
8261
} else if !version.LessThan(*since) {
83-
di.LogLevel = Warn
62+
di.LogLevel = telegraf.Warn
8463
}
8564
return nil
8665
}
@@ -113,7 +92,7 @@ func (c *Config) collectDeprecationInfo(category, name string, plugin interface{
11392
info := pluginDeprecationInfo{
11493
deprecationInfo: deprecationInfo{
11594
Name: category + "." + name,
116-
LogLevel: None,
95+
LogLevel: telegraf.None,
11796
},
11897
}
11998

@@ -139,7 +118,7 @@ func (c *Config) collectDeprecationInfo(category, name string, plugin interface{
139118
if err := info.determineEscalation(c.version); err != nil {
140119
panic(fmt.Errorf("plugin %q: %v", info.Name, err))
141120
}
142-
if info.LogLevel != None {
121+
if info.LogLevel != telegraf.None {
143122
c.incrementPluginDeprecations(category)
144123
}
145124

@@ -172,7 +151,7 @@ func (c *Config) collectDeprecationInfo(category, name string, plugin interface{
172151
panic(fmt.Errorf("plugin %q option %q: %v", info.Name, field.Name, err))
173152
}
174153

175-
if optionInfo.LogLevel != None {
154+
if optionInfo.LogLevel != telegraf.None {
176155
c.incrementPluginOptionDeprecations(category)
177156
}
178157

@@ -189,30 +168,17 @@ func (c *Config) collectDeprecationInfo(category, name string, plugin interface{
189168

190169
func (c *Config) printUserDeprecation(category, name string, plugin interface{}) error {
191170
info := c.collectDeprecationInfo(category, name, plugin, false)
171+
models.PrintPluginDeprecationNotice(info.LogLevel, info.Name, info.info)
192172

193-
switch info.LogLevel {
194-
case Warn:
195-
prefix := "W! " + color.YellowString("DeprecationWarning")
196-
printPluginDeprecationNotice(prefix, info.Name, info.info)
197-
// We will not check for any deprecated options as the whole plugin is deprecated anyway.
198-
return nil
199-
case Error:
200-
prefix := "E! " + color.RedString("DeprecationError")
201-
printPluginDeprecationNotice(prefix, info.Name, info.info)
202-
// We are past the grace period
173+
if info.LogLevel == telegraf.Error {
203174
return fmt.Errorf("plugin deprecated")
204175
}
205176

206177
// Print deprecated options
207178
deprecatedOptions := make([]string, 0)
208179
for _, option := range info.Options {
209-
switch option.LogLevel {
210-
case Warn:
211-
prefix := "W! " + color.YellowString("DeprecationWarning")
212-
printOptionDeprecationNotice(prefix, info.Name, option.Name, option.info)
213-
case Error:
214-
prefix := "E! " + color.RedString("DeprecationError")
215-
printOptionDeprecationNotice(prefix, info.Name, option.Name, option.info)
180+
models.PrintOptionDeprecationNotice(option.LogLevel, info.Name, option.Name, option.info)
181+
if option.LogLevel == telegraf.Error {
216182
deprecatedOptions = append(deprecatedOptions, option.Name)
217183
}
218184
}
@@ -236,7 +202,7 @@ func (c *Config) CollectDeprecationInfos(inFilter, outFilter, aggFilter, procFil
236202
plugin := creator()
237203
info := c.collectDeprecationInfo("inputs", name, plugin, true)
238204

239-
if info.LogLevel != None || len(info.Options) > 0 {
205+
if info.LogLevel != telegraf.None || len(info.Options) > 0 {
240206
infos["inputs"] = append(infos["inputs"], info)
241207
}
242208
}
@@ -250,7 +216,7 @@ func (c *Config) CollectDeprecationInfos(inFilter, outFilter, aggFilter, procFil
250216
plugin := creator()
251217
info := c.collectDeprecationInfo("outputs", name, plugin, true)
252218

253-
if info.LogLevel != None || len(info.Options) > 0 {
219+
if info.LogLevel != telegraf.None || len(info.Options) > 0 {
254220
infos["outputs"] = append(infos["outputs"], info)
255221
}
256222
}
@@ -264,7 +230,7 @@ func (c *Config) CollectDeprecationInfos(inFilter, outFilter, aggFilter, procFil
264230
plugin := creator()
265231
info := c.collectDeprecationInfo("processors", name, plugin, true)
266232

267-
if info.LogLevel != None || len(info.Options) > 0 {
233+
if info.LogLevel != telegraf.None || len(info.Options) > 0 {
268234
infos["processors"] = append(infos["processors"], info)
269235
}
270236
}
@@ -278,7 +244,7 @@ func (c *Config) CollectDeprecationInfos(inFilter, outFilter, aggFilter, procFil
278244
plugin := creator()
279245
info := c.collectDeprecationInfo("aggregators", name, plugin, true)
280246

281-
if info.LogLevel != None || len(info.Options) > 0 {
247+
if info.LogLevel != telegraf.None || len(info.Options) > 0 {
282248
infos["aggregators"] = append(infos["aggregators"], info)
283249
}
284250
}
@@ -291,7 +257,7 @@ func (c *Config) PrintDeprecationList(plugins []pluginDeprecationInfo) {
291257

292258
for _, plugin := range plugins {
293259
switch plugin.LogLevel {
294-
case Warn, Error:
260+
case telegraf.Warn, telegraf.Error:
295261
_, _ = fmt.Printf(
296262
" %-40s %-5s since %-5s removal in %-5s %s\n",
297263
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
319285
)
320286
}
321287

322-
func printPluginDeprecationNotice(prefix, name string, info telegraf.DeprecationInfo) {
323-
log.Printf(
324-
"%s: Plugin %q deprecated since version %s and will be removed in %s: %s",
325-
prefix, name, info.Since, info.RemovalIn, info.Notice,
326-
)
327-
}
328-
329-
func printOptionDeprecationNotice(prefix, plugin, option string, info telegraf.DeprecationInfo) {
330-
log.Printf(
331-
"%s: Option %q of plugin %q deprecated since version %s and will be removed in %s: %s",
332-
prefix, option, plugin, info.Since, info.RemovalIn, info.Notice,
333-
)
334-
}
335-
336288
// walkPluginStruct iterates over the fields of a structure in depth-first search (to cover nested structures)
337289
// and calls the given function for every visited field.
338290
func walkPluginStruct(value reflect.Value, fn func(f reflect.StructField, fv reflect.Value)) {

models/log.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"log"
55
"reflect"
66

7+
"github.com/fatih/color"
78
"github.com/influxdata/telegraf"
89
)
910

@@ -101,3 +102,35 @@ func SetLoggerOnPlugin(i interface{}, logger telegraf.Logger) {
101102
valI.Type().Name(), field.Type().String())
102103
}
103104
}
105+
106+
func PrintPluginDeprecationNotice(level telegraf.Escalation, name string, info telegraf.DeprecationInfo) {
107+
var prefix string
108+
109+
switch level {
110+
case telegraf.Warn:
111+
prefix = "W! " + color.YellowString("DeprecationWarning")
112+
case telegraf.Error:
113+
prefix = "E! " + color.RedString("DeprecationError")
114+
}
115+
116+
log.Printf(
117+
"%s: Plugin %q deprecated since version %s and will be removed in %s: %s",
118+
prefix, name, info.Since, info.RemovalIn, info.Notice,
119+
)
120+
}
121+
122+
func PrintOptionDeprecationNotice(level telegraf.Escalation, plugin, option string, info telegraf.DeprecationInfo) {
123+
var prefix string
124+
125+
switch level {
126+
case telegraf.Warn:
127+
prefix = "W! " + color.YellowString("DeprecationWarning")
128+
case telegraf.Error:
129+
prefix = "E! " + color.RedString("DeprecationError")
130+
}
131+
132+
log.Printf(
133+
"%s: Option %q of plugin %q deprecated since version %s and will be removed in %s: %s",
134+
prefix, option, plugin, info.Since, info.RemovalIn, info.Notice,
135+
)
136+
}

plugin.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,28 @@ package telegraf
22

33
var Debug bool
44

5+
// Escalation level for the plugin or option
6+
type Escalation int
7+
8+
func (e Escalation) String() string {
9+
switch e {
10+
case Warn:
11+
return "WARN"
12+
case Error:
13+
return "ERROR"
14+
}
15+
return "NONE"
16+
}
17+
18+
const (
19+
// None means no deprecation
20+
None Escalation = iota
21+
// Warn means deprecated but still within the grace period
22+
Warn
23+
// Error means deprecated and beyond grace period
24+
Error
25+
)
26+
527
// DeprecationInfo contains information for marking a plugin deprecated.
628
type DeprecationInfo struct {
729
// Since specifies the version since when the plugin is deprecated

plugins/parsers/xpath/parser.go

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/influxdata/telegraf"
1313
"github.com/influxdata/telegraf/internal"
1414
"github.com/influxdata/telegraf/metric"
15+
"github.com/influxdata/telegraf/models"
1516
"github.com/influxdata/telegraf/plugins/parsers"
1617
"github.com/influxdata/telegraf/plugins/parsers/temporary/xpath"
1718
)
@@ -38,17 +39,53 @@ type Parser struct {
3839
DefaultTags map[string]string `toml:"-"`
3940
Log telegraf.Logger `toml:"-"`
4041

42+
// Required for backward compatibility
43+
ConfigsXML []xpath.Config `toml:"xml" deprecated:"1.23.1;use 'xpath' instead"`
44+
ConfigsJSON []xpath.Config `toml:"xpath_json"`
45+
ConfigsMsgPack []xpath.Config `toml:"xpath_msgpack"`
46+
ConfigsProto []xpath.Config `toml:"xpath_protobuf"`
47+
4148
document dataDocument
4249
}
4350

4451
func (p *Parser) Init() error {
4552
switch p.Format {
4653
case "", "xml":
4754
p.document = &xmlDocument{}
55+
56+
// Required for backward compatibility
57+
if len(p.ConfigsXML) > 0 {
58+
p.Configs = append(p.Configs, p.ConfigsXML...)
59+
models.PrintOptionDeprecationNotice(telegraf.Warn, "parsers.xpath", "xml", telegraf.DeprecationInfo{
60+
Since: "1.23.1",
61+
RemovalIn: "2.0.0",
62+
Notice: "use 'xpath' instead",
63+
})
64+
}
4865
case "xpath_json":
4966
p.document = &jsonDocument{}
67+
68+
// Required for backward compatibility
69+
if len(p.ConfigsJSON) > 0 {
70+
p.Configs = append(p.Configs, p.ConfigsJSON...)
71+
models.PrintOptionDeprecationNotice(telegraf.Warn, "parsers.xpath", "xpath_json", telegraf.DeprecationInfo{
72+
Since: "1.23.1",
73+
RemovalIn: "2.0.0",
74+
Notice: "use 'xpath' instead",
75+
})
76+
}
5077
case "xpath_msgpack":
5178
p.document = &msgpackDocument{}
79+
80+
// Required for backward compatibility
81+
if len(p.ConfigsMsgPack) > 0 {
82+
p.Configs = append(p.Configs, p.ConfigsMsgPack...)
83+
models.PrintOptionDeprecationNotice(telegraf.Warn, "parsers.xpath", "xpath_msgpack", telegraf.DeprecationInfo{
84+
Since: "1.23.1",
85+
RemovalIn: "2.0.0",
86+
Notice: "use 'xpath' instead",
87+
})
88+
}
5289
case "xpath_protobuf":
5390
pbdoc := protobufDocument{
5491
MessageDefinition: p.ProtobufMessageDef,
@@ -60,6 +97,16 @@ func (p *Parser) Init() error {
6097
return err
6198
}
6299
p.document = &pbdoc
100+
101+
// Required for backward compatibility
102+
if len(p.ConfigsProto) > 0 {
103+
p.Configs = append(p.Configs, p.ConfigsProto...)
104+
models.PrintOptionDeprecationNotice(telegraf.Warn, "parsers.xpath", "xpath_proto", telegraf.DeprecationInfo{
105+
Since: "1.23.1",
106+
RemovalIn: "2.0.0",
107+
Notice: "use 'xpath' instead",
108+
})
109+
}
63110
default:
64111
return fmt.Errorf("unknown data-format %q for xpath parser", p.Format)
65112
}
@@ -534,7 +581,7 @@ func init() {
534581
)
535582
}
536583

537-
// InitFromConfig is a compatibitlity function to construct the parser the old way
584+
// InitFromConfig is a compatibility function to construct the parser the old way
538585
func (p *Parser) InitFromConfig(config *parsers.Config) error {
539586
p.Format = config.DataFormat
540587
if p.Format == "xpath_protobuf" {

0 commit comments

Comments
 (0)