Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

APP-6990 consume debug level from cloud config #50

Merged
merged 3 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
abe-winter marked this conversation as resolved.
Show resolved Hide resolved
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
Loading