Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: generate the plugins sample config #10886

Merged
merged 18 commits into from
Apr 5, 2022
Merged

feat: generate the plugins sample config #10886

merged 18 commits into from
Apr 5, 2022

Conversation

sspaink
Copy link
Contributor

@sspaink sspaink commented Mar 24, 2022

resolve: #9733

Description

Adding a tool called generate_plugindata that reads a plugins README.md and extracts the sample config, then using the source file as a template inject the sample configuration into it. The purpose is to avoid having to maintain the sample config in two different spots, in the README.md and in the code. This will lead to removing 11,000 lines of duplicate sample configuration from our source code that we no longer have to maintain!

Each plugin would have a header like this:

//go:generate go run ../../../tools/generate_plugindata/main.go
//go:generate go run ../../../tools/generate_plugindata/main.go --clean

Then the sample config function would be updated like this:

func (p *Plugin) SampleConfig() string {
	return `{{ .SampleConfig }}`
}

and if you run make you would see this output:

$ make
go mod download -x
go generate -run="plugindata/main.go$" ./plugins/inputs/... ./plugins/outputs/... ./plugins/processors/... ./plugins/aggregators/...
go build -ldflags " -X main.commit=7d8fc6f0 -X main.branch=generatesampleconfig -X main.goos=linux -X main.goarch=amd64 -X main.version=1.23.0-7d8fc6f0" ./cmd/telegraf
go generate -run="plugindata/main.go --clean" ./plugins/inputs/... ./plugins/outputs/... ./plugins/processors/... ./plugins/aggregators/...

Plugin migration

All the plugins have been migrated to this new format in separate pull requests. Separate pull requests were made to keep this pull request smaller and easier to review originally this was 396 files changed with all plugins migrated. I wrote a simple local tool to help automate the migration of the plugins. Unfortunately, there are minor differences between the README.md and source code configs that will have to be manually reviewed and fixed to determine the correct one.

Testing

Resulting telegraf.conf: https://gist.github.com/sspaink/554f4843e87afbaa39bb436625a39b4d

You can generate your own local config by running the following commands:

  1. git checkout -b testbranch
  2. git merge origin/generatesampleconfig && git merge origin/updateinputconifg_a-l --no-edit && git merge origin/updateinputconifg_m-z --no-edit && git merge origin/updateaggregatorconfig --no-edit && git merge origin/updateprocessorconfig --no-edit && git merge origin/updateoutputconfig -s recursive -X theirs --no-edit
  3. make
  4. ./telegraf config > telegraf.conf
  5. git reset --hard origin/generatesampleconfig

Copy link
Contributor

@reimda reimda left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love this idea

scripts/generate_plugindata/main.go Outdated Show resolved Hide resolved
plugins/inputs/jti_openconfig_telemetry/gen.go Outdated Show resolved Hide resolved
Copy link
Contributor

@powersj powersj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is awesome! A few questions before this afternoon

docs/AGGREGATORS.md Show resolved Hide resolved
config/config_test.go Show resolved Hide resolved
tools/generate_plugindata/main.go Show resolved Hide resolved
Makefile Outdated Show resolved Hide resolved
tools/generate_plugindata/main_test.go Show resolved Hide resolved
tools/generate_plugindata/main_test.go Show resolved Hide resolved
@sspaink sspaink self-assigned this Mar 30, 2022
@sspaink
Copy link
Contributor Author

sspaink commented Mar 30, 2022

TODO:

Sebastian Spaink added 3 commits April 4, 2022 11:28
Not sure why it is failing now, while it worked before
docs/OUTPUTS.md Outdated Show resolved Hide resolved
plugin.go Show resolved Hide resolved
@telegraf-tiger
Copy link
Contributor

telegraf-tiger bot commented Apr 5, 2022

@sspaink sspaink merged commit 5d6748f into master Apr 5, 2022
@sspaink sspaink deleted the generatesampleconfig branch April 5, 2022 22:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ready for final review This pull request has been reviewed and/or tested by multiple users and is ready for a final review.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Embed plugin README.md into Telegraf binary
3 participants