fix: generate uuid as CloudEvents id and add additional requestid (#18451)

1. Change the the value of CloudEvents id from requestid to uuid
2. Add additional requestid to trace to event

Fixes: #18448

Signed-off-by: chlins <chenyuzh@vmware.com>
Co-authored-by: Wang Yan <wangyan@vmware.com>
This commit is contained in:
Chlins Zhang 2023-04-03 12:02:19 +08:00 committed by GitHub
parent fddfaa7ba5
commit ca9e2d2f07
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 10 deletions

View File

@ -27,7 +27,6 @@ import (
"github.com/goharbor/harbor/src/controller/event" "github.com/goharbor/harbor/src/controller/event"
"github.com/goharbor/harbor/src/lib" "github.com/goharbor/harbor/src/lib"
"github.com/goharbor/harbor/src/lib/errors" "github.com/goharbor/harbor/src/lib/errors"
"github.com/goharbor/harbor/src/lib/log"
"github.com/goharbor/harbor/src/pkg/notifier/model" "github.com/goharbor/harbor/src/pkg/notifier/model"
) )
@ -43,7 +42,8 @@ func init() {
const ( const (
// CloudEventsFormat is the type for CloudEvents format. // CloudEventsFormat is the type for CloudEvents format.
CloudEventsFormat = "CloudEvents" CloudEventsFormat = "CloudEvents"
// extRequestID is the key for the request id in the CloudEvents.
extRequestID = "requestid"
// extOperator is the key for the operator in the CloudEvents. // extOperator is the key for the operator in the CloudEvents.
extOperator = "operator" extOperator = "operator"
) )
@ -79,6 +79,7 @@ type CloudEvents struct{}
/* /*
{ {
"specversion":"1.0", "specversion":"1.0",
"requestid": "2eedfab8-61d3-4f3c-8ec3-8f82d1ec4c84",
"id":"4b2f89a6-548d-4c12-9993-a1f5790b97d2", "id":"4b2f89a6-548d-4c12-9993-a1f5790b97d2",
"source":"/projects/1/webhook/policies/3", "source":"/projects/1/webhook/policies/3",
"type":"harbor.artifact.pulled", "type":"harbor.artifact.pulled",
@ -114,13 +115,9 @@ func (ce *CloudEvents) Format(ctx context.Context, he *model.HookEvent) (http.He
} }
event := cloudevents.NewEvent() event := cloudevents.NewEvent()
// retrieve request id from context as id, set to uuid if not found // the cloudEvents id is uuid, but we insert the request id as extension which can be used to trace the event.
id := lib.GetXRequestID(ctx) event.SetID(uuid.NewString())
if len(id) == 0 { event.SetExtension(extRequestID, lib.GetXRequestID(ctx))
id = uuid.NewString()
log.Warningf("cannot extract request id from context, use UUID %s instead", id)
}
event.SetID(id)
event.SetSource(source(he.ProjectID, he.PolicyID)) event.SetSource(source(he.ProjectID, he.PolicyID))
event.SetType(eventType) event.SetType(eventType)
event.SetTime(time.Unix(he.Payload.OccurAt, 0)) event.SetTime(time.Unix(he.Payload.OccurAt, 0))

View File

@ -75,7 +75,7 @@ func TestCloudEvents_Format(t *testing.T) {
err = json.Unmarshal(data, &event) err = json.Unmarshal(data, &event)
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, "1.0", event.SpecVersion()) assert.Equal(t, "1.0", event.SpecVersion())
assert.Equal(t, requestID, event.ID()) assert.Equal(t, requestID, event.Extensions()["requestid"])
assert.Equal(t, "/projects/1/webhook/policies/3", event.Source()) assert.Equal(t, "/projects/1/webhook/policies/3", event.Source())
assert.Equal(t, "harbor.artifact.pulled", event.Type()) assert.Equal(t, "harbor.artifact.pulled", event.Type())
assert.Equal(t, "application/json", event.DataContentType()) assert.Equal(t, "application/json", event.DataContentType())