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

filebeat/input/journald: allow specifying since when to read journald entries #35408

Merged
merged 11 commits into from
Jul 27, 2023

Conversation

efd6
Copy link
Contributor

@efd6 efd6 commented May 10, 2023

What does this PR do?

This allows users to specify a relative time offset for starting to read journald entries, or as a fallback for cursor restarts.

Why is it important?

This is required for an integration enhancement.

Checklist

  • My code follows the style guidelines of this project
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I have made corresponding change to the default configuration files
  • I have added tests that prove my fix is effective or that my feature works
  • I have added an entry in CHANGELOG.next.asciidoc or CHANGELOG-developer.next.asciidoc.

Author's Checklist

  • [ ]

How to test this PR locally

Related issues

Use cases

Screenshots

Logs

@efd6 efd6 added enhancement Filebeat Filebeat Team:Security-External Integrations backport-skip Skip notification from the automated backport with mergify 8.8-candidate labels May 10, 2023
@efd6 efd6 self-assigned this May 10, 2023
@botelastic botelastic bot added needs_team Indicates that the issue/PR needs a Team:* label and removed needs_team Indicates that the issue/PR needs a Team:* label labels May 10, 2023
@efd6 efd6 force-pushed the i6060-journald branch 2 times, most recently from 2226034 to 151f14a Compare May 10, 2023 02:08
@elasticmachine
Copy link
Collaborator

elasticmachine commented May 10, 2023

💚 Build Succeeded

the below badges are clickable and redirect to their specific view in the CI or DOCS
Pipeline View Test View Changes Artifacts preview preview

Expand to view the summary

Build stats

  • Start Time: 2023-07-27T17:48:13.322+0000

  • Duration: 74 min 19 sec

Test stats 🧪

Test Results
Failed 0
Passed 8090
Skipped 757
Total 8847

💚 Flaky test report

Tests succeeded.

🤖 GitHub comments

Expand to view the GitHub comments

To re-run your PR in the CI, just comment with:

  • /test : Re-trigger the build.

  • /package : Generate the packages and run the E2E tests.

  • /beats-tester : Run the installation tests with beats-tester.

  • run elasticsearch-ci/docs : Re-trigger the docs validation. (use unformatted text in the comment!)

@efd6 efd6 force-pushed the i6060-journald branch 4 times, most recently from 26c3686 to a0d669b Compare May 10, 2023 05:33
@efd6 efd6 marked this pull request as ready for review May 10, 2023 07:23
@efd6 efd6 requested review from a team as code owners May 10, 2023 07:23
@elasticmachine
Copy link
Collaborator

Pinging @elastic/security-external-integrations (Team:Security-External Integrations)

@mergify

This comment was marked as outdated.

filebeat/input/journald/pkg/journalread/reader.go Outdated Show resolved Hide resolved
filebeat/input/journald/config.go Outdated Show resolved Hide resolved
filebeat/docs/inputs/input-journald.asciidoc Outdated Show resolved Hide resolved
filebeat/input/journald/pkg/journalread/reader.go Outdated Show resolved Hide resolved
@mergify

This comment was marked as outdated.

@efd6 efd6 force-pushed the i6060-journald branch 2 times, most recently from fb104e2 to 0d44d80 Compare May 11, 2023 00:10
@mergify

This comment was marked as outdated.

Copy link
Member

@rdner rdner left a comment

Choose a reason for hiding this comment

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

Do we have existing integration/e2e tests working with journald? If so we need to update them in order to test this new option. If not, we need to file an issue to add them ASAP.

Comment on lines 127 to 136
func sameError(a, b error) bool {
switch {
case a == nil && b == nil:
return true
case a == nil, b == nil:
return false
default:
return a.Error() == b.Error()
}
}
Copy link
Member

Choose a reason for hiding this comment

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

Why not require.ErrorIs?

Copy link
Contributor Author

@efd6 efd6 May 26, 2023

Choose a reason for hiding this comment

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

I avoid assertion libraries for the reasons stated in the Go wiki.

Copy link
Member

Choose a reason for hiding this comment

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

@efd6 this is a very strange statement to make since our entire codebase is using the testify library and it makes our tests more concise. Even this file is importing both assert and require and they're used in another test.

I've read the reasons stated in the wiki and I think it's coming from misunderstanding how testify works:

  • assert does not stop the test and lets it run further but fails it at the end of the run
  • require fails the test immediately and does not run further.

so, developers are free to choose the behaviour when necessary.

Copy link
Contributor Author

@efd6 efd6 May 26, 2023

