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

Prefix event key attributes with identifier #534

Merged
merged 1 commit into from
May 24, 2023
Merged
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
23 changes: 15 additions & 8 deletions internal/server/event_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,28 +190,35 @@ func (s *EventServer) logRateLimitMiddleware(h http.Handler) http.Handler {
})
}

// eventKeyFunc generates a unique key for an event based on the provided HTTP
// request, which can be used to deduplicate events. The key is calculated by
// concatenating specific event attributes and hashing them using SHA-256.
// The key is then returned as a hex-encoded string.
//
// The event attributes are prefixed with an identifier to avoid collisions
// between different event attributes.
func eventKeyFunc(r *http.Request) (string, error) {
event := r.Context().Value(eventContextKey{}).(*eventv1.Event)

comps := []string{
"event",
event.InvolvedObject.Name,
event.InvolvedObject.Namespace,
event.InvolvedObject.Kind,
event.Message,
"name=" + event.InvolvedObject.Name,
"namespace=" + event.InvolvedObject.Namespace,
"kind=" + event.InvolvedObject.Kind,
"message=" + event.Message,
}

revision, ok := event.Metadata[eventv1.MetaRevisionKey]
if ok {
comps = append(comps, revision)
comps = append(comps, "revision="+revision)
}

token, ok := event.Metadata[eventv1.MetaTokenKey]
if ok {
comps = append(comps, token)
comps = append(comps, "token="+token)
}

val := strings.Join(comps, "/")
digest := sha256.Sum256([]byte(val))
key := strings.Join(comps, "/")
digest := sha256.Sum256([]byte(key))
return fmt.Sprintf("%x", digest), nil
}