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

Set glog flags from configuration #3062

Merged
merged 3 commits into from
Feb 26, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 28 additions & 7 deletions dgraph/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package cmd

import (
goflag "flag"
"flag"
"strings"

"github.com/dgraph-io/dgraph/dgraph/cmd/alpha"
Expand All @@ -31,7 +31,6 @@ import (
"github.com/dgraph-io/dgraph/dgraph/cmd/zero"
"github.com/dgraph-io/dgraph/x"
"github.com/spf13/cobra"
flag "github.com/spf13/pflag"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Aliasing pflag => flag and flag => goflag created unnecessary ambiguity. I've changed this to remove the ambiguity.

"github.com/spf13/viper"
)

Expand All @@ -55,9 +54,9 @@ cluster.
func Execute() {
initCmds()

// Convinces goflags that Parse() has been called to avoid noisy logs.
// Convinces glog that Parse() has been called to avoid noisy logs.
// https://github.com/kubernetes/kubernetes/issues/17162#issuecomment-225596212
x.Check(goflag.CommandLine.Parse([]string{}))
x.Check(flag.CommandLine.Parse([]string{}))

// Dumping the usage in case of an error makes the error messages harder to see.
RootCmd.SilenceUsage = true
Expand Down Expand Up @@ -87,10 +86,12 @@ func initCmds() {
"Allow trace endpoint to be accessible from remote")
rootConf.BindPFlags(RootCmd.PersistentFlags())

flag.CommandLine.AddGoFlagSet(goflag.CommandLine)
// Add all existing global flag (eg: from glog) to rootCmd's flags
RootCmd.PersistentFlags().AddGoFlagSet(flag.CommandLine)

// Always set stderrthreshold=0. Don't let users set it themselves.
x.Check(flag.Set("stderrthreshold", "0"))
x.Check(flag.CommandLine.MarkDeprecated("stderrthreshold",
x.Check(RootCmd.PersistentFlags().Set("stderrthreshold", "0"))
x.Check(RootCmd.PersistentFlags().MarkDeprecated("stderrthreshold",
"Dgraph always sets this flag to 0. It can't be overwritten."))

for _, sc := range subcommands {
Expand All @@ -112,6 +113,26 @@ func initCmds() {
for _, sc := range subcommands {
sc.Conf.SetConfigFile(cfg)
x.Check(x.Wrapf(sc.Conf.ReadInConfig(), "reading config"))
setGlogFlags(sc.Conf)
}
})
}

// setGlogFlags function sets the glog flags based on the configuration.
// We need to manually set the flags from configuration because glog reads
// values from flags, not configuration.
func setGlogFlags(conf *viper.Viper) {
// List of flags taken from
// https://github.com/golang/glog/blob/master/glog.go#L399
// and https://github.com/golang/glog/blob/master/glog_file.go#L41
glogFlags := [...]string{
"log_dir", "logtostderr", "alsologtostderr", "v",
"stderrthreshold", "vmodule", "log_backtrace_at",
}
for _, gflag := range glogFlags {
// Set value of flag to the value in config
if stringValue, ok := conf.Get(gflag).(string); ok {
x.Check(flag.Lookup(gflag).Value.Set(stringValue))
}
}
}