Choose a reason for hiding this comment

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

The final part is the most sigificant:

Assert libraries make it too easy to write imprecise tests and inevitably end up duplicating features already in the language, like expression evaluation, comparisons, sometimes even more. Strive to write tests that are precise both about what went wrong and what went right, and make use of Go itself instead of creating a mini-language inside Go.

My experience with assert libraries, particularly testify, is that they make test failure outputs needlessly complex as the cost for the concision that is gained. It's important that comprehensibility, rather than concision is the goal for test code. Test should should really be completely transparent, concise code is often not this is an excellent decades-long study in that. The other issue is that as the quoted text above says, they make it easy for authors to write imprecise tests; this often presents as a test which says nothing other than where the failure happened and what the comparands were. Assert libraries usually (testify does) provide a format/args list to decorate failures with; in my experience, these are almost never used because the author favours the proximate terness and concision over the long term clarity that a well though out test provides.

Copy link
Member

Choose a reason for hiding this comment

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

That still sounds like a personal preference and not argumentation. The whole code base is using testify, the decision was made and the new tests should maintain consistency, thank you.

filebeat/input/journald/config.go Show resolved Hide resolved
filebeat/input/journald/pkg/journalread/reader.go Outdated Show resolved Hide resolved
filebeat/input/journald/config_test.go Outdated Show resolved Hide resolved
Copy link
Contributor

@belimawr belimawr left a comment

Choose a reason for hiding this comment

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

The only last thing I'm missing is a test to ensure the feature works, especially that the pointer deference does not panic.
There is a test on https://github.com/elastic/beats/blob/main/filebeat/input/journald/input_test.go that you can use as a start point.

filebeat/input/journald/input.go Outdated Show resolved Hide resolved
filebeat/input/journald/pkg/journalread/reader.go Outdated Show resolved Hide resolved
Copy link
Contributor

@belimawr belimawr left a comment

Choose a reason for hiding this comment

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

The new changes look good, thanks!

However I'm still missing a test that actually tests the functionality of using the Since value when reading the journal. Could you add this last one, please?

@efd6
Copy link
Contributor Author

efd6 commented May 31, 2023

Aren't we then testing the behaviour of journald which was rejected here?

@belimawr
Copy link
Contributor

belimawr commented Jun 8, 2023

Aren't we then testing the behaviour of journald which was rejected here?

They're two different things, at least the way I see them.

#35408 (comment) was a general comment about the ideal way of testing our code.

This one is about having some sort of tests that actually run the implemented code in the input (the run function).

It is about having some test ensuring the added code does not break anything, ideally it would not rely on journald, but if it does, it's also ok. We can always improve add more tests later, but it is important to have some tests actually running the implemented code.

@efd6
Copy link
Contributor Author

efd6 commented Jun 8, 2023

From what I can see it is not possible to test Run without involving journald. This is because the only way we can get to the path here is after calling journald.open which returns a real sdjournal value that we cannot mock. What this test would tell us is that a call to the SeekRealtimeUsec method (indirected via the interface in that package) does what the sdjournal docs and tests say it does since we already know from the tests that I've added here that it is not possible to reach a state where inp.Since is nil and the mode returned by seekBy is journalread.SeekSince, so we cannot cause a nil-deref of inp.Since. Whether there are issues in the use (*sdjournal.Journal).SeekRealtimeUsec? We can see that from the testing that that package does.

@mergify
Copy link
Contributor

mergify bot commented Jun 8, 2023

This pull request is now in conflicts. Could you fix it? 🙏
To fixup this pull request, you can check out it locally. See documentation: https://help.github.com/articles/checking-out-pull-requests-locally/

git fetch upstream
git checkout -b i6060-journald upstream/i6060-journald
git merge upstream/main
git push upstream i6060-journald

@andrewkroh
Copy link
Member

However I'm still missing a test that actually tests the functionality of using the Since value when reading the journal. Could you add this last one, please?

@belimawr I pushed in two test cases to verify that seek: since and cursor_seek_fallback: since are working. Please take another look.

CHANGELOG.next.asciidoc Outdated Show resolved Hide resolved
@efd6 efd6 merged commit 739e381 into elastic:main Jul 27, 2023
8 checks passed
Scholar-Li pushed a commit to Scholar-Li/beats that referenced this pull request Feb 5, 2024
…d entries (elastic#35408)

Co-authored-by: Andrew Kroh <andrew.kroh@elastic.co>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
8.10-candidate backport-skip Skip notification from the automated backport with mergify enhancement Filebeat Filebeat
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants