Skip to content

Commit

Permalink
Clean up logging of watcher kinds (#33957)
Browse files Browse the repository at this point in the history
We were logging the raw struct, which is hard to read.
Log only the values of kind strings instead.
  • Loading branch information
zmb3 authored Oct 31, 2023
1 parent 46afb1e commit 6e77bcb
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions lib/services/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,21 @@ type resourceCollector interface {
initializationChan() <-chan struct{}
}

func watchKindsString(kinds []types.WatchKind) string {
var sb strings.Builder
for i, k := range kinds {
if i != 0 {
sb.WriteString(", ")
}
sb.WriteString(k.Kind)
if k.SubKind != "" {
sb.WriteString("/")
sb.WriteString(k.SubKind)
}
}
return sb.String()
}

// ResourceWatcherConfig configures resource watcher.
type ResourceWatcherConfig struct {
// Component is a component used in logs.
Expand Down Expand Up @@ -111,7 +126,8 @@ func newResourceWatcher(ctx context.Context, collector resourceCollector, cfg Re
if err != nil {
return nil, trace.Wrap(err)
}
cfg.Log = cfg.Log.WithField("resource-kind", collector.resourceKinds())

cfg.Log = cfg.Log.WithField("resource-kind", watchKindsString(collector.resourceKinds()))
ctx, cancel := context.WithCancel(ctx)
p := &resourceWatcher{
ResourceWatcherConfig: cfg,
Expand Down Expand Up @@ -185,13 +201,9 @@ func (p *resourceWatcher) WaitInitialization() error {
case <-p.collector.initializationChan():
return nil
case <-t.C:
p.Log.Debugf("ResourceWatcher %s is not yet initialized.", p.collector.resourceKinds())
p.Log.Debug("ResourceWatcher is not yet initialized.")
case <-p.ctx.Done():
var kindStrings []string
for _, kind := range p.collector.resourceKinds() {
kindStrings = append(kindStrings, kind.Kind)
}
return trace.BadParameter("ResourceWatcher %s failed to initialize.", strings.Join(kindStrings, ", "))
return trace.BadParameter("ResourceWatcher %s failed to initialize.", watchKindsString(p.collector.resourceKinds()))
}
}
}
Expand Down

0 comments on commit 6e77bcb

Please sign in to comment.