Skip to content

Commit

Permalink
REFACTOR: Event log message
Browse files Browse the repository at this point in the history
Refactor the event log message to not be as noisy when in Info log
level. When there are a lot of resources the logs where not helpfully.

Signed-off-by: Jim Fitzpatrick <[email protected]>
  • Loading branch information
Boomatang committed Jan 9, 2025
1 parent f97bc14 commit 3457be8
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion controllers/event_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package controllers

import (
"context"
"slices"
"sync"

"github.com/kuadrant/policy-machinery/controller"
Expand All @@ -16,13 +17,15 @@ func NewEventLogger() *EventLogger {

func (e *EventLogger) Log(ctx context.Context, resourceEvents []controller.ResourceEvent, _ *machinery.Topology, err error, _ *sync.Map) error {
logger := controller.LoggerFromContext(ctx).WithName("event logger")
eventType := make(map[string]int, 0)
resources := make([]string, 0)
for _, event := range resourceEvents {
// log the event
obj := event.OldObject
if obj == nil {
obj = event.NewObject
}
logger.Info("new event",
logger.V(1).Info("new event",
"type", event.EventType.String(),
"kind", obj.GetObjectKind().GroupVersionKind().Kind,
"namespace", obj.GetNamespace(),
Expand All @@ -31,7 +34,20 @@ func (e *EventLogger) Log(ctx context.Context, resourceEvents []controller.Resou
if err != nil {
logger.Error(err, "error passed to reconcile")
}

_, ok := eventType[event.EventType.String()]
if ok {
eventType[event.EventType.String()]++
} else {
eventType[event.EventType.String()] = 1
}

if !slices.Contains(resources, obj.GetObjectKind().GroupVersionKind().Kind) {
resources = append(resources, obj.GetObjectKind().GroupVersionKind().Kind)
}
}

logger.Info("new events", "resources", resources, "eventTypes", eventType)

return nil
}

0 comments on commit 3457be8

Please sign in to comment.