From ca9e2d2f07e112c1b4b8db88249450b1d52be5bf Mon Sep 17 00:00:00 2001 From: Chlins Zhang Date: Mon, 3 Apr 2023 12:02:19 +0800 Subject: [PATCH] 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 Co-authored-by: Wang Yan --- src/pkg/notifier/formats/cloudevents.go | 15 ++++++--------- src/pkg/notifier/formats/cloudevents_test.go | 2 +- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/src/pkg/notifier/formats/cloudevents.go b/src/pkg/notifier/formats/cloudevents.go index 7c5c820d9..a7ac53f1c 100644 --- a/src/pkg/notifier/formats/cloudevents.go +++ b/src/pkg/notifier/formats/cloudevents.go @@ -27,7 +27,6 @@ import ( "github.com/goharbor/harbor/src/controller/event" "github.com/goharbor/harbor/src/lib" "github.com/goharbor/harbor/src/lib/errors" - "github.com/goharbor/harbor/src/lib/log" "github.com/goharbor/harbor/src/pkg/notifier/model" ) @@ -43,7 +42,8 @@ func init() { const ( // CloudEventsFormat is the type for CloudEvents format. 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 = "operator" ) @@ -79,6 +79,7 @@ type CloudEvents struct{} /* { "specversion":"1.0", + "requestid": "2eedfab8-61d3-4f3c-8ec3-8f82d1ec4c84", "id":"4b2f89a6-548d-4c12-9993-a1f5790b97d2", "source":"/projects/1/webhook/policies/3", "type":"harbor.artifact.pulled", @@ -114,13 +115,9 @@ func (ce *CloudEvents) Format(ctx context.Context, he *model.HookEvent) (http.He } event := cloudevents.NewEvent() - // retrieve request id from context as id, set to uuid if not found - id := lib.GetXRequestID(ctx) - if len(id) == 0 { - id = uuid.NewString() - log.Warningf("cannot extract request id from context, use UUID %s instead", id) - } - event.SetID(id) + // the cloudEvents id is uuid, but we insert the request id as extension which can be used to trace the event. + event.SetID(uuid.NewString()) + event.SetExtension(extRequestID, lib.GetXRequestID(ctx)) event.SetSource(source(he.ProjectID, he.PolicyID)) event.SetType(eventType) event.SetTime(time.Unix(he.Payload.OccurAt, 0)) diff --git a/src/pkg/notifier/formats/cloudevents_test.go b/src/pkg/notifier/formats/cloudevents_test.go index c5d96dc8e..ebcc523e4 100644 --- a/src/pkg/notifier/formats/cloudevents_test.go +++ b/src/pkg/notifier/formats/cloudevents_test.go @@ -75,7 +75,7 @@ func TestCloudEvents_Format(t *testing.T) { err = json.Unmarshal(data, &event) assert.NoError(t, err) 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, "harbor.artifact.pulled", event.Type()) assert.Equal(t, "application/json", event.DataContentType())