diff --git a/internal/component/azure/servicebus/message.go b/internal/component/azure/servicebus/message.go index a709c7a79d..8a73a70555 100644 --- a/internal/component/azure/servicebus/message.go +++ b/internal/component/azure/servicebus/message.go @@ -159,6 +159,13 @@ func addMetadataToMessage(asbMsg *azservicebus.Message, metadata map[string]stri timeVal, err := time.Parse(http.TimeFormat, v) if err == nil { asbMsg.ScheduledEnqueueTime = &timeVal + } else { + timeVal, err2 := time.Parse(time.RFC3339, v) + if err2 == nil { + asbMsg.ScheduledEnqueueTime = &timeVal + } else { + return fmt.Errorf("invalid time format for %s; expected HTTP time format or RFC3339", k) + } } // Fallback: set as application property diff --git a/internal/component/azure/servicebus/metadata_test.go b/internal/component/azure/servicebus/metadata_test.go index 02ee11eb80..04cd5789a1 100644 --- a/internal/component/azure/servicebus/metadata_test.go +++ b/internal/component/azure/servicebus/metadata_test.go @@ -16,6 +16,7 @@ package servicebus import ( "testing" + azservicebus "github.com/Azure/azure-sdk-for-go/sdk/messaging/azservicebus" "github.com/stretchr/testify/assert" ) @@ -505,4 +506,29 @@ func TestParseServiceBusMetadata(t *testing.T) { assert.Nil(t, m.LockDurationInSec) assert.Nil(t, err) }) + + t.Run("Test add system metadata: ScheduledEnqueueTimeUtc", func(t *testing.T) { + msg := azservicebus.Message{} + metadata := map[string]string{ + MessageKeyScheduledEnqueueTimeUtc: "2024-06-15T13:45:30.00000000Z", + } + parseErr := addMetadataToMessage(&msg, metadata) + assert.NoError(t, parseErr) + assert.Equal(t, int64(1718459130000000), msg.ScheduledEnqueueTime.UnixMicro()) + + msg2 := azservicebus.Message{} + metadata2 := map[string]string{ + MessageKeyScheduledEnqueueTimeUtc: "Sat, 15 Jun 2024 13:45:30 GMT", + } + parseErr2 := addMetadataToMessage(&msg2, metadata2) + assert.NoError(t, parseErr2) + assert.Equal(t, int64(1718459130000000), msg2.ScheduledEnqueueTime.UnixMicro()) + + msg3 := azservicebus.Message{} + metadata3 := map[string]string{ + MessageKeyScheduledEnqueueTimeUtc: "Sat 2024-06-15 12:13:14 UTC+4", + } + parseErr3 := addMetadataToMessage(&msg3, metadata3) + assert.Error(t, parseErr3) + }) }