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][httpjson]- Added min & max functions to the template engine #36036

Merged
merged 14 commits into from
Jul 24, 2023

Conversation

ShourieG
Copy link
Contributor

@ShourieG ShourieG commented Jul 11, 2023

Type of change

  • Enhancement

What does this PR do?

Adds min, max methods to the template engine.

Why is it important?

This is important because it will help solve issues where min max methods are required to extract a certain value to be passed in the requests.

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

@ShourieG ShourieG requested a review from a team as a code owner July 11, 2023 10:59
@botelastic botelastic bot added the needs_team Indicates that the issue/PR needs a Team:* label label Jul 11, 2023
@mergify
Copy link
Contributor

mergify bot commented Jul 11, 2023

This pull request does not have a backport label.
If this is a bug or security fix, could you label this PR @ShourieG? 🙏.
For such, you'll need to label your PR with:

  • The upcoming major version of the Elastic Stack
  • The upcoming minor version of the Elastic Stack (if you're not pushing a breaking change)

To fixup this pull request, you need to add the backport labels for the needed
branches, such as:

  • backport-v8./d.0 is the label to automatically backport to the 8./d branch. /d is the digit

@elasticmachine
Copy link
Collaborator

elasticmachine commented Jul 11, 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-24T07:04:29.916+0000

  • Duration: 73 min 9 sec

Test stats 🧪

Test Results
Failed 0
Passed 3076
Skipped 178
Total 3254

💚 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!)

@ShourieG ShourieG added Team:Security-External Integrations and removed needs_team Indicates that the issue/PR needs a Team:* label labels Jul 11, 2023
@elasticmachine
Copy link
Collaborator

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

@ShourieG ShourieG changed the title added min/max functions [filebeat][httpjson]- Added min & max functions to the template engine Jul 11, 2023
Copy link
Member

@andrewkroh andrewkroh left a comment

Choose a reason for hiding this comment

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

FWIW consul-template (MPL-2.0) has a pretty complete implementation of min and max functions. sprig also has implementations.

x-pack/filebeat/input/httpjson/value_tpl.go Outdated Show resolved Hide resolved
@ShourieG
Copy link
Contributor Author

FWIW consul-template (MPL-2.0) has a pretty complete implementation of min and max functions. sprig also has implementations.

@andrewkroh made the min/max functions generic in a similar fashion.

@mergify
Copy link
Contributor

mergify bot commented Jul 14, 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 httpjson/add_min_max upstream/httpjson/add_min_max
git merge upstream/main
git push upstream httpjson/add_min_max

@ShourieG ShourieG requested a review from andrewkroh July 14, 2023 10:27
CHANGELOG.next.asciidoc Outdated Show resolved Hide resolved
x-pack/filebeat/input/httpjson/value_tpl.go Outdated Show resolved Hide resolved
x-pack/filebeat/input/httpjson/value_tpl.go Outdated Show resolved Hide resolved
x-pack/filebeat/input/httpjson/value_tpl.go Outdated Show resolved Hide resolved
@ShourieG
Copy link
Contributor Author

@efd6 I've reworked the min max functions to be more specific.

@ShourieG
Copy link
Contributor Author

ShourieG commented Jul 18, 2023

@efd6 made suggested changes.

@ShourieG ShourieG requested a review from efd6 July 19, 2023 08:07
@ShourieG
Copy link
Contributor Author

@efd6 if all looks ok, are we good for merging atm ?

Copy link
Member

@andrewkroh andrewkroh left a comment

Choose a reason for hiding this comment

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

We don't have a need to support lists of arguments. It adds complexity so let's simplify down to basic two-parameter functions.

The min and max functions should be shortcuts for something that can be expressed via the built-ins. The built-in comparator functions make sane comparisons between like types (see the lt implementation in text/template).

[[if lt .A .B ]][[ .A ]][[ else ]][[ .B ]] == [[min .A .B]]

[[if gt .A .B ]][[ .A ]][[ else ]][[ .B ]] == [[max .A .B]]

