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

Refactor collectors to use config structs #2812

Open
wants to merge 42 commits into
base: master
Choose a base branch
from

Conversation

marctc
Copy link

@marctc marctc commented Sep 27, 2023

(This is a continuation of the initial work done here.)

The PR refactors node exporter to decouple collectors' config from kingpin flags. This allows that
the exporter can be embedded in other libraries and run multiple instances of it by removing the global state
that avoids that. Example here.

The definition of config params has been moved to structs in a central config struct (NodeCollectorConfig in collector/config.go) that gathers all config params for all the collectors. The registration of kingpin has been move to flags.go where all the flags are parsed to populate the main collector config.

Some of the logic to support deprecated old flags (i.e UnitIncludeSet) has been moved from global vars to part of the collectors
config struct. Extra refactors in collector.go were needed to remove the global state.

Addionally, we could refactor (in a separated PR):

  • Extract the defaults from flags.go and move them to be exportable.
  • Change error messages like this to something generic.
  • Allow node exporter to be configured with YAML file?

Related: grafana/alloy#548

SuperQ and others added 9 commits July 21, 2023 16:08
Add a new pattern to allow moving the flag handling out of the collector
package and into a separate package. This allows collector package users
to create their own config or flag handling.

Signed-off-by: Ben Kochie <superq@gmail.com>
}
err = c.compileIncludeFlags(flagsInclude, bugsInclude)
err = c.compileIncludeFlags(c.config.CPU.FlagsInclude, c.config.CPU.BugsInclude)
if err != nil {
return nil, fmt.Errorf("fail to compile --collector.cpu.info.flags-include and --collector.cpu.info.bugs-include, the values of them must be regular expressions: %w", err)

Choose a reason for hiding this comment

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

Global statement about error messages referencing flags, might not need to change this for this initial push but since these can come from any source we should change to a more generic name.

Copy link
Author

Choose a reason for hiding this comment

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

Yeah, i couldn't come with an elegant solution for now. We can change it later 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants