Skip to content

Commit

Permalink
fix: nil dereference on wrong log level values, cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
burningalchemist committed Dec 29, 2023
1 parent adb6364 commit 8da6b12
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
23 changes: 12 additions & 11 deletions cmd/sql_exporter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,20 @@ func main() {
}
flag.Parse()

// Show version and exit.
if *showVersion {
fmt.Println(version.Print(appName))
os.Exit(0)
}

// Setup logging.
promlogConfig := &promlog.Config{}
promlogConfig.Level = &promlog.AllowedLevel{}
_ = promlogConfig.Level.Set(*logLevel)
err := promlogConfig.Level.Set(*logLevel)
if err != nil {
fmt.Printf("Error initializing exporter: %s\n", err)
os.Exit(1)
}
if *logFormatJSON {
promlogConfig.Format = &promlog.AllowedFormat{}
_ = promlogConfig.Format.Set("json")
Expand All @@ -66,21 +77,11 @@ func main() {
klog.SetLogger(logger)
klog.ClampLevel(debugMaxLevel)

// Override --alsologtostderr default value.
if alsoLogToStderr := flag.Lookup("alsologtostderr"); alsoLogToStderr != nil {
alsoLogToStderr.DefValue = "true"
_ = alsoLogToStderr.Value.Set("true")
}
// Override the config.file default with the SQLEXPORTER_CONFIG environment variable if set.
if val, ok := os.LookupEnv(envConfigFile); ok {
*configFile = val
}

if *showVersion {
fmt.Println(version.Print(appName))
os.Exit(0)
}

klog.Warningf("Starting SQL exporter %s %s", version.Info(), version.BuildContext())

exporter, err := sql_exporter.NewExporter(*configFile)
Expand Down
8 changes: 4 additions & 4 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import (

func TestResolveCollectorRefs(t *testing.T) {
colls := map[string]*CollectorConfig{
"a": &CollectorConfig{Name: "a"},
"b": &CollectorConfig{Name: "b"},
"c": &CollectorConfig{Name: "b"},
"aa": &CollectorConfig{Name: "aa"},
"a": {Name: "a"},
"b": {Name: "b"},
"c": {Name: "b"},
"aa": {Name: "aa"},
}

t.Run("NoGlobbing", func(t *testing.T) {
Expand Down

0 comments on commit 8da6b12

Please sign in to comment.