diff --git a/clients/pkg/promtail/targets/azureeventhubs/parser.go b/clients/pkg/promtail/targets/azureeventhubs/parser.go index 9f65b0fc83509..659f1a2e7a643 100644 --- a/clients/pkg/promtail/targets/azureeventhubs/parser.go +++ b/clients/pkg/promtail/targets/azureeventhubs/parser.go @@ -61,22 +61,23 @@ func (l azureMonitorResourceLog) isTimeOrTimeStampFieldSet() bool { // getTime returns time from `time` or `timeStamp` field. If both fields are set, `time` is used. If both fields are empty, error is returned. func (l azureMonitorResourceLog) getTime() (time.Time, error) { if len(l.Time) == 0 && len(l.TimeStamp) == 0 { + var t time.Time return t, errors.New("time and timeStamp fields are empty") } - + if len(l.Time) != 0 { t, err := time.Parse(time.RFC3339, l.Time) if err != nil { return t, err } - + return t.UTC(), nil } - t, err := time.Parse(time.RFC3339, l.TimeStamp) - if err != nil { - return t, err - } + t, err := time.Parse(time.RFC3339, l.TimeStamp) + if err != nil { + return t, err + } return t.UTC(), nil } diff --git a/clients/pkg/promtail/targets/azureeventhubs/parser_test.go b/clients/pkg/promtail/targets/azureeventhubs/parser_test.go index 6112b94e8752e..662dce4358790 100644 --- a/clients/pkg/promtail/targets/azureeventhubs/parser_test.go +++ b/clients/pkg/promtail/targets/azureeventhubs/parser_test.go @@ -273,3 +273,17 @@ func Test_parseMessage_message_without_time_with_time_stamp(t *testing.T) { assert.Equal(t, time.Date(2024, time.September, 18, 00, 45, 9, 0, time.UTC), entries[0].Timestamp) } + +func Test_parseMessage_message_without_time_and_time_stamp(t *testing.T) { + messageParser := &messageParser{ + disallowCustomMessages: true, + } + + message := &sarama.ConsumerMessage{ + Value: readFile(t, "testdata/message_without_time_and_time_stamp.json"), + Timestamp: time.Date(2023, time.March, 17, 8, 44, 02, 0, time.UTC), + } + + _, err := messageParser.Parse(message, nil, nil, true) + assert.EqualError(t, err, "required field or fields is empty") +} diff --git a/clients/pkg/promtail/targets/azureeventhubs/testdata/message_without_time_and_time_stamp.json b/clients/pkg/promtail/targets/azureeventhubs/testdata/message_without_time_and_time_stamp.json new file mode 100644 index 0000000000000..f9fc41ad02aea --- /dev/null +++ b/clients/pkg/promtail/targets/azureeventhubs/testdata/message_without_time_and_time_stamp.json @@ -0,0 +1,9 @@ +{ + "records": [ + { + "resourceId": "/RESOURCE_ID", + "operationName": "ApplicationGatewayAccess", + "category": "ApplicationGatewayAccessLog" + } + ] +} \ No newline at end of file