-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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
Loki: Common Config #4347
Merged
Merged
Loki: Common Config #4347
Changes from 1 commit
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
cb9c670
refactor the pkg/util/cfg package to simplify and make it accessable …
slim-bean b16004b
refactor how dynamic/common config is loaded
trevorwhitney 4aaf99c
Add common config logic for path prefix
trevorwhitney 81a8b1b
Missed a file, update common config with prefix property
trevorwhitney c0afa15
fix test, remove loki from args
trevorwhitney c4c9b2c
Clean up test names
trevorwhitney 780efaf
fix typo in filename, make linter happy
trevorwhitney 317efba
Update pkg/loki/config_wrapper.go
trevorwhitney 3404c95
Update pkg/loki/config_wrapper.go
trevorwhitney d993c18
Update pkg/loki/config_wrapper_test.go
trevorwhitney 1140046
remove temp files in test
trevorwhitney 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package cfg | ||
|
||
import ( | ||
"flag" | ||
"os" | ||
|
||
"github.com/grafana/dskit/flagext" | ||
"github.com/pkg/errors" | ||
|
||
"github.com/grafana/loki/pkg/loki" | ||
"github.com/grafana/loki/pkg/util/cfg" | ||
) | ||
|
||
// ConfigWrapper is a struct containing the Loki config along with other values that can be set on the command line | ||
// for interacting with the config file or the application directly. | ||
type ConfigWrapper struct { | ||
loki.Config `yaml:",inline"` | ||
PrintVersion bool | ||
VerifyConfig bool | ||
PrintConfig bool | ||
LogConfig bool | ||
ConfigFile string | ||
ConfigExpandEnv bool | ||
} | ||
|
||
func (c *ConfigWrapper) RegisterFlags(f *flag.FlagSet) { | ||
f.BoolVar(&c.PrintVersion, "version", false, "Print this builds version information") | ||
f.BoolVar(&c.VerifyConfig, "verify-config", false, "Verify config file and exits") | ||
f.BoolVar(&c.PrintConfig, "print-config-stderr", false, "Dump the entire Loki config object to stderr") | ||
f.BoolVar(&c.LogConfig, "log-config-reverse-order", false, "Dump the entire Loki config object at Info log "+ | ||
"level with the order reversed, reversing the order makes viewing the entries easier in Grafana.") | ||
f.StringVar(&c.ConfigFile, "config.file", "", "yaml file to load") | ||
f.BoolVar(&c.ConfigExpandEnv, "config.expand-env", false, "Expands ${var} in config according to the values of the environment variables.") | ||
c.Config.RegisterFlags(f) | ||
} | ||
|
||
// Clone takes advantage of pass-by-value semantics to return a distinct *Config. | ||
// This is primarily used to parse a different flag set without mutating the original *Config. | ||
func (c *ConfigWrapper) Clone() flagext.Registerer { | ||
return func(c ConfigWrapper) *ConfigWrapper { | ||
return &c | ||
}(*c) | ||
} | ||
|
||
// Unmarshal handles populating Loki's config based on the following precedence: | ||
// 1. Defaults provided by the `RegisterFlags` interface | ||
// 2. Sections populated by the `common` config section of the Loki config | ||
// 3. Any config options specified directly in the loki config file | ||
// 4. Any config options specified on the command line. | ||
func Unmarshal(dst cfg.Cloneable) error { | ||
return cfg.Unmarshal(dst, | ||
// First populate the config with defaults including flags from the command line | ||
cfg.Defaults(flag.CommandLine), | ||
// Next populate the config from the config file, we do this to populate the `common` | ||
// section of the config file by taking advantage of the code in YAMLFlag which will load | ||
// and process the config file. | ||
cfg.YAMLFlag(os.Args[1:], "config.file"), | ||
// Apply our logic to use values from the common section to set values throughout the Loki config. | ||
CommonConfig(), | ||
// Load configs from the config file a second time, this will supersede anything set by the common | ||
// config with values specified in the config file. | ||
cfg.YAMLFlag(os.Args[1:], "config.file"), | ||
// Load the flags again, this will supersede anything set from config file with flags from the command line. | ||
cfg.Flags(), | ||
) | ||
} | ||
|
||
// CommonConfig applies all rules for setting Loki config values from the common section of the Loki config file. | ||
// This entire method's purpose is to simplify Loki's config in an opinionated way so that Loki can be run | ||
// with the minimal amount of config options for most use cases. It also aims to reduce redundancy where | ||
// some values are set multiple times through the Loki config. | ||
func CommonConfig() cfg.Source { | ||
return func(dst cfg.Cloneable) error { | ||
r, ok := dst.(*ConfigWrapper) | ||
if !ok { | ||
return errors.New("dst is not a Loki ConfigWrapper") | ||
} | ||
|
||
// Apply all our custom logic here to set values in the Loki config from values in the common config | ||
// FIXME this is just an example showing how we can use values from the common section to set values on the Loki config object | ||
r.StorageConfig.BoltDBShipperConfig.SharedStoreType = r.Common.Store | ||
|
||
return nil | ||
} | ||
} |
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 @@ | ||
package common | ||
|
||
type Config struct { | ||
Store string // This is just an example, but here we should define all the 'common' config values used to set other Loki values. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here we can define the |
||
} |
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
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.
Here we can do anything needed to map
common
configs to the Loki configs