Skip to content

Commit

Permalink
make updateLoggerRegistry a helper in config
Browse files Browse the repository at this point in the history
  • Loading branch information
benjirewis committed Sep 6, 2024
1 parent 84315d6 commit 56efd88
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 47 deletions.
33 changes: 33 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -1108,3 +1108,36 @@ type Revision struct {
Revision string
LastUpdated time.Time
}

// UpdateLoggerRegistryFromConfig will update the passed in registry with all log patterns in
// `cfg.LogConfig` and each resource's `LogConfiguration` field if present.
func UpdateLoggerRegistryFromConfig(registry *logging.Registry, cfg *Config, warnLogger logging.Logger) {
var combinedLogCfg []logging.LoggerPatternConfig
if cfg.LogConfig != nil {
combinedLogCfg = append(combinedLogCfg, cfg.LogConfig...)
}

for _, serv := range cfg.Services {
if serv.LogConfiguration != nil {
resLogCfg := logging.LoggerPatternConfig{
Pattern: "rdk.resource_manager." + serv.ResourceName().String(),
Level: serv.LogConfiguration.Level.String(),
}
combinedLogCfg = append(combinedLogCfg, resLogCfg)
}
}
for _, comp := range cfg.Components {
if comp.LogConfiguration != nil {
resLogCfg := logging.LoggerPatternConfig{
Pattern: "rdk.resource_manager." + comp.ResourceName().String(),
Level: comp.LogConfiguration.Level.String(),
}
combinedLogCfg = append(combinedLogCfg, resLogCfg)
}
}

if err := registry.Update(combinedLogCfg, warnLogger); err != nil {
warnLogger.Warnw("Error processing log patterns",
"error", err)
}
}
28 changes: 14 additions & 14 deletions logging/log_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,34 @@ type LoggerPatternConfig struct {
}

const (
// Pattern matching on loggers. Examples describe regular expression below.
// Regular expressions for logger names (non-resource loggers). Examples
// describe the regular expression that follows.

// e.g. "foo"
// e.g. "foo".
validLoggerSectionName = `[a-zA-Z0-9]+([_-]*[a-zA-Z0-9]+)*`
// e.g. "foo" or "*"
// e.g. "foo" or "*".
validLoggerSectionNameWithWildcard = `(` + validLoggerSectionName + `|\*)`
// e.g. "foo.bar"
validLoggerSections = validLoggerSectionName + `(\.` + validLoggerSectionName + `)*`
// e.g. "foo.*.foo"
// e.g. "foo.*.foo".
validLoggerSectionsWithWildcard = validLoggerSectionNameWithWildcard + `(\.` + validLoggerSectionNameWithWildcard + `)*`
// Restricts above regex to be the entire pattern.
validLoggerName = `^` + validLoggerSectionsWithWildcard + `$`

// Resource configurations. Examples describe regular expression below.
// Regular expressions for resource logger names. Examples describe the
// regular expression that follows.

// e.g. "foo-bar"
// e.g. "foo-bar".
validNamespacePattern = `([\w-]+|\*)`
// e.g. "service" or "component" or "*"
// e.g. "service" or "component" or "*".
validResourceTypePattern = `(service|component|\*)`
// e.g. "foo-bar"
// e.g. "foo-bar".
validResourceSubTypePattern = validNamespacePattern
// e.g. "foo-bar"
// e.g. "foo-bar".
validModelNamePattern = validNamespacePattern
// e.g. "service:foo" or "remote:"
// e.g. "service:foo" or "remote:".
validTypeSubsectionPattern = `(` + validResourceTypePattern + `:` + validResourceSubTypePattern + `|remote:)`
// e.g. "rdk.resource_manager.rdk:component:motor/foo"
// e.g. "rdk.resource_manager.rdk:component:motor/foo".
validResourcePattern = `^rdk.resource_manager.` + validNamespacePattern + `:` + validTypeSubsectionPattern + `\/` +
validModelNamePattern + `$` // e.g. "rdk.resource_manager.rdk:component:motor/foo"
validModelNamePattern + `$`
)

var (
Expand Down
35 changes: 2 additions & 33 deletions web/server/entrypoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,37 +209,6 @@ func (s *robotServer) createWebOptions(cfg *config.Config) (weboptions.Options,
return options, nil
}

func (s *robotServer) updateLoggerRegistry(cfg *config.Config) {
var combinedLogCfg []logging.LoggerPatternConfig
if cfg.LogConfig != nil {
combinedLogCfg = append(combinedLogCfg, cfg.LogConfig...)
}

for _, serv := range cfg.Services {
if serv.LogConfiguration != nil {
resLogCfg := logging.LoggerPatternConfig{
Pattern: "rdk.resource_manager." + serv.ResourceName().String(),
Level: serv.LogConfiguration.Level.String(),
}
combinedLogCfg = append(combinedLogCfg, resLogCfg)
}
}
for _, comp := range cfg.Components {
if comp.LogConfiguration != nil {
resLogCfg := logging.LoggerPatternConfig{
Pattern: "rdk.resource_manager." + comp.ResourceName().String(),
Level: comp.LogConfiguration.Level.String(),
}
combinedLogCfg = append(combinedLogCfg, resLogCfg)
}
}

if err := s.registry.Update(combinedLogCfg, s.logger); err != nil {
s.logger.Errorw("Error processing log patterns",
"error", err)
}
}

func (s *robotServer) serveWeb(ctx context.Context, cfg *config.Config) (err error) {
ctx, cancel := context.WithCancel(ctx)

Expand Down Expand Up @@ -337,7 +306,7 @@ func (s *robotServer) serveWeb(ctx context.Context, cfg *config.Config) (err err

// Update logger registry as soon as we have fully processed config. Further
// updates to the registry will be handled by the config watcher goroutine.
s.updateLoggerRegistry(processedConfig)
config.UpdateLoggerRegistryFromConfig(s.registry, processedConfig, s.logger)

if processedConfig.Cloud != nil {
cloudRestartCheckerActive = make(chan struct{})
Expand Down Expand Up @@ -442,7 +411,7 @@ func (s *robotServer) serveWeb(ctx context.Context, cfg *config.Config) (err err
// Update logger registry if log patterns may have changed.
if !diff.LogEqual {
s.logger.Debug("Detected potential changes to log patterns; updating logger levels")
s.updateLoggerRegistry(processedConfig)
config.UpdateLoggerRegistryFromConfig(s.registry, processedConfig, s.logger)
}

myRobot.Reconfigure(ctx, processedConfig)
Expand Down

0 comments on commit 56efd88

Please sign in to comment.