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

[Response Ops][Alerting] Refactor ExecutionHandler stage 2 #193807

Merged
merged 7 commits into from
Oct 9, 2024

Conversation

ymao1
Copy link
Contributor

@ymao1 ymao1 commented Sep 24, 2024

Resolves #186534

Summary

This PR splits the for-loop in the ActionScheduler.run function into the appropriate scheduler classes. Previously, each scheduler had a generateExecutables function that would return an array of executables and the ActionScheduler would loop through the array and convert the executable to a scheduleable action depending on whether it was a per-alert action, summary action or system action. This refactor renames generateExecutables into getActionsToSchedule and moves the logic to convert the executables into a schedulable action into the appropriate scheduler class.

To Verify

Create some rules with per-alert and summary and system actions and verify they are triggered as expected.

@ymao1 ymao1 force-pushed the refactor-execution-handler-stage-2 branch from a79dbed to d3e831f Compare September 26, 2024 16:37
delete logActions[r.id];
const uuid = r.uuid;
if (uuid) {
actionsToNotLog.push(uuid);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Previously, we were creating a logActions record where the key was the actionId and the value was the information to pass to the event log in order to log the execute-action document. After adding some more unit tests in the PerAlertActionScheduler, I noticed that if we have multiple alerts for the same action or multiple actions that use the same connector, storing this information by the action ID would only allow one event log document to be written, so I switched to passing and using the action UUID instead.

Copy link
Member

Choose a reason for hiding this comment

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

Not understanding why an action not having a uuid is treated differently here. Based on this, but mainly the assignment of actionsToLog below, it seems like we won't log an event log doc for these?

Wondering if we could create a placeholder uuid when the action doesn't have one, just for the action scheduling bits here, so we wouldn't have to worry about it potentially being empty. So I guess in at least ExecutionResponseItem and maybe ExecuteOptions, uuid wouldn't be optional.

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 created an issue about the optional typing and added comments about the optional typing here: 32beee0. I can add a placeholder UUID if you think that's better but I think that might cause more confusion in the future

Copy link
Member

Choose a reason for hiding this comment

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

the comment is fine, no placeholder needed, thx!

@ymao1 ymao1 changed the title Initial commit refactoring stuff [Response Ops][Alerting] Refactor ExecutionHandler stage 2 Sep 27, 2024
@ymao1 ymao1 self-assigned this Sep 27, 2024
@ymao1 ymao1 added release_note:skip Skip the PR/issue when compiling release notes v9.0.0 v8.16.0 Feature:Alerting Team:ResponseOps Label for the ResponseOps team (formerly the Cases and Alerting teams) backport:prev-minor Backport to (8.x) the previous minor version (i.e. one version back from main) labels Sep 27, 2024
@ymao1 ymao1 marked this pull request as ready for review September 27, 2024 13:43
@ymao1 ymao1 requested a review from a team as a code owner September 27, 2024 13:43
@elasticmachine
Copy link
Contributor

Pinging @elastic/response-ops (Team:ResponseOps)

@ymao1 ymao1 requested review from pmuellr and guskovaue September 30, 2024 13:42
@ymao1
Copy link
Contributor Author

ymao1 commented Oct 1, 2024

@elasticmachine merge upstream

@ymao1
Copy link
Contributor Author

ymao1 commented Oct 3, 2024

@elasticmachine merge upstream

@@ -31,6 +31,7 @@ interface CreateExecuteFunctionOptions {
export interface ExecuteOptions
extends Pick<ActionExecutorOptions, 'params' | 'source' | 'relatedSavedObjects' | 'consumer'> {
id: string;
uuid?: string;
Copy link
Member

Choose a reason for hiding this comment

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

Curious why this is optional. I think it's because for really old rules that haven't been updated since we added the uuid to the actions, it won't be there, and we don't have a migration. But double-checking in case it's BWC or something.

Just from a quick scan of the code, it doesn't seem like we added many tests in this PR that would have a non-existing uuid. Maybe some of the tests already deal with this, that I wouldn't have noticed?

I see one reference to a uuid!, which was just refactor/move from old code - but ... scares me :-). As I take a closer look at this PR, maybe I'll figure out it's ok.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is typed as optional even though it should always exist on the action :(. I think because we use a single schema in the API and the persistence layer and we don't expect the UUID to be passed into the API but we create one before it reaches the persistence layer.

Copy link
Member

Choose a reason for hiding this comment

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

I found the PR where we added the uuid. For some reason I thought we hadn't done a migration to add to old rules, but we did! Add uuid to rule actions. My fear was we didn't, so an upgrade from an old version wouldn't have uuids, which got me wondering about the cases where you check it.

Seems like something we should fix. Open an issue? No need to fix in this PR (and may be nasty, presumably another type to deal with!).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Created an issue: #195255

@ymao1 ymao1 requested a review from pmuellr October 7, 2024 13:26
@kibana-ci
Copy link
Collaborator

💛 Build succeeded, but was flaky

Failed CI Steps

Metrics [docs]

✅ unchanged

History

To update your PR or re-run it, just comment with:
@elasticmachine merge upstream

cc @ymao1

@ymao1
Copy link
Contributor Author

ymao1 commented Oct 9, 2024

@elasticmachine merge upstream

Copy link
Member

@pmuellr pmuellr left a comment

Choose a reason for hiding this comment

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

LGTM, whew!

@@ -3176,7 +3196,7 @@ describe('Task Runner', () => {
status: 'warning',
errorReason: `maxExecutableActions`,
logAlert: 4,
logAction: 3,
logAction: 5,
Copy link
Member

Choose a reason for hiding this comment

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

curious if this change indicates we were dropping some EL docs when we weren't using the UUIDs ...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, that's correct. If there were N actions configured in a rule with the same connector, then only one execute-action event log doc per alert was written instead of N

@ymao1
Copy link
Contributor Author

ymao1 commented Oct 9, 2024

@elasticmachine merge upstream

@elasticmachine
Copy link
Contributor

💚 Build Succeeded

Metrics [docs]

✅ unchanged

History

cc @ymao1

@ymao1 ymao1 merged commit 9221ab1 into elastic:main Oct 9, 2024
37 checks passed
@ymao1 ymao1 deleted the refactor-execution-handler-stage-2 branch October 9, 2024 21:01
@kibanamachine
Copy link
Contributor

Starting backport for target branches: 8.x

https://github.com/elastic/kibana/actions/runs/11262993933

kibanamachine pushed a commit to kibanamachine/kibana that referenced this pull request Oct 9, 2024
…#193807)

Resolves elastic#186534

## Summary

This PR splits the for-loop in the `ActionScheduler.run` function into
the appropriate scheduler classes. Previously, each scheduler had a
`generateExecutables` function that would return an array of executables
and the `ActionScheduler` would loop through the array and convert the
executable to a scheduleable action depending on whether it was a
per-alert action, summary action or system action. This refactor renames
`generateExecutables` into `getActionsToSchedule` and moves the logic to
convert the executables into a schedulable action into the appropriate
scheduler class.

## To Verify

Create some rules with per-alert and summary and system actions and
verify they are triggered as expected.

---------

Co-authored-by: Elastic Machine <[email protected]>
(cherry picked from commit 9221ab1)
@kibanamachine
Copy link
Contributor

💚 All backports created successfully

Status Branch Result
8.x

Note: Successful backport PRs will be merged automatically after passing CI.

Questions ?

Please refer to the Backport tool documentation

kibanamachine added a commit that referenced this pull request Oct 9, 2024
…stage 2 (#193807) (#195676)

# Backport

This will backport the following commits from `main` to `8.x`:
- [[Response Ops][Alerting] Refactor &#x60;ExecutionHandler&#x60; stage
2 (#193807)](#193807)

<!--- Backport version: 9.4.3 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Ying
Mao","email":"[email protected]"},"sourceCommit":{"committedDate":"2024-10-09T21:01:16Z","message":"[Response
Ops][Alerting] Refactor `ExecutionHandler` stage 2 (#193807)\n\nResolves
https://github.com/elastic/kibana/issues/186534\r\n\r\n##
Summary\r\n\r\nThis PR splits the for-loop in the `ActionScheduler.run`
function into\r\nthe appropriate scheduler classes. Previously, each
scheduler had a\r\n`generateExecutables` function that would return an
array of executables\r\nand the `ActionScheduler` would loop through the
array and convert the\r\nexecutable to a scheduleable action depending
on whether it was a\r\nper-alert action, summary action or system
action. This refactor renames\r\n`generateExecutables` into
`getActionsToSchedule` and moves the logic to\r\nconvert the executables
into a schedulable action into the appropriate\r\nscheduler
class.\r\n\r\n## To Verify\r\n\r\nCreate some rules with per-alert and
summary and system actions and\r\nverify they are triggered as
expected.\r\n\r\n---------\r\n\r\nCo-authored-by: Elastic Machine
<[email protected]>","sha":"9221ab19e86ca7d3215205110fc709f7ba4739af","branchLabelMapping":{"^v9.0.0$":"main","^v8.16.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["Feature:Alerting","release_note:skip","Team:ResponseOps","v9.0.0","backport:prev-minor","v8.16.0"],"title":"[Response
Ops][Alerting] Refactor `ExecutionHandler` stage
2","number":193807,"url":"https://github.com/elastic/kibana/pull/193807","mergeCommit":{"message":"[Response
Ops][Alerting] Refactor `ExecutionHandler` stage 2 (#193807)\n\nResolves
https://github.com/elastic/kibana/issues/186534\r\n\r\n##
Summary\r\n\r\nThis PR splits the for-loop in the `ActionScheduler.run`
function into\r\nthe appropriate scheduler classes. Previously, each
scheduler had a\r\n`generateExecutables` function that would return an
array of executables\r\nand the `ActionScheduler` would loop through the
array and convert the\r\nexecutable to a scheduleable action depending
on whether it was a\r\nper-alert action, summary action or system
action. This refactor renames\r\n`generateExecutables` into
`getActionsToSchedule` and moves the logic to\r\nconvert the executables
into a schedulable action into the appropriate\r\nscheduler
class.\r\n\r\n## To Verify\r\n\r\nCreate some rules with per-alert and
summary and system actions and\r\nverify they are triggered as
expected.\r\n\r\n---------\r\n\r\nCo-authored-by: Elastic Machine
<[email protected]>","sha":"9221ab19e86ca7d3215205110fc709f7ba4739af"}},"sourceBranch":"main","suggestedTargetBranches":["8.x"],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","branchLabelMappingKey":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/193807","number":193807,"mergeCommit":{"message":"[Response
Ops][Alerting] Refactor `ExecutionHandler` stage 2 (#193807)\n\nResolves
https://github.com/elastic/kibana/issues/186534\r\n\r\n##
Summary\r\n\r\nThis PR splits the for-loop in the `ActionScheduler.run`
function into\r\nthe appropriate scheduler classes. Previously, each
scheduler had a\r\n`generateExecutables` function that would return an
array of executables\r\nand the `ActionScheduler` would loop through the
array and convert the\r\nexecutable to a scheduleable action depending
on whether it was a\r\nper-alert action, summary action or system
action. This refactor renames\r\n`generateExecutables` into
`getActionsToSchedule` and moves the logic to\r\nconvert the executables
into a schedulable action into the appropriate\r\nscheduler
class.\r\n\r\n## To Verify\r\n\r\nCreate some rules with per-alert and
summary and system actions and\r\nverify they are triggered as
expected.\r\n\r\n---------\r\n\r\nCo-authored-by: Elastic Machine
<[email protected]>","sha":"9221ab19e86ca7d3215205110fc709f7ba4739af"}},{"branch":"8.x","label":"v8.16.0","branchLabelMappingKey":"^v8.16.0$","isSourceBranch":false,"state":"NOT_CREATED"}]}]
BACKPORT-->

Co-authored-by: Ying Mao <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport:prev-minor Backport to (8.x) the previous minor version (i.e. one version back from main) Feature:Alerting release_note:skip Skip the PR/issue when compiling release notes Team:ResponseOps Label for the ResponseOps team (formerly the Cases and Alerting teams) v8.16.0 v9.0.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Response Ops][Alerting] Refactor ExecutionHandler Pt 2
5 participants