Skip to content

Commit

Permalink
Set verbosity after flag definition
Browse files Browse the repository at this point in the history
This seems to be related to a change in the klog library where the "v"
flag isn't defined until after InitFlags() is called. Defer the call of
setting the default verbosity until after we do that instead of during
init(). Fixes startup:

    $ GO_FLAGS=-trimpath build/build.sh && _output/cadvisor -logtostderr
    >> building cadvisor
    go: downloading github.com/prometheus/common v0.38.0
    panic: flag v set at github.com/google/cadvisor/cmd/cadvisor.go:105 before being defined

    goroutine 1 [running]:
    flag.(*FlagSet).Var(0xc00017a150, {0x1874460, 0x2213bc8}, {0x1866490, 0x1}, {0x15f0596, 0x22})
            flag/flag.go:1031 +0x33a
    k8s.io/klog/v2.InitFlags.func1(0xc0001ab2f0?)
            k8s.io/klog/[email protected]/klog.go:439 +0x31
    flag.(*FlagSet).VisitAll(0xc00032fa50?, 0xc0005e1c90)
            flag/flag.go:458 +0x42
    k8s.io/klog/v2.InitFlags(0x7fbe00b2f5b8?)
            k8s.io/klog/[email protected]/klog.go:438 +0x45
    main.main()
            github.com/google/cadvisor/cmd/cadvisor.go:109 +0x36

Signed-off-by: Joe Groocock <[email protected]>
  • Loading branch information
frebib committed Aug 20, 2023
1 parent fbd519b commit 20317bf
Showing 1 changed file with 2 additions and 3 deletions.
5 changes: 2 additions & 3 deletions cmd/cadvisor.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,13 @@ func init() {
optstr := container.AllMetrics.String()
flag.Var(&ignoreMetrics, "disable_metrics", fmt.Sprintf("comma-separated list of `metrics` to be disabled. Options are %s.", optstr))
flag.Var(&enableMetrics, "enable_metrics", fmt.Sprintf("comma-separated list of `metrics` to be enabled. If set, overrides 'disable_metrics'. Options are %s.", optstr))

// Default logging verbosity to V(2)
_ = flag.Set("v", "2")
}

func main() {
klog.InitFlags(nil)
defer klog.Flush()
// Default logging verbosity to V(2)
_ = flag.Set("v", "2")
flag.Parse()

if *versionFlag {
Expand Down

0 comments on commit 20317bf

Please sign in to comment.