forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deprecation.go
330 lines (282 loc) · 9.33 KB
/
deprecation.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
package config
import (
"fmt"
"log" //nolint:revive // log is ok here as the logging facility is not set-up yet
"reflect"
"sort"
"strings"
"github.com/coreos/go-semver/semver"
"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"
)
// 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 telegraf.Escalation
info telegraf.DeprecationInfo
}
func (di *deprecationInfo) determineEscalation(telegrafVersion *semver.Version) error {
di.LogLevel = telegraf.None
if di.info.Since == "" {
return nil
}
since, err := semver.NewVersion(di.info.Since)
if err != nil {
return fmt.Errorf("cannot parse 'since' version %q: %v", di.info.Since, err)
}
var removal *semver.Version
if di.info.RemovalIn != "" {
removal, err = semver.NewVersion(di.info.RemovalIn)
if err != nil {
return fmt.Errorf("cannot parse 'removal' version %q: %v", di.info.RemovalIn, err)
}
} else {
removal = &semver.Version{Major: since.Major}
removal.BumpMajor()
di.info.RemovalIn = removal.String()
}
// Drop potential pre-release tags
version := semver.Version{
Major: telegrafVersion.Major,
Minor: telegrafVersion.Minor,
Patch: telegrafVersion.Patch,
}
if !version.LessThan(*removal) {
di.LogLevel = telegraf.Error
} else if !version.LessThan(*since) {
di.LogLevel = telegraf.Warn
}
return nil
}
// pluginDeprecationInfo holds all information about a deprecated plugin or it's options
type pluginDeprecationInfo struct {
deprecationInfo
// Options deprecated for this plugin
Options []deprecationInfo
}
func (c *Config) incrementPluginDeprecations(category string) {
newcounts := []int64{1, 0}
if counts, found := c.Deprecations[category]; found {
newcounts = []int64{counts[0] + 1, counts[1]}
}
c.Deprecations[category] = newcounts
}
func (c *Config) incrementPluginOptionDeprecations(category string) {
newcounts := []int64{0, 1}
if counts, found := c.Deprecations[category]; found {
newcounts = []int64{counts[0], counts[1] + 1}
}
c.Deprecations[category] = newcounts
}
func (c *Config) collectDeprecationInfo(category, name string, plugin interface{}, all bool) pluginDeprecationInfo {
info := pluginDeprecationInfo{
deprecationInfo: deprecationInfo{
Name: category + "." + name,
LogLevel: telegraf.None,
},
}
// First check if the whole plugin is deprecated
switch category {
case "aggregators":
if pi, deprecated := aggregators.Deprecations[name]; deprecated {
info.deprecationInfo.info = pi
}
case "inputs":
if pi, deprecated := inputs.Deprecations[name]; deprecated {
info.deprecationInfo.info = pi
}
case "outputs":
if pi, deprecated := outputs.Deprecations[name]; deprecated {
info.deprecationInfo.info = pi
}
case "processors":
if pi, deprecated := processors.Deprecations[name]; deprecated {
info.deprecationInfo.info = pi
}
}
if err := info.determineEscalation(c.version); err != nil {
panic(fmt.Errorf("plugin %q: %v", info.Name, err))
}
if info.LogLevel != telegraf.None {
c.incrementPluginDeprecations(category)
}
// Allow checking for names only.
if plugin == nil {
return info
}
// Check for deprecated options
walkPluginStruct(reflect.ValueOf(plugin), func(field reflect.StructField, value reflect.Value) {
// Try to report only those fields that are set
if !all && value.IsZero() {
return
}
tags := strings.SplitN(field.Tag.Get("deprecated"), ";", 3)
if len(tags) < 1 || tags[0] == "" {
return
}
optionInfo := deprecationInfo{Name: field.Name}
optionInfo.info.Since = tags[0]
if len(tags) > 1 {
optionInfo.info.Notice = tags[len(tags)-1]
}
if len(tags) > 2 {
optionInfo.info.RemovalIn = tags[1]
}
if err := optionInfo.determineEscalation(c.version); err != nil {
panic(fmt.Errorf("plugin %q option %q: %v", info.Name, field.Name, err))
}
if optionInfo.LogLevel != telegraf.None {
c.incrementPluginOptionDeprecations(category)
}
// Get the toml field name
option := field.Tag.Get("toml")
if option != "" {
optionInfo.Name = option
}
info.Options = append(info.Options, optionInfo)
})
return info
}
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)
if info.LogLevel == telegraf.Error {
return fmt.Errorf("plugin deprecated")
}
// Print deprecated options
deprecatedOptions := make([]string, 0)
for _, option := range info.Options {
models.PrintOptionDeprecationNotice(option.LogLevel, info.Name, option.Name, option.info)
if option.LogLevel == telegraf.Error {
deprecatedOptions = append(deprecatedOptions, option.Name)
}
}
if len(deprecatedOptions) > 0 {
return fmt.Errorf("plugin options %q deprecated", strings.Join(deprecatedOptions, ","))
}
return nil
}
func (c *Config) CollectDeprecationInfos(inFilter, outFilter, aggFilter, procFilter []string) map[string][]pluginDeprecationInfo {
infos := make(map[string][]pluginDeprecationInfo)
infos["inputs"] = make([]pluginDeprecationInfo, 0)
for name, creator := range inputs.Inputs {
if len(inFilter) > 0 && !sliceContains(name, inFilter) {
continue
}
plugin := creator()
info := c.collectDeprecationInfo("inputs", name, plugin, true)
if info.LogLevel != telegraf.None || len(info.Options) > 0 {
infos["inputs"] = append(infos["inputs"], info)
}
}
infos["outputs"] = make([]pluginDeprecationInfo, 0)
for name, creator := range outputs.Outputs {
if len(outFilter) > 0 && !sliceContains(name, outFilter) {
continue
}
plugin := creator()
info := c.collectDeprecationInfo("outputs", name, plugin, true)
if info.LogLevel != telegraf.None || len(info.Options) > 0 {
infos["outputs"] = append(infos["outputs"], info)
}
}
infos["processors"] = make([]pluginDeprecationInfo, 0)
for name, creator := range processors.Processors {
if len(procFilter) > 0 && !sliceContains(name, procFilter) {
continue
}
plugin := creator()
info := c.collectDeprecationInfo("processors", name, plugin, true)
if info.LogLevel != telegraf.None || len(info.Options) > 0 {
infos["processors"] = append(infos["processors"], info)
}
}
infos["aggregators"] = make([]pluginDeprecationInfo, 0)
for name, creator := range aggregators.Aggregators {
if len(aggFilter) > 0 && !sliceContains(name, aggFilter) {
continue
}
plugin := creator()
info := c.collectDeprecationInfo("aggregators", name, plugin, true)
if info.LogLevel != telegraf.None || len(info.Options) > 0 {
infos["aggregators"] = append(infos["aggregators"], info)
}
}
return infos
}
func (c *Config) PrintDeprecationList(plugins []pluginDeprecationInfo) {
sort.Slice(plugins, func(i, j int) bool { return plugins[i].Name < plugins[j].Name })
for _, plugin := range plugins {
switch plugin.LogLevel {
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,
)
}
if len(plugin.Options) < 1 {
continue
}
sort.Slice(plugin.Options, func(i, j int) bool { return plugin.Options[i].Name < plugin.Options[j].Name })
for _, option := range plugin.Options {
_, _ = fmt.Printf(
" %-40s %-5s since %-5s removal in %-5s %s\n",
plugin.Name+"/"+option.Name, option.LogLevel, option.info.Since, option.info.RemovalIn, option.info.Notice,
)
}
}
}
func printHistoricPluginDeprecationNotice(category, name string, info telegraf.DeprecationInfo) {
prefix := "E! " + color.RedString("DeprecationError")
log.Printf(
"%s: Plugin %q deprecated since version %s and removed: %s",
prefix, category+"."+name, info.Since, 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)) {
v := reflect.Indirect(value)
t := v.Type()
// Only works on structs
if t.Kind() != reflect.Struct {
return
}
// Walk over the struct fields and call the given function. If we encounter more complex embedded
// elements (stucts, slices/arrays, maps) we need to descend into those elements as they might
// contain structures nested in the current structure.
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
fieldValue := v.Field(i)
if field.PkgPath != "" {
continue
}
switch field.Type.Kind() {
case reflect.Struct:
walkPluginStruct(fieldValue, fn)
case reflect.Array, reflect.Slice:
for j := 0; j < fieldValue.Len(); j++ {
element := fieldValue.Index(j)
// The array might contain structs
walkPluginStruct(element, fn)
fn(field, element)
}
case reflect.Map:
iter := fieldValue.MapRange()
for iter.Next() {
element := iter.Value()
// The map might contain structs
walkPluginStruct(element, fn)
fn(field, element)
}
}
fn(field, fieldValue)
}
}