Skip to content

Commit

Permalink
feat: Implement deprecation infrastructure (#10200)
Browse files Browse the repository at this point in the history
  • Loading branch information
reimda authored Dec 1, 2021
1 parent b4ef429 commit df6bf48
Show file tree
Hide file tree
Showing 16 changed files with 584 additions and 29 deletions.
5 changes: 4 additions & 1 deletion .markdownlint.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"MD013": false
"MD013": false,
"MD033": {
"allowed_elements": ["br"]
}
}
27 changes: 14 additions & 13 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ HOSTGO := env -u GOOS -u GOARCH -u GOARM -- go
LDFLAGS := $(LDFLAGS) -X main.commit=$(commit) -X main.branch=$(branch) -X main.goos=$(GOOS) -X main.goarch=$(GOARCH)
ifneq ($(tag),)
LDFLAGS += -X main.version=$(version)
else
LDFLAGS += -X main.version=$(version)-$(commit)
endif

# Go built-in race detector works only for 64 bits architectures.
Expand Down Expand Up @@ -148,19 +150,18 @@ lint-install:

.PHONY: lint
lint:
@which golangci-lint >/dev/null 2>&1 || { \
echo "golangci-lint not found, please run: make lint-install"; \
exit 1; \
}

golangci-lint run

@which markdownlint >/dev/null 2>&1 || { \
echo "markdownlint not found, please run: make lint-install"; \
exit 1; \
}

