Skip to content
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

Refactor log-validator to break up the big main function. #7170

Merged
merged 3 commits into from
Nov 27, 2023

Conversation

mcpherrinm
Copy link
Contributor

The main function in log-validator is overly big, so this refactors it in preparation for adding support for file globs, which is started in the (currently draft) #7134

This PR should be functionally a no-op, except for one change:
The 1-per-second ratelimiter is moved to be per-file, so it's 1-per-second-per-file. I think this more closely aligns with what we'd want, as we could potentially miss that a file had bad lines in it if it was overwhelmed by another log file at the same time.

The main function in log-validator is overly big, so this refactors it in
preparation for adding support for file globs.

Largely it's functionally identical, except for one change:
The 1-per-second ratelimiter is moved to be per-file, so it's
1-per-second-per-file.  I think this more closely aligns with what we'd want,
as we could potentially miss that a file had bad lines in it if it was
overwhelmed by another log file at the same time.
@mcpherrinm mcpherrinm requested a review from a team as a code owner November 22, 2023 21:10
log/validator/validator.go Show resolved Hide resolved

// ValidateFile validates a single file and returns
func ValidateFile(filename string) error {
file, err := os.ReadFile(filename)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider reading the file line-by-line instead of loading it all into memory:

file, err := os.Open(filename)
if err != nil {
    return err
}
defer file.Close()

scanner := bufio.NewScanner(file)
badFile := false
lineNum := 0

for scanner.Scan() {
    lineNum++
    line := scanner.Text()
...

Copy link
Member

@beautifulentropy beautifulentropy Nov 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I almost forgot, the scanner has one part that's a little wonky:

// Check for any errors that occurred during the scan.
if err := scanner.Err()
if err != nil {
    return fmt.Errorf("error reading file: %w", err)
}

You'll want to do this after you've finished scanning a file, not inside of the loop.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually have an idea to just get rid of this function completely: it’s not used at all during the regular operation of log-validator so is extra code duplication, but I didn’t want to make too many changes while moving things around. This function is currently unmodified from the source. So I’d like to just leave it as-is for now and eliminate it in the next patch in this series.

jsha
jsha previously approved these changes Nov 22, 2023
Copy link
Contributor

@jsha jsha left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me, and I like doing splitups separately from substantive changes.

@beautifulentropy beautifulentropy merged commit 70a6b10 into main Nov 27, 2023
@beautifulentropy beautifulentropy deleted the mattm-refactor-log-validator branch November 27, 2023 18:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants