-
Notifications
You must be signed in to change notification settings - Fork 208
Add Event key to identify events #1440
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| // Copyright 2021 The PipeCD Authors. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package model | ||
|
|
||
| import ( | ||
| "crypto/sha256" | ||
| "encoding/hex" | ||
| "fmt" | ||
| "sort" | ||
| "strings" | ||
| ) | ||
|
|
||
| var hashFunc = sha256.New | ||
|
|
||
| // MakeEventKey builds a fixed-length identifier based on the given name | ||
| // and labels. It returns the exact same string as long as both are the same. | ||
| func MakeEventKey(name string, labels map[string]string) string { | ||
| h := hashFunc() | ||
| if len(labels) == 0 { | ||
| h.Write([]byte(name)) | ||
| return hex.EncodeToString(h.Sum(nil)) | ||
| } | ||
|
|
||
| var b strings.Builder | ||
| b.WriteString(name) | ||
|
|
||
| // Guarantee uniqueness by sorting by keys. | ||
| keys := make([]string, 0, len(labels)) | ||
| for key := range labels { | ||
| keys = append(keys, key) | ||
| } | ||
| sort.Strings(keys) | ||
| for _, key := range keys { | ||
| b.WriteString(fmt.Sprintf("/%s:%s", key, labels[key])) | ||
| } | ||
|
|
||
| // Make a hash to be fixed length regardless of the length of the event key. | ||
| h.Write([]byte(b.String())) | ||
| return hex.EncodeToString(h.Sum(nil)) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,6 +31,8 @@ message Event { | |
| // The key/value pairs that are attached to event. | ||
| // This is intended to be used to specify additional attributes of event. | ||
| map<string,string> labels = 5; | ||
| // A fixed-length identifier consists of its own name and labels. | ||
| string event_key = 6 [(validate.rules).string.min_len = 1]; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Where is the hash value that we talked about before?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My apologies, I totally forgot it.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you. I've done added the hash just now. |
||
|
|
||
| // Unix time when the event was created. | ||
| int64 created_at = 14 [(validate.rules).int64.gt = 0]; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| // Copyright 2021 The PipeCD Authors. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package model | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestMakeEventKey(t *testing.T) { | ||
| testcases := []struct { | ||
| testname string | ||
| name1 string | ||
| labels1 map[string]string | ||
| name2 string | ||
| labels2 map[string]string | ||
| wantSame bool | ||
| }{ | ||
| { | ||
| testname: "no name and labels given", | ||
| wantSame: true, | ||
| }, | ||
| { | ||
| testname: "no labels given", | ||
| name1: "name", | ||
| name2: "name", | ||
| wantSame: true, | ||
| }, | ||
| { | ||
| testname: "no name given", | ||
| labels1: map[string]string{ | ||
| "key1": "value1", | ||
| }, | ||
| labels2: map[string]string{ | ||
| "key1": "value1", | ||
| }, | ||
| wantSame: true, | ||
| }, | ||
| { | ||
| testname: "the exact same labels given", | ||
| name1: "name", | ||
| labels1: map[string]string{ | ||
| "key1": "value", | ||
| "key2": "value", | ||
| "key3": "value", | ||
| }, | ||
| name2: "name", | ||
| labels2: map[string]string{ | ||
| "key2": "value", | ||
| "key3": "value", | ||
| "key1": "value", | ||
| }, | ||
| wantSame: true, | ||
| }, | ||
| { | ||
| testname: "the sub match labels given", | ||
| name1: "name", | ||
| labels1: map[string]string{ | ||
| "key1": "value", | ||
| "key2": "value", | ||
| "key3": "value", | ||
| }, | ||
| name2: "name", | ||
| labels2: map[string]string{ | ||
| "key1": "value", | ||
| }, | ||
| wantSame: false, | ||
| }, | ||
| } | ||
| for _, tc := range testcases { | ||
| t.Run(tc.testname, func(t *testing.T) { | ||
| // Check if we can get the exact same string. | ||
| got1 := MakeEventKey(tc.name1, tc.labels1) | ||
| got2 := MakeEventKey(tc.name2, tc.labels2) | ||
| assert.NotEmpty(t, got1) | ||
| assert.NotEmpty(t, got2) | ||
| assert.Equal(t, tc.wantSame, got1 == got2) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add the check of key length to ensure that they are not empty strings. |
||
| }) | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: This should be done before locking.