markdownlint .
ifeq (, $(shell which golangci-lint))
$(info golangci-lint can't be found, please run: make lint-install)
exit 1
endif

golangci-lint run

ifeq (, $(shell which markdownlint-cli))
$(info markdownlint-cli can't be found, please run: make lint-install)
exit 1
endif
markdownlint-cli

.PHONY: lint-branch
lint-branch:
Expand Down
51 changes: 41 additions & 10 deletions cmd/telegraf/telegraf.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ var fVersion = flag.Bool("version", false, "display the version and exit")
var fSampleConfig = flag.Bool("sample-config", false,
"print out full sample configuration")
var fPidfile = flag.String("pidfile", "", "file to write our pid to")
var fDeprecationList = flag.Bool("deprecation-list", false,
"print all deprecated plugins or plugin options.")
var fSectionFilters = flag.String("section-filter", "",
"filter the sections to print, separator is ':'. Valid values are 'agent', 'global_tags', 'outputs', 'processors', 'aggregators' and 'inputs'")
var fInputFilters = flag.String("input-filter", "",
Expand Down Expand Up @@ -270,6 +272,19 @@ func runAgent(ctx context.Context,
log.Printf("I! Loaded outputs: %s", strings.Join(c.OutputNames(), " "))
log.Printf("I! Tags enabled: %s", c.ListTags())

if count, found := c.Deprecations["inputs"]; found && (count[0] > 0 || count[1] > 0) {
log.Printf("W! Deprecated inputs: %d and %d options", count[0], count[1])
}
if count, found := c.Deprecations["aggregators"]; found && (count[0] > 0 || count[1] > 0) {
log.Printf("W! Deprecated aggregators: %d and %d options", count[0], count[1])
}
if count, found := c.Deprecations["processors"]; found && (count[0] > 0 || count[1] > 0) {
log.Printf("W! Deprecated processors: %d and %d options", count[0], count[1])
}
if count, found := c.Deprecations["outputs"]; found && (count[0] > 0 || count[1] > 0) {
log.Printf("W! Deprecated outputs: %d and %d options", count[0], count[1])
}

if *fPidfile != "" {
f, err := os.OpenFile(*fPidfile, os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
Expand Down Expand Up @@ -348,6 +363,11 @@ func main() {

logger.SetupLogging(logger.LogConfig{})

// Configure version
if err := internal.SetVersion(version); err != nil {
log.Println("Telegraf version already configured to: " + internal.Version())
}

// Load external plugins, if requested.
if *fPlugins != "" {
log.Printf("I! Loading external plugins from: %s", *fPlugins)
Expand Down Expand Up @@ -392,6 +412,27 @@ func main() {

// switch for flags which just do something and exit immediately
switch {
case *fDeprecationList:
c := config.NewConfig()
infos := c.CollectDeprecationInfos(
inputFilters,
outputFilters,
aggregatorFilters,
processorFilters,
)
//nolint:revive // We will notice if Println fails
fmt.Println("Deprecated Input Plugins: ")
c.PrintDeprecationList(infos["inputs"])
//nolint:revive // We will notice if Println fails
fmt.Println("Deprecated Output Plugins: ")
c.PrintDeprecationList(infos["outputs"])
//nolint:revive // We will notice if Println fails
fmt.Println("Deprecated Processor Plugins: ")
c.PrintDeprecationList(infos["processors"])
//nolint:revive // We will notice if Println fails
fmt.Println("Deprecated Aggregator Plugins: ")
c.PrintDeprecationList(infos["aggregators"])
return
case *fOutputList:
fmt.Println("Available Output Plugins: ")
names := make([]string, 0, len(outputs.Outputs))
Expand Down Expand Up @@ -435,16 +476,6 @@ func main() {
return
}

shortVersion := version
if shortVersion == "" {
shortVersion = "unknown"
}

// Configure version
if err := internal.SetVersion(shortVersion); err != nil {
log.Println("Telegraf version already configured to: " + internal.Version())
}

run(
inputFilters,
outputFilters,
Expand Down
54 changes: 54 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import (
"strings"
"time"

"github.com/coreos/go-semver/semver"

"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/internal"
"github.com/influxdata/telegraf/internal/choice"
Expand Down Expand Up @@ -77,6 +79,9 @@ type Config struct {
// Processors have a slice wrapper type because they need to be sorted
Processors models.RunningProcessors
AggProcessors models.RunningProcessors

Deprecations map[string][]int64
version *semver.Version
}

// NewConfig creates a new struct to hold the Telegraf config.
Expand All @@ -102,7 +107,15 @@ func NewConfig() *Config {
AggProcessors: make([]*models.RunningProcessor, 0),
InputFilters: make([]string, 0),
OutputFilters: make([]string, 0),
Deprecations: make(map[string][]int64),
}

// Handle unknown version
version := internal.Version()
if version == "" || version == "unknown" {
version = "0.0.0-unknown"
}
c.version = semver.New(version)

tomlCfg := &toml.Config{
NormFieldName: toml.DefaultConfig.NormFieldName,
Expand Down Expand Up @@ -1009,6 +1022,11 @@ func parseConfig(contents []byte) (*ast.Table, error) {
func (c *Config) addAggregator(name string, table *ast.Table) error {
creator, ok := aggregators.Aggregators[name]
if !ok {
// Handle removed, deprecated plugins
if di, deprecated := aggregators.Deprecations[name]; deprecated {
printHistoricPluginDeprecationNotice("aggregators", name, di)
return fmt.Errorf("plugin deprecated")
}
return fmt.Errorf("Undefined but requested aggregator: %s", name)
}
aggregator := creator()
Expand All @@ -1022,13 +1040,22 @@ func (c *Config) addAggregator(name string, table *ast.Table) error {
return err
}

if err := c.printUserDeprecation("aggregators", name, aggregator); err != nil {
return err
}

c.Aggregators = append(c.Aggregators, models.NewRunningAggregator(aggregator, conf))
return nil
}

func (c *Config) addProcessor(name string, table *ast.Table) error {
creator, ok := processors.Processors[name]
if !ok {
// Handle removed, deprecated plugins
if di, deprecated := processors.Deprecations[name]; deprecated {
printHistoricPluginDeprecationNotice("processors", name, di)
return fmt.Errorf("plugin deprecated")
}
return fmt.Errorf("Undefined but requested processor: %s", name)
}

Expand Down Expand Up @@ -1070,6 +1097,10 @@ func (c *Config) newRunningProcessor(
}
}

if err := c.printUserDeprecation("processors", processorConfig.Name, processor); err != nil {
return nil, err
}

rf := models.NewRunningProcessor(processor, processorConfig)
return rf, nil
}
Expand All @@ -1080,6 +1111,11 @@ func (c *Config) addOutput(name string, table *ast.Table) error {
}
creator, ok := outputs.Outputs[name]
if !ok {
// Handle removed, deprecated plugins
if di, deprecated := outputs.Deprecations[name]; deprecated {
printHistoricPluginDeprecationNotice("outputs", name, di)
return fmt.Errorf("plugin deprecated")
}
return fmt.Errorf("Undefined but requested output: %s", name)
}
output := creator()
Expand All @@ -1104,6 +1140,10 @@ func (c *Config) addOutput(name string, table *ast.Table) error {
return err
}

if err := c.printUserDeprecation("outputs", name, output); err != nil {
return err
}

ro := models.NewRunningOutput(output, outputConfig, c.Agent.MetricBatchSize, c.Agent.MetricBufferLimit)
c.Outputs = append(c.Outputs, ro)
return nil
Expand All @@ -1113,13 +1153,23 @@ func (c *Config) addInput(name string, table *ast.Table) error {
if len(c.InputFilters) > 0 && !sliceContains(name, c.InputFilters) {
return nil
}

// Legacy support renaming io input to diskio
if name == "io" {
if err := c.printUserDeprecation("inputs", name, nil); err != nil {
return err
}
name = "diskio"
}

creator, ok := inputs.Inputs[name]
if !ok {
// Handle removed, deprecated plugins
if di, deprecated := inputs.Deprecations[name]; deprecated {
printHistoricPluginDeprecationNotice("inputs", name, di)
return fmt.Errorf("plugin deprecated")
}

return fmt.Errorf("Undefined but requested input: %s", name)
}
input := creator()
Expand Down Expand Up @@ -1153,6 +1203,10 @@ func (c *Config) addInput(name string, table *ast.Table) error {
return err
}

if err := c.printUserDeprecation("inputs", name, input); err != nil {
return err
}

rp := models.NewRunningInput(input, pluginConfig)
rp.SetDefaultTags(c.Tags)
c.Inputs = append(c.Inputs, rp)
Expand Down
Loading

0 comments on commit df6bf48

Please sign in to comment.