The existing numerical functions expect int64. We could continue that pattern, but as I pointed out in a previous comment this can be difficult to use correctly. It requires the users to know that only int64 values will be accepted. This is similar to creating type specific functions (e.g. minInt) because users need to know the types involved to select the correct function.

Dan raised a good concern about the some of the comparisons between types in consul-template implementation. I think we should still have a single min and max, but only allow comparisons between like types (as is handled in the builtin lt() func).

My proposal in 4cdc09c re-uses the lt function to create min and max. WDYT?

@ShourieG
Copy link
Contributor Author

ShourieG commented Jul 20, 2023

We don't have a need to support lists of arguments. It adds complexity so let's simplify down to basic two-parameter functions.

The min and max functions should be shortcuts for something that can be expressed via the built-ins. The built-in comparator functions make sane comparisons between like types (see the lt implementation in text/template).

[[if lt .A .B ]][[ .A ]][[ else ]][[ .B ]] == [[min .A .B]]

[[if gt .A .B ]][[ .A ]][[ else ]][[ .B ]] == [[max .A .B]]

The existing numerical functions expect int64. We could continue that pattern, but as I pointed out in a previous comment this can be difficult to use correctly. It requires the users to know that only int64 values will be accepted. This is similar to creating type specific functions (e.g. minInt) because users need to know the types involved to select the correct function.

Dan raised a good concern about the some of the comparisons between types in consul-template implementation. I think we should still have a single min and max, but only allow comparisons between like types (as is handled in the builtin lt() func).

My proposal in 4cdc09c re-uses the lt function to create min and max. WDYT?

@andrewkroh The simplification of down to using just 2 values instead of a list makes sense, but this approach again re-introduces the ability to compare values of different ranges and signs, I agree the user would need the knowledge of certain types to implement the current approach but shouldn't we expect the user to have that basic knowledge when using something like httpjson ? Also this is using reflection so the code becomes more complex, similar to the consul code(minor nit-pick). @efd6 What are your thoughts on this ?

@andrewkroh
Copy link
Member

andrewkroh commented Jul 20, 2023

but this approach again re-introduces the ability to compare values of different ranges and signs

There is a difference. lt only compares like kinds 1 (e.g. int to int, float to float). It makes an exception to allow comparing uint and int 2, and it only casts from int -> uint64 when the int kind's value is non-negative (which should be safe AFAICT). lt will not compare an int with a float32, for example.

Footnotes

  1. https://github.com/golang/go/blob/5fe3f0a265c90a9c0346403742c6cafeb154503b/src/text/template/funcs.go#L554-L561

  2. https://github.com/golang/go/blob/5fe3f0a265c90a9c0346403742c6cafeb154503b/src/text/template/funcs.go#L543-L546

@ShourieG
Copy link
Contributor Author

ShourieG commented Jul 20, 2023

@andrewkroh alright then, we can go ahead with this approach if everyone's onboard, waiting on you @efd6.

@efd6
Copy link
Contributor

efd6 commented Jul 23, 2023

I much prefer the simpler solution; the possibility of comparing different types is unfortunate, but it is at least consistent with the rest of the templating language in that it is merely an optimisation of an expression that can be made in-language, i.e. [[if lt .A .B ]][[ .A ]][[ else ]][[ .B ]].

@ShourieG
Copy link
Contributor Author

@andrewkroh I've merged your changes to this PR. @efd Could you review and approve ?

@ShourieG ShourieG merged commit 2429af1 into elastic:main Jul 24, 2023
@ShourieG ShourieG deleted the httpjson/add_min_max branch July 24, 2023 08:49
Scholar-Li pushed a commit to Scholar-Li/beats that referenced this pull request Feb 5, 2024
elastic#36036)

* added min/max functions

* updated methods to accept integer list

* updated the methods to accept atleast 1 arg

* extended the min/max functions to be more generic

* reworked the min/max implementation

* made the min max funcs generic with a custom number ingterface

* Implement min/max using text/template lt func

---------

Co-authored-by: Andrew Kroh <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[filebeat][httpjson] - Add min/max template functions
4 participants