Skip to content

Commit

Permalink
APP-6990 consume debug level from cloud config (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
abe-winter authored Dec 4, 2024
1 parent 45e1c0e commit ed72f69
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
2 changes: 1 addition & 1 deletion cmd/viam-agent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func main() {
}

if opts.Debug {
globalLogger = logging.NewDebugLogger("viam-agent")
globalLogger.SetLevel(logging.DEBUG)
}

// need to be root to go any further than this
Expand Down
27 changes: 25 additions & 2 deletions manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ func (m *Manager) LoadSubsystems(ctx context.Context) error {
if err != nil {
m.logger.Error(errw.Wrap(err, "getting cached config"))
}
m.processConfig(cachedConfig)

for _, name := range registry.List() {
cfg, ok := cachedConfig[name]
Expand Down Expand Up @@ -344,8 +345,8 @@ func (m *Manager) getCachedConfig() (map[string]*pb.DeviceSubsystemConfig, error
cachedConfig := map[string]*pb.DeviceSubsystemConfig{SubsystemName: {}}

cacheFilePath := filepath.Join(ViamDirs["cache"], agentCachePath)
//nolint:gosec
cacheBytes, err := os.ReadFile(cacheFilePath)

cacheBytes, err := os.ReadFile(cacheFilePath) //nolint:gosec
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
return cachedConfig, nil
Expand Down Expand Up @@ -422,6 +423,26 @@ func (m *Manager) dial(ctx context.Context) error {
return nil
}

// process non-subsystem effects of a config (i.e. agent-specific stuff that needs to happen when loading cache and when updating).
func (m *Manager) processConfig(cfg map[string]*pb.DeviceSubsystemConfig) {
if agent, ok := cfg["viam-agent"]; ok {
if debugRaw, ok := agent.GetAttributes().AsMap()["debug"]; ok {
if debug, ok := debugRaw.(bool); !ok {
m.logger.Error("viam-agent debug attribute is present but is not a bool")
} else {
// note: if this is present (true or false, rather than missing) it overrides the CLI debug switch.
// if the user removes the `debug` attribute, we don't revert to the CLI debug switch state. (we ideally should).
// note: this assumes m.logger is the global logger shared by the other subsystems.
if debug {
m.logger.SetLevel(logging.DEBUG)
} else {
m.logger.SetLevel(logging.INFO)
}
}
}
}
}

// GetConfig retrieves the configuration from the cloud, or returns a cached version if unable to communicate.
func (m *Manager) GetConfig(ctx context.Context) (map[string]*pb.DeviceSubsystemConfig, time.Duration, error) {
if m.cloudConfig == nil {
Expand Down Expand Up @@ -453,6 +474,8 @@ func (m *Manager) GetConfig(ctx context.Context) (map[string]*pb.DeviceSubsystem
m.logger.Error(errw.Wrap(err, "saving agent config to cache"))
}

m.processConfig(resp.GetSubsystemConfigs())

interval := resp.GetCheckInterval().AsDuration()

if interval < minimalCheckInterval {
Expand Down

0 comments on commit ed72f69

Please sign in to comment.