-
Notifications
You must be signed in to change notification settings - Fork 214
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Trims timestamp from log message if customer enables on logs plugin #1568
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6419e5b
Trims timestamp from log message if customer enables on logs plugin
nathalapooja fe212de
fixed unit test
nathalapooja 2614141
fixed lint issues
nathalapooja 0b1b792
lint errors
nathalapooja 2d975d8
fixing lint errors
nathalapooja File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -130,15 +130,34 @@ func TestTimestampParser(t *testing.T) { | |
expectedTimestamp := time.Unix(1497882318, 0) | ||
timestampString := "19 Jun 2017 14:25:18" | ||
logEntry := fmt.Sprintf("%s [INFO] This is a test message.", timestampString) | ||
timestamp := fileConfig.timestampFromLogLine(logEntry) | ||
timestamp, modifiedLogEntry := fileConfig.timestampFromLogLine(logEntry) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It depends on the TrimTimestamp flag right. so I just kept it generic |
||
assert.Equal(t, expectedTimestamp.UnixNano(), timestamp.UnixNano(), | ||
fmt.Sprintf("The timestampFromLogLine value %v is not the same as expected %v.", timestamp, expectedTimestamp)) | ||
assert.Equal(t, logEntry, modifiedLogEntry) | ||
|
||
// Test regex match for multiline, the first timestamp in multiline should be matched | ||
logEntry = fmt.Sprintf("%s [INFO] This is the first line.\n19 Jun 2017 14:25:19 [INFO] This is the second line.\n", timestampString) | ||
timestamp = fileConfig.timestampFromLogLine(logEntry) | ||
timestamp, modifiedLogEntry = fileConfig.timestampFromLogLine(logEntry) | ||
assert.Equal(t, expectedTimestamp.UnixNano(), timestamp.UnixNano(), | ||
fmt.Sprintf("The timestampFromLogLine value %v is not the same as expected %v.", timestamp, expectedTimestamp)) | ||
assert.Equal(t, logEntry, modifiedLogEntry) | ||
|
||
// Test TrimTimeStamp for single line | ||
fileConfig.TrimTimestamp = true | ||
logEntry = fmt.Sprintf("%s [INFO] This is a test message.", timestampString) | ||
trimmedTimestampString := " [INFO] This is a test message." | ||
timestamp, modifiedLogEntry = fileConfig.timestampFromLogLine(logEntry) | ||
assert.Equal(t, expectedTimestamp.UnixNano(), timestamp.UnixNano(), | ||
fmt.Sprintf("The timestampFromLogLine value %v is not the same as expected %v.", timestamp, expectedTimestamp)) | ||
assert.Equal(t, trimmedTimestampString, modifiedLogEntry) | ||
|
||
// Test TrimTimeStamp for multiline, the first timestamp in multiline should be matched | ||
logEntry = fmt.Sprintf("%s [INFO] This is the first line.\n19 Jun 2017 14:25:19 [INFO] This is the second line.\n", timestampString) | ||
trimmedTimestampString = " [INFO] This is the first line.\n19 Jun 2017 14:25:19 [INFO] This is the second line.\n" | ||
timestamp, modifiedLogEntry = fileConfig.timestampFromLogLine(logEntry) | ||
assert.Equal(t, expectedTimestamp.UnixNano(), timestamp.UnixNano(), | ||
fmt.Sprintf("The timestampFromLogLine value %v is not the same as expected %v.", timestamp, expectedTimestamp)) | ||
assert.Equal(t, trimmedTimestampString, modifiedLogEntry) | ||
} | ||
|
||
func TestTimestampParserWithPadding(t *testing.T) { | ||
|
@@ -155,15 +174,33 @@ func TestTimestampParserWithPadding(t *testing.T) { | |
Timezone: timezone, | ||
TimezoneLoc: timezoneLoc} | ||
|
||
logEntry := fmt.Sprintf(" 2 1 07:10:06 instance-id: i-02fce21a425a2efb3") | ||
timestamp := fileConfig.timestampFromLogLine(logEntry) | ||
logEntry := " 2 1 07:10:06 instance-id: i-02fce21a425a2efb3" | ||
timestamp, modifiedLogEntry := fileConfig.timestampFromLogLine(logEntry) | ||
assert.Equal(t, 7, timestamp.Hour(), fmt.Sprintf("Timestamp does not match: %v, act: %v", "7", timestamp.Hour())) | ||
assert.Equal(t, 10, timestamp.Minute(), fmt.Sprintf("Timestamp does not match: %v, act: %v", "10", timestamp.Minute())) | ||
assert.Equal(t, logEntry, modifiedLogEntry) | ||
|
||
logEntry = "2 1 07:10:06 instance-id: i-02fce21a425a2efb3" | ||
timestamp, modifiedLogEntry = fileConfig.timestampFromLogLine(logEntry) | ||
assert.Equal(t, 7, timestamp.Hour(), fmt.Sprintf("Timestamp does not match: %v, act: %v", "7", timestamp.Hour())) | ||
assert.Equal(t, 10, timestamp.Minute(), fmt.Sprintf("Timestamp does not match: %v, act: %v", "10", timestamp.Minute())) | ||
assert.Equal(t, logEntry, modifiedLogEntry) | ||
|
||
//Test when TrimTimeStamp is enabled | ||
fileConfig.TrimTimestamp = true | ||
logEntry = " 2 1 07:10:06 instance-id: i-02fce21a425a2efb3" | ||
trimmedTimestampString := " instance-id: i-02fce21a425a2efb3" | ||
timestamp, modifiedLogEntry = fileConfig.timestampFromLogLine(logEntry) | ||
assert.Equal(t, 7, timestamp.Hour(), fmt.Sprintf("Timestamp does not match: %v, act: %v", "7", timestamp.Hour())) | ||
assert.Equal(t, 10, timestamp.Minute(), fmt.Sprintf("Timestamp does not match: %v, act: %v", "10", timestamp.Minute())) | ||
assert.Equal(t, trimmedTimestampString, modifiedLogEntry) | ||
|
||
logEntry = fmt.Sprintf("2 1 07:10:06 instance-id: i-02fce21a425a2efb3") | ||
timestamp = fileConfig.timestampFromLogLine(logEntry) | ||
logEntry = "2 1 07:10:06 instance-id: i-02fce21a425a2efb3" | ||
trimmedTimestampString = " instance-id: i-02fce21a425a2efb3" | ||
timestamp, modifiedLogEntry = fileConfig.timestampFromLogLine(logEntry) | ||
assert.Equal(t, 7, timestamp.Hour(), fmt.Sprintf("Timestamp does not match: %v, act: %v", "7", timestamp.Hour())) | ||
assert.Equal(t, 10, timestamp.Minute(), fmt.Sprintf("Timestamp does not match: %v, act: %v", "10", timestamp.Minute())) | ||
assert.Equal(t, trimmedTimestampString, modifiedLogEntry) | ||
} | ||
|
||
func TestTimestampParserDefault(t *testing.T) { | ||
|
@@ -183,26 +220,56 @@ func TestTimestampParserDefault(t *testing.T) { | |
TimezoneLoc: timezoneLoc} | ||
|
||
// make sure layout is compatible for "Sep 9", "Sep 9" , "Sep 09", "Sep 09" options | ||
logEntry := fmt.Sprintf("Sep 9 02:00:43 ip-10-4-213-132 \n") | ||
timestamp := fileConfig.timestampFromLogLine(logEntry) | ||
logEntry := "Sep 9 02:00:43 ip-10-4-213-132 \n" | ||
timestamp, modifiedLogEntry := fileConfig.timestampFromLogLine(logEntry) | ||
assert.Equal(t, 02, timestamp.Hour()) | ||
assert.Equal(t, 00, timestamp.Minute()) | ||
assert.Equal(t, logEntry, modifiedLogEntry) | ||
|
||
logEntry = "Sep 9 02:00:43 ip-10-4-213-132 \n" | ||
timestamp, modifiedLogEntry = fileConfig.timestampFromLogLine(logEntry) | ||
assert.Equal(t, 02, timestamp.Hour()) | ||
assert.Equal(t, 00, timestamp.Minute()) | ||
assert.Equal(t, logEntry, modifiedLogEntry) | ||
|
||
logEntry = fmt.Sprintf("Sep 9 02:00:43 ip-10-4-213-132 \n") | ||
timestamp = fileConfig.timestampFromLogLine(logEntry) | ||
logEntry = "Sep 09 02:00:43 ip-10-4-213-132 \n" | ||
timestamp, modifiedLogEntry = fileConfig.timestampFromLogLine(logEntry) | ||
assert.Equal(t, 02, timestamp.Hour()) | ||
assert.Equal(t, 00, timestamp.Minute()) | ||
assert.Equal(t, logEntry, modifiedLogEntry) | ||
|
||
logEntry = fmt.Sprintf("Sep 09 02:00:43 ip-10-4-213-132 \n") | ||
timestamp = fileConfig.timestampFromLogLine(logEntry) | ||
logEntry = "Sep 09 02:00:43 ip-10-4-213-132 \n" | ||
timestamp, modifiedLogEntry = fileConfig.timestampFromLogLine(logEntry) | ||
assert.Equal(t, 02, timestamp.Hour()) | ||
assert.Equal(t, 00, timestamp.Minute()) | ||
assert.Equal(t, logEntry, modifiedLogEntry) | ||
|
||
logEntry = fmt.Sprintf("Sep 09 02:00:43 ip-10-4-213-132 \n") | ||
timestamp = fileConfig.timestampFromLogLine(logEntry) | ||
// When TrimTimestamp is enabled, make sure layout is compatible for "Sep 9", "Sep 9" , "Sep 09", "Sep 09" options and log value is trimmed correctly | ||
fileConfig.TrimTimestamp = true | ||
logEntry = "Sep 9 02:00:43 ip-10-4-213-132 \n" | ||
trimmedTimestampString := " ip-10-4-213-132 \n" | ||
timestamp, modifiedLogEntry = fileConfig.timestampFromLogLine(logEntry) | ||
assert.Equal(t, 02, timestamp.Hour()) | ||
assert.Equal(t, 00, timestamp.Minute()) | ||
assert.Equal(t, trimmedTimestampString, modifiedLogEntry) | ||
|
||
logEntry = "Sep 9 02:00:43 ip-10-4-213-132 \n" | ||
timestamp, modifiedLogEntry = fileConfig.timestampFromLogLine(logEntry) | ||
assert.Equal(t, 02, timestamp.Hour()) | ||
assert.Equal(t, 00, timestamp.Minute()) | ||
assert.Equal(t, trimmedTimestampString, modifiedLogEntry) | ||
|
||
logEntry = "Sep 09 02:00:43 ip-10-4-213-132 \n" | ||
timestamp, modifiedLogEntry = fileConfig.timestampFromLogLine(logEntry) | ||
assert.Equal(t, 02, timestamp.Hour()) | ||
assert.Equal(t, 00, timestamp.Minute()) | ||
assert.Equal(t, trimmedTimestampString, modifiedLogEntry) | ||
|
||
logEntry = "Sep 09 02:00:43 ip-10-4-213-132 \n" | ||
timestamp, modifiedLogEntry = fileConfig.timestampFromLogLine(logEntry) | ||
assert.Equal(t, 02, timestamp.Hour()) | ||
assert.Equal(t, 00, timestamp.Minute()) | ||
assert.Equal(t, trimmedTimestampString, modifiedLogEntry) | ||
} | ||
|
||
func TestTimestampParserWithFracSeconds(t *testing.T) { | ||
|
@@ -222,15 +289,34 @@ func TestTimestampParserWithFracSeconds(t *testing.T) { | |
expectedTimestamp := time.Unix(1497882318, 234000000) | ||
timestampString := "19 Jun 2017 14:25:18,234088 UTC" | ||
logEntry := fmt.Sprintf("%s [INFO] This is a test message.", timestampString) | ||
timestamp := fileConfig.timestampFromLogLine(logEntry) | ||
timestamp, modifiedLogEntry := fileConfig.timestampFromLogLine(logEntry) | ||
assert.Equal(t, expectedTimestamp.UnixNano(), timestamp.UnixNano(), | ||
fmt.Sprintf("The timestampFromLogLine value %v is not the same as expected %v.", timestamp, expectedTimestamp)) | ||
assert.Equal(t, logEntry, modifiedLogEntry) | ||
|
||
// Test regex match for multiline, the first timestamp in multiline should be matched | ||
logEntry = fmt.Sprintf("%s [INFO] This is the first line.\n19 Jun 2017 14:25:19,123456 UTC [INFO] This is the second line.\n", timestampString) | ||
timestamp = fileConfig.timestampFromLogLine(logEntry) | ||
timestamp, modifiedLogEntry = fileConfig.timestampFromLogLine(logEntry) | ||
assert.Equal(t, expectedTimestamp.UnixNano(), timestamp.UnixNano(), | ||
fmt.Sprintf("The timestampFromLogLine value %v is not the same as expected %v.", timestamp, expectedTimestamp)) | ||
assert.Equal(t, logEntry, modifiedLogEntry) | ||
|
||
// Test TrimTimeStamp for single line | ||
fileConfig.TrimTimestamp = true | ||
logEntry = fmt.Sprintf("%s [INFO] This is a test message.", timestampString) | ||
trimmedTimestampString := " [INFO] This is a test message." | ||
timestamp, modifiedLogEntry = fileConfig.timestampFromLogLine(logEntry) | ||
assert.Equal(t, expectedTimestamp.UnixNano(), timestamp.UnixNano(), | ||
fmt.Sprintf("The timestampFromLogLine value %v is not the same as expected %v.", timestamp, expectedTimestamp)) | ||
assert.Equal(t, trimmedTimestampString, modifiedLogEntry) | ||
|
||
// Test TrimTimeStamp for multiline, the first timestamp in multiline should be matched | ||
logEntry = fmt.Sprintf("%s [INFO] This is the first line.\n19 Jun 2017 14:25:19,123456 UTC [INFO] This is the second line.\n", timestampString) | ||
trimmedTimestampString = " [INFO] This is the first line.\n19 Jun 2017 14:25:19,123456 UTC [INFO] This is the second line.\n" | ||
timestamp, modifiedLogEntry = fileConfig.timestampFromLogLine(logEntry) | ||
assert.Equal(t, expectedTimestamp.UnixNano(), timestamp.UnixNano(), | ||
fmt.Sprintf("The timestampFromLogLine value %v is not the same as expected %v.", timestamp, expectedTimestamp)) | ||
assert.Equal(t, trimmedTimestampString, modifiedLogEntry) | ||
} | ||
|
||
func TestNonAllowlistedTimezone(t *testing.T) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you explain the value of
index[0]
[index[1]
? What are they corresponding to?Reading the if cases above, it seems like the length can vary depending on the regex so want to make sure we are fine with always calling index 0 and 1.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Regex will always write down the index array with index[0] is the start position of the timestamp match
index[1] is the end position of the timestamp match
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we also trim any leading whitespace after the timestamp trim?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For a point against doing this by default, the OTEL filelog receiver has
preserve_(leading|trailing)_whitespace
options in their config.