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

feat(api): added full trigger validation in api #892

Conversation

lordvidex
Copy link

PR Summary

Trigger is fully validated using expr when trigger is created or updated.

Problem solved

It was possible for govalute to skip validations for expressions that do not fall into the current conditional branch based on the target values.
For example, t1 > 10 ? ok : (t2 * 10 : error ? warn) would return result without error where t1 > 10 without considering the syntax error (misplacement of ternary operators) in the expression.

Notes

Commit 7bb6f5a removes expression optimization because the vm.Program in expr caches the result of Get() function which may produce wrong result in expr.Run function. Check benchmark and commit description for more.

Possible problems

  • expr is not fully compatible with govalute
    • expr supports functions
    • expr uses [a, b, c,...] while govalute uses (a,b,c,...) for array literals

Because of the following errors, expr might mark an expression as valid where govalute will return errors and vice versa.

Relations

Augments #881

- optimizing get calls can make vm.Program return wrong values because
  variables have been replaced before caching, and new set `WARN_VALUE`,
  `t1, t2, ...` are not evaluated properly.
- If validation of variables is not necessary, this commit can be
  reverted for speed purpose
@lordvidex lordvidex requested a review from a team as a code owner August 7, 2023 08:01
@lordvidex lordvidex changed the title feat(api): added full trigger validation when created feat(api): added full trigger validation in api Aug 7, 2023
expression/expression.go Outdated Show resolved Hide resolved
expression/expression.go Outdated Show resolved Hide resolved
expression/expression.go Outdated Show resolved Hide resolved
expression/expression_test.go Outdated Show resolved Hide resolved
expression/expression_test.go Outdated Show resolved Hide resolved
expression/expression_test.go Outdated Show resolved Hide resolved
@codecov-commenter
Copy link

codecov-commenter commented Aug 8, 2023

Codecov Report

Merging #892 (b5c3cbf) into master (f29ce12) will increase coverage by 1.48%.
Report is 2 commits behind head on master.
The diff coverage is 89.28%.

❗ Current head b5c3cbf differs from pull request most recent head c302f68. Consider uploading reports for the commit c302f68 to get more accurate results

❗ Your organization is not using the GitHub App Integration. As a result you may experience degraded service beginning May 15th. Please install the Github App Integration for your organization. Read more.

@@            Coverage Diff             @@
##           master     #892      +/-   ##
==========================================
+ Coverage   68.28%   69.76%   +1.48%     
==========================================
  Files         187      187              
  Lines       10453    10439      -14     
==========================================
+ Hits         7138     7283     +145     
+ Misses       2906     2736     -170     
- Partials      409      420      +11     
Files Changed Coverage Δ
api/dto/triggers.go 61.61% <0.00%> (ø)
database/redis/database.go 85.71% <71.42%> (+4.94%) ⬆️
api/handler/trigger_render.go 64.17% <77.77%> (+64.17%) ⬆️
expression/expression.go 83.96% <85.18%> (-1.27%) ⬇️
api/handler/notification.go 56.41% <100.00%> (+56.41%) ⬆️
api/handler/trigger_metrics.go 19.44% <100.00%> (+19.44%) ⬆️
api/middleware/context.go 45.03% <100.00%> (+45.03%) ⬆️
database/redis/contact.go 90.90% <100.00%> (+7.01%) ⬆️
database/redis/last_check.go 88.28% <100.00%> (+6.68%) ⬆️
database/redis/metric.go 80.27% <100.00%> (+8.72%) ⬆️

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@@ -26,19 +31,52 @@ func BenchmarkDefault2Expr(b *testing.B) {
MainTargetValue: 10.0,
WarnValue: &warnValue,
ErrorValue: &errorValue,
TriggerType: moira.RisingTrigger,
Copy link
Member

@kissken kissken Aug 8, 2023

Choose a reason for hiding this comment

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

hmmm, why it becomes necessary?

Copy link
Author

@lordvidex lordvidex Aug 8, 2023

Choose a reason for hiding this comment

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

I debugged the benchmark and it was actually returning error "trigger_type is not set", therefore the tests were not really testing the speed of expression parsing.

Copy link
Member

Choose a reason for hiding this comment

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

👀

Copy link
Author

Choose a reason for hiding this comment

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

I think this issue can be resolved already. @kissken ?

expression/expression.go Outdated Show resolved Hide resolved
expression/expression.go Outdated Show resolved Hide resolved
This is necessary because govalute is used to verify expression
  evaluation and `expr.Compile` is enough to validate expression.
api/dto/triggers.go Show resolved Hide resolved
expression/expression.go Show resolved Hide resolved
expression/expression.go Outdated Show resolved Hide resolved
expression/expression.go Outdated Show resolved Hide resolved
expression/expression.go Outdated Show resolved Hide resolved
@kissken
Copy link
Member

kissken commented Aug 14, 2023

/build

@github-actions
Copy link

Build and push Docker images with tag: feature-expression-validation.2023-08-14.c302f68

1 similar comment
@github-actions
Copy link

Build and push Docker images with tag: feature-expression-validation.2023-08-14.c302f68

@Tetrergeru Tetrergeru changed the base branch from master to feature/expression-validation November 3, 2023 09:23
@Tetrergeru Tetrergeru merged commit aee9269 into moira-alert:feature/expression-validation Nov 7, 2023
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.

5 participants