Skip to content

Commit

Permalink
fix the fatal errors when reading config file
Browse files Browse the repository at this point in the history
  • Loading branch information
mxiamxia committed Apr 17, 2021
1 parent 989b35a commit c0ab7ed
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
8 changes: 1 addition & 7 deletions cmd/awscollector/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,6 @@ func main() {

// init cfgFactory
cfgFactory := config.GetParserProvider()
// TODO(pingleig): a hack to keep consistent behaviour with previous code
// so we exit 0 when running on vm for config error.
// https://github.com/aws-observability/aws-otel-collector/issues/340
if err := config.TryFileConfig(); err != nil {
os.Exit(0)
}

// init lumberFunc for zap logger
lumberHook := logger.GetLumberHook()
Expand All @@ -74,7 +68,7 @@ func main() {
ApplicationStartInfo: info,
ParserProvider: cfgFactory,
LoggingOptions: []zap.Option{zap.Hooks(lumberHook)}}); err != nil {
log.Fatal(err)
logFatal(err)
}

}
Expand Down
4 changes: 4 additions & 0 deletions cmd/awscollector/main_others.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,7 @@ func run(params service.Parameters) error {
}
return runInteractive(params)
}

func logFatal(err error) {
log.Fatal(err)
}
15 changes: 15 additions & 0 deletions cmd/awscollector/main_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
package main

import (
"log"
"os"
"strings"

"github.com/pkg/errors"
"golang.org/x/sys/windows/svc"

Expand All @@ -40,8 +44,19 @@ func run(params service.Parameters) error {
func runService(params service.Parameters) error {
// do not need to supply service name when startup is invoked through Service Control Manager directly
if err := svc.Run("", service.NewWindowsService(params)); err != nil {
if strings.Contains(err.Error(), "cannot load configuration's parser:") {
return errors.New(err.Error())
}
return errors.Wrap(err, "failed to start service")
}

return nil
}

// Has to set exit code 0 for the fatal errors when run the collector as service in Windows
// to prevent infinite reboot by Windows service
// https://github.com/aws-observability/aws-otel-collector/issues/340
func logFatal(err error) {
log.Println(err.Error())
os.Exit(0)
}

0 comments on commit c0ab7ed

Please sign in to comment.