Skip to content

Commit

Permalink
Add test to for message without time and timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
andriikushch committed Oct 29, 2024
1 parent 471c63b commit 1c8ca38
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
13 changes: 7 additions & 6 deletions clients/pkg/promtail/targets/azureeventhubs/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
14 changes: 14 additions & 0 deletions clients/pkg/promtail/targets/azureeventhubs/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"records": [
{
"resourceId": "/RESOURCE_ID",
"operationName": "ApplicationGatewayAccess",
"category": "ApplicationGatewayAccessLog"
}
]
}

0 comments on commit 1c8ca38

Please sign in to comment.