Skip to content

Commit

Permalink
Exclude eventv1.MetaTokenKey from event metadata
Browse files Browse the repository at this point in the history
eventv1.MetaTokenKey is required to be considered in rate limiting but
it is only for internal use by flux components and should not be sent to
the alert provider. Remove eventv1.MetaTokenKey from the metadata of
event before processing the event for various matching alerts.

Signed-off-by: Sunny <darkowlzz@protonmail.com>
  • Loading branch information
darkowlzz committed Dec 13, 2023
1 parent 7834608 commit 68c3824
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
14 changes: 14 additions & 0 deletions internal/server/event_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ func (s *EventServer) handleEvent() func(w http.ResponseWriter, r *http.Request)
ctx, cancel := context.WithTimeout(r.Context(), 15*time.Second)
defer cancel()

// Remove any internal metadata before further processing the event.
excludeInternalMetadata(event)

alerts, err := s.getAllAlertsForEvent(ctx, event)
if err != nil {
eventLogger.Error(err, "failed to get alerts for the event")
Expand Down Expand Up @@ -440,3 +443,14 @@ func (s *EventServer) enhanceEventWithAlertMetadata(ctx context.Context, event *
event.Metadata = meta
}
}

// excludeInternalMetadata removes any internal metadata from the given event.
func excludeInternalMetadata(event *eventv1.Event) {
if len(event.Metadata) == 0 {
return
}
excludeList := []string{eventv1.MetaTokenKey}
for _, key := range excludeList {
delete(event.Metadata, key)
}
}
32 changes: 32 additions & 0 deletions internal/server/event_handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1058,3 +1058,35 @@ func TestEnhanceEventWithAlertMetadata(t *testing.T) {
})
}
}

func Test_excludeInternalMetadata(t *testing.T) {
tests := []struct {
name string
event eventv1.Event
wantMetadata map[string]string
}{
{
name: "no metadata",
},
{
name: "internal metadata",
event: eventv1.Event{
Metadata: map[string]string{
eventv1.MetaTokenKey: "aaaa",
eventv1.MetaRevisionKey: "bbbb",
},
},
wantMetadata: map[string]string{
eventv1.MetaRevisionKey: "bbbb",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
g := NewWithT(t)

excludeInternalMetadata(&tt.event)
g.Expect(tt.event.Metadata).To(BeEquivalentTo(tt.wantMetadata))
})
}
}

0 comments on commit 68c3824

Please sign in to comment.