diff --git a/pkg/chipingress/client.go b/pkg/chipingress/client.go index 7b477487a7..c4e45cc943 100644 --- a/pkg/chipingress/client.go +++ b/pkg/chipingress/client.go @@ -194,13 +194,14 @@ func NewEvent(domain, entity string, payload []byte, attributes map[string]any) attributes = make(map[string]any) } - if val, ok := attributes["recordedtime"].(time.Time); ok { - event.SetExtension("recordedtime", val.UTC()) - } else { - event.SetExtension("recordedtime", ce.Timestamp{Time: time.Now().UTC()}) + recordedTime := time.Now() + if val, ok := attributes["recordedtime"].(time.Time); ok && !val.IsZero() { + recordedTime = val } + recordedTime = recordedTime.UTC().Truncate(time.Millisecond) + event.SetExtension("recordedtime", ce.Timestamp{Time: recordedTime}) - if val, ok := attributes["time"].(time.Time); ok { + if val, ok := attributes["time"].(time.Time); ok && !val.IsZero() { event.SetTime(val.UTC()) } if val, ok := attributes["datacontenttype"].(string); ok { diff --git a/pkg/chipingress/client_test.go b/pkg/chipingress/client_test.go index c8b2db8924..8bbd381ed2 100644 --- a/pkg/chipingress/client_test.go +++ b/pkg/chipingress/client_test.go @@ -67,7 +67,7 @@ func TestNewEvent(t *testing.T) { "datacontenttype": "application/protobuf", "dataschema": "https://example.com/schema", "subject": "example-subject", - "time": time.Now(), + "time": time.Now().Add(-5 * time.Second), } assert.NoError(t, err) @@ -87,7 +87,6 @@ func TestNewEvent(t *testing.T) { assert.Equal(t, "example-subject", event.Subject()) assert.Equal(t, attributes["time"].(time.Time).UTC(), event.Time()) assert.NotEmpty(t, event.Extensions()["recordedtime"]) - assert.NotEmpty(t, event.Extensions()["recordedtime"]) assert.True(t, event.Extensions()["recordedtime"].(ce.Timestamp).Time.After(attributes["time"].(time.Time))) // Assert the event data was set as expected