-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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: Tool to customize Telegraf builds #11524
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
49f4340
Add MustCompile function for the filter to handle static use-cases.
srebhan 868f903
Export LoadConfigFile() to be used by customizer.
srebhan ca08f52
Add customizer tool.
srebhan 73b2087
Build customizer tool and setup gitignore and linter.
srebhan 45690b0
Tag telegraf as customized if it is and print the number of available…
srebhan 659965d
Fix compatibility for go < 1.19.
srebhan 397bc19
Actually it's better to use a comma-separated list.
srebhan 835d980
Fix make-command environment to include the OS settings.
srebhan e017583
Rename 'customizer' to 'custom_builder'.
srebhan 63f9352
Add notes about dependency includes.
srebhan 32b3cf5
Only run 'make build' to avoid deleting the custom_builder binary.
srebhan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
//go:build !custom | ||
|
||
package internal | ||
|
||
const Customized = "" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
//go:build custom | ||
|
||
package internal | ||
|
||
const Customized = " (customized)" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
# Telegraf customization tool | ||
|
||
Telegraf's `custom_builder` is a tool to select the plugins compiled into the | ||
Telegraf binary. By doing so, Telegraf can become smaller, saving both disk | ||
space and memory if only a sub-set of plugins is selected. | ||
|
||
## Building | ||
|
||
To build `custom_builder` run the following command: | ||
|
||
```shell | ||
# make build_tools | ||
``` | ||
|
||
The resulting binary is located in the `tools/custom_builder` folder. | ||
|
||
## Running | ||
|
||
The easiest way of building a customized Telegraf is to use your | ||
Telegraf configuration file(s). Assuming your configuration is | ||
in `/etc/telegraf/telegraf.conf` you can run | ||
|
||
```shell | ||
# ./tools/custom_builder/custom_builder --config /etc/telegraf/telegraf.conf | ||
``` | ||
|
||
to build a Telegraf binary tailored to your configuration. | ||
You can also specify a configuration directory similar to | ||
Telegraf itself. To additionally use the configurations in | ||
`/etc/telegraf/telegraf.d` run | ||
|
||
```shell | ||
# ./tools/custom_builder/custom_builder \ | ||
--config /etc/telegraf/telegraf.conf \ | ||
--config-dir /etc/telegraf/telegraf.d | ||
``` | ||
|
||
Configurations can also be retrieved from remote locations just | ||
like for Telegraf. | ||
|
||
```shell | ||
# ./tools/custom_builder/custom_builder --config http://myserver/telegraf.conf | ||
``` | ||
|
||
will download the configuration from `myserver`. | ||
|
||
The `--config` and `--config-dir` option can be used multiple times. | ||
In case you want to deploy Telegraf to multiple systems with | ||
different configurations, simply specify the super-set of all | ||
configurations you have. `custom_builder` will figure out the list | ||
for you | ||
|
||
```shell | ||
# ./tools/custom_builder/custom_builder \ | ||
--config system1/telegraf.conf \ | ||
--config system2/telegraf.conf \ | ||
--config ... \ | ||
--config systemN/telegraf.conf \ | ||
--config-dir system1/telegraf.d \ | ||
--config-dir system2/telegraf.d \ | ||
--config-dir ... \ | ||
--config-dir systemN/telegraf.d | ||
``` | ||
|
||
The Telegraf customization uses | ||
[Golang's build-tags](https://pkg.go.dev/go/build#hdr-Build_Constraints) to | ||
select the set of plugins. To see which tags are set use the `--tags` flag. | ||
|
||
To get more help run | ||
|
||
```shell | ||
# ./tools/custom_builder/custom_builder --help | ||
``` | ||
|
||
## Notes | ||
|
||
Please make sure to include all `parsers` you intend to use and check the | ||
enabled-plugins list. | ||
|
||
Additional plugins can potentially be enabled automatically due to | ||
dependencies without being shown in the enabled-plugins list. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"errors" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/influxdata/telegraf/config" | ||
"github.com/influxdata/toml" | ||
"github.com/influxdata/toml/ast" | ||
) | ||
|
||
type pluginState map[string]bool | ||
type selection map[string]pluginState | ||
|
||
func ImportConfigurations(files, dirs []string) (*selection, int, error) { | ||
sel := selection(make(map[string]pluginState)) | ||
|
||
// Initialize the categories | ||
for _, category := range categories { | ||
sel[category] = make(map[string]bool) | ||
} | ||
|
||
// Gather all configuration files | ||
var filenames []string | ||
filenames = append(filenames, files...) | ||
|
||
for _, dir := range dirs { | ||
// Walk the directory and get the packages | ||
elements, err := os.ReadDir(dir) | ||
if err != nil { | ||
return nil, 0, fmt.Errorf("reading directory %q failed: %w", dir, err) | ||
} | ||
|
||
for _, element := range elements { | ||
if element.IsDir() || filepath.Ext(element.Name()) != ".conf" { | ||
continue | ||
} | ||
|
||
filenames = append(filenames, filepath.Join(dir, element.Name())) | ||
} | ||
} | ||
if len(filenames) == 0 { | ||
return &sel, 0, errors.New("no configuration files given or found") | ||
} | ||
|
||
// Do the actual import | ||
err := sel.importFiles(filenames) | ||
return &sel, len(filenames), err | ||
} | ||
|
||
func (s *selection) Filter(p packageCollection) (*packageCollection, error) { | ||
enabled := packageCollection{ | ||
packages: map[string][]packageInfo{}, | ||
} | ||
|
||
for category, pkgs := range p.packages { | ||
var categoryEnabledPackages []packageInfo | ||
settings := (*s)[category] | ||
for _, pkg := range pkgs { | ||
if _, found := settings[pkg.Plugin]; found { | ||
categoryEnabledPackages = append(categoryEnabledPackages, pkg) | ||
} | ||
} | ||
enabled.packages[category] = categoryEnabledPackages | ||
} | ||
|
||
// Make sure we update the list of default parsers used by | ||
// the remaining packages | ||
enabled.FillDefaultParsers() | ||
|
||
// If the user did not configure any parser, we want to include | ||
// the default parsers if any to preserve a functional set of | ||
// plugins. | ||
if len(enabled.packages["parsers"]) == 0 && len(enabled.defaultParsers) > 0 { | ||
var parsers []packageInfo | ||
for _, pkg := range p.packages["parsers"] { | ||
for _, name := range enabled.defaultParsers { | ||
if pkg.Plugin == name { | ||
parsers = append(parsers, pkg) | ||
break | ||
} | ||
} | ||
} | ||
enabled.packages["parsers"] = parsers | ||
} | ||
|
||
return &enabled, nil | ||
} | ||
|
||
func (s *selection) importFiles(configurations []string) error { | ||
for _, cfg := range configurations { | ||
buf, err := config.LoadConfigFile(cfg) | ||
if err != nil { | ||
return fmt.Errorf("reading %q failed: %v", cfg, err) | ||
} | ||
|
||
if err := s.extractPluginsFromConfig(buf); err != nil { | ||
return fmt.Errorf("extracting plugins from %q failed: %v", cfg, err) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (s *selection) extractPluginsFromConfig(buf []byte) error { | ||
table, err := toml.Parse(trimBOM(buf)) | ||
if err != nil { | ||
return fmt.Errorf("parsing TOML failed: %w", err) | ||
} | ||
|
||
for category, subtbl := range table.Fields { | ||
categoryTbl, ok := subtbl.(*ast.Table) | ||
if !ok { | ||
continue | ||
} | ||
|
||
if _, found := (*s)[category]; !found { | ||
continue | ||
} | ||
|
||
for name, data := range categoryTbl.Fields { | ||
(*s)[category][name] = true | ||
|
||
// We need to check the data_format field to get all required parsers | ||
switch category { | ||
case "inputs", "processors": | ||
pluginTables, ok := data.([]*ast.Table) | ||
if !ok { | ||
continue | ||
} | ||
for _, subsubtbl := range pluginTables { | ||
for field, fieldData := range subsubtbl.Fields { | ||
if field != "data_format" { | ||
continue | ||
} | ||
kv := fieldData.(*ast.KeyValue) | ||
name := kv.Value.(*ast.String) | ||
(*s)["parsers"][name.Value] = true | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func trimBOM(f []byte) []byte { | ||
return bytes.TrimPrefix(f, []byte("\xef\xbb\xbf")) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I love this additional output, thank you!
I built with a config of just
inputs.diskio
andoutputs.file
and I ended up seeing:Which are:
Two questions:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems
wavefront
is coming through the backyard. ;-) The route isfile
output importsgithub.com/influxdata/telegraf/plugins/serializers
serializers/registry.go
importsgithub.com/influxdata/telegraf/plugins/serializers/wavefront
wavefront
serializer importsgithub.com/influxdata/telegraf/plugins/outputs/wavefront
😨This happens with the epic comment:
🤦♂️
I guess this has to be fixed, but is not an issue of
custom_builder
... What do you think?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Absolutely. Will do.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lol @ that comment, let's circle around and fix that after this is merged. I can file an issue.