Skip to content

Commit

Permalink
RSDK-9326 - log important viam env variables (#4602)
Browse files Browse the repository at this point in the history
Co-authored-by: Cheuk <90270663+cheukt@users.noreply.github.com>
  • Loading branch information
Kschappacher and cheukt authored Dec 5, 2024
1 parent 6345786 commit 2117ac3
Showing 1 changed file with 33 additions and 9 deletions.
42 changes: 33 additions & 9 deletions web/server/entrypoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,25 @@ type robotServer struct {
registry *logging.Registry
}

func logViamEnvVariables(logger logging.Logger) {
var viamEnvVariables []interface{}
if value, exists := os.LookupEnv("VIAM_MODULE_ROOT"); exists {
viamEnvVariables = append(viamEnvVariables, "VIAM_MODULE_ROOT", value)
}
if value, exists := os.LookupEnv("VIAM_RESOURCE_CONFIGURATION_TIMEOUT"); exists {
viamEnvVariables = append(viamEnvVariables, "VIAM_RESOURCE_CONFIGURATION_TIMEOUT", value)
}
if value, exists := os.LookupEnv("VIAM_MODULE_STARTUP_TIMEOUT"); exists {
viamEnvVariables = append(viamEnvVariables, "VIAM_MODULE_STARTUP_TIMEOUT", value)
}
if rutils.PlatformHomeDir() != "" {
viamEnvVariables = append(viamEnvVariables, "HOME", rutils.PlatformHomeDir())
}
if len(viamEnvVariables) != 0 {
logger.Infow("Starting viam-server with following environment variables", viamEnvVariables...)
}
}

func logVersion(logger logging.Logger) {
var versionFields []interface{}
if config.Version != "" {
Expand All @@ -72,6 +91,11 @@ func logVersion(logger logging.Logger) {
}
}

func logStartupInfo(logger logging.Logger) {
logVersion(logger)
logViamEnvVariables(logger)
}

// RunServer is an entry point to starting the web server that can be called by main in a code
// sample or otherwise be used to initialize the server.
func RunServer(ctx context.Context, args []string, _ logging.Logger) (err error) {
Expand All @@ -94,17 +118,17 @@ func RunServer(ctx context.Context, args []string, _ logging.Logger) (err error)
config.InitLoggingSettings(logger, argsParsed.Debug)

if argsParsed.Version {
// log version here and return if version flag.
logVersion(logger)
// log startup info here and return if version flag.
logStartupInfo(logger)
return
}

// log version locally if server fails and exits while attempting to start up
var versionLogged bool
// log startup info locally if server fails and exits while attempting to start up
var startupInfoLogged bool
defer func() {
if !versionLogged {
if !startupInfoLogged {
logger.CInfo(ctx, "error starting viam-server, logging version and exiting")
logVersion(logger)
logStartupInfo(logger)
}
}()

Expand Down Expand Up @@ -160,9 +184,9 @@ func RunServer(ctx context.Context, args []string, _ logging.Logger) (err error)

registry.AddAppenderToAll(netAppender)
}
// log version after netlogger is initialized so it's captured in cloud machine logs.
logVersion(logger)
versionLogged = true
// log startup info after netlogger is initialized so it's captured in cloud machine logs.
logStartupInfo(logger)
startupInfoLogged = true

server := robotServer{
logger: logger,
Expand Down

0 comments on commit 2117ac3

Please sign in to comment.