mirror of
https://github.com/goharbor/harbor.git
synced 2024-12-27 02:58:05 +01:00
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:
parent
fddfaa7ba5
commit
ca9e2d2f07
@ -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))
|
||||||
|
@ -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())
|
||||||
|
Loading…
Reference in New Issue
Block a user