-
Notifications
You must be signed in to change notification settings - Fork 486
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Load default values from flagset defaults when unmarshaling to YAML (#…
…122) * load default values from flagset defaults when unmarshaling to YAML Implementing yaml.Unmarshaler by setting the default object (i.e., the "Prometheus way" of doing it) interferes with default values initialized through command line flags. As the Agent uses config objects from both Prometheus and Cortex, these two mechanisms of applying defaults were stepping over each other. The new methodology for applying defaults is as follows: 1. Every config object within pkg/prom should have a DefaultConfig object. 2. When that config object relies on default values from flags, there should be a GetDefaultConfig function within the package that creates a temporary FlagSet for getting default values. The DefaultConfig for the package should be the result of calling this function. 3. Every Config object must implement yaml.Unmarshaler to assign the default object. 4. DefaultConfigs that contain other config structs must also initialize the subconfig structs by using the DefaultConfig from that given package. This commit also changes some other small things: 1. The scraping service Tanka configs can now be given an explicit image set. 2. The shutdown order of the scraping service has been fixed. 3. A logging message has been fixed. * create shared function to get a DefaultConfig object from Cortex-style config structs
- Loading branch information
Showing
7 changed files
with
124 additions
and
7 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package util | ||
|
||
import "flag" | ||
|
||
// DefaultConfigFromFlags will load default values into cfg by | ||
// retrieving default values that are registered as flags. | ||
// | ||
// cfg must implement either PrefixedConfigFlags or ConfigFlags. | ||
func DefaultConfigFromFlags(cfg interface{}) interface{} { | ||
// This function is super ugly but is required for mixing the combination | ||
// of mechanisms for providing default for config structs that are used | ||
// across both Prometheus (via UnmarshalYAML and assigning the default object) | ||
// and Cortex (via RegisterFlags*). | ||
// | ||
// The issue stems from default values assigned via RegisterFlags being set | ||
// at *registration* time, not *flag parse* time. For example, this | ||
// flag: | ||
// | ||
// fs.BoolVar(&enabled, "enabled", true, "enable everything") | ||
// | ||
// Sets enabled to true as soon as fs.BoolVar is called. Normally this is | ||
// fine, but with how Prometheus implements UnmarshalYAML, these defaults | ||
// get overridden: | ||
// | ||
// func (c *Config) UnmarshalYAML(unmarshal func(v interface{}) error) error { | ||
// *c = DefaultConfig // <-- !! overrides defaults from flags !! | ||
// type plain Config | ||
// return unmarshal((*plain)(c)) | ||
// } | ||
// | ||
// The solution to this is to make sure that the DefaultConfig object contains | ||
// the defaults that are set up through registering flags. Unfortunately, the | ||
// best way to do this is this function that creates a temporary flagset just for | ||
// the sake of collecting default values. | ||
// | ||
// This function should be used like so: | ||
// | ||
// var DefaultConfig = *DefaultConfigFromFlags(&Config{}).(*Config) | ||
|
||
fs := flag.NewFlagSet("DefaultConfigFromFlags", flag.PanicOnError) | ||
|
||
if v, ok := cfg.(PrefixedConfigFlags); ok { | ||
v.RegisterFlagsWithPrefix("", fs) | ||
} else if v, ok := cfg.(ConfigFlags); ok { | ||
v.RegisterFlags(fs) | ||
} else { | ||
panic("config does not implement PrefixedConfigFlags or ConfigFlags") | ||
} | ||
|
||
return cfg | ||
} | ||
|
||
// ConfigFlags is an interface that will register flags that can control | ||
// some object. | ||
type ConfigFlags interface { | ||
RegisterFlags(f *flag.FlagSet) | ||
} | ||
|
||
// PrefixedConfigFlags is an interface that, given a prefix for flags | ||
// and a flagset, will register flags that can control some object. | ||
type PrefixedConfigFlags interface { | ||
RegisterFlagsWithPrefix(prefix string, f *flag.FlagSet) | ||
} |
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