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(core): Move all execution lifecycle telemetry events to lifecycle hooks (no-changelog) #12816

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

netroy
Copy link
Member

@netroy netroy commented Jan 23, 2025

Summary

This PR moves telemetry events out of workflow-execute-additional-data and WorkflowRunner, and into execution lifecycle hooks, to make these temetry hooks consistent across various modes.

Review / Merge checklist

  • PR title and summary are descriptive. (conventions)
  • Docs updated or follow-up ticket created.
  • Tests included.
  • PR Labeled with release/backport (if the PR is an urgent fix that needs to be backported)

Copy link

codecov bot commented Jan 23, 2025

Codecov Report

Attention: Patch coverage is 90.90909% with 4 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
...c/execution-lifecycle/execution-lifecycle-hooks.ts 90.90% 3 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

@n8n-assistant n8n-assistant bot added core Enhancement outside /nodes-base and /editor-ui n8n team Authored by the n8n team labels Jan 23, 2025
@netroy netroy requested a review from ivov January 23, 2025 16:00
Copy link
Contributor

@ivov ivov left a comment

Choose a reason for hiding this comment

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

I admire your patience for these hooks 🙏🏻

Comment on lines +48 to +50
if (hooks.hasOwnProperty(key)) {
result[key]!.push(...hooks[key]!);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

  1. Do we need the hasOwnProperty check for a key coming from in?
  2. Can we avoid the assertions? Maybe like this?
if (!result[key] || !hooks[key]) continue;

Copy link
Member Author

Choose a reason for hiding this comment

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

  1. no. type safety should take care of that.
  2. sure

workflowExecuteBefore: [
async function (this: WorkflowHooks): Promise<void> {
const { executionId, workflowData } = this;
eventService.emit('workflow-pre-execute', { executionId, data: workflowData });
Copy link
Contributor

Choose a reason for hiding this comment

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

Odd that workflow-pre-execute doesn't need user ID but workflow-post-execute does.

Copy link
Member Author

Choose a reason for hiding this comment

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

yeah, there are still a couple of inconsistencies. I'd like to change this in another PR though.

},
],
workflowExecuteAfter: [
async function (this: WorkflowHooks, runData: IRun): Promise<void> {
Copy link
Contributor

Choose a reason for hiding this comment

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

What's our policy on inferrable return types? Personally I find them noisy.

Copy link
Member Author

Choose a reason for hiding this comment

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

quite a lot of explicit typing in this file will be removed after I refactor WorkflowHooks to be type-safe.

};
}

function hookFunctionsNodeTelemetry(): IWorkflowExecuteHooks {
Copy link
Contributor

Choose a reason for hiding this comment

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

telemetry is inaccurate because more things happen in reaction to these events, e.g. log streaming. hookFunctionsNodeEvents?

Comment on lines +78 to +88
async function (this: WorkflowHooks, nodeName: string): Promise<void> {
const { executionId, workflowData: workflow } = this;
eventService.emit('node-pre-execute', { executionId, workflow, nodeName });
},
],
nodeExecuteAfter: [
async function (this: WorkflowHooks, nodeName: string): Promise<void> {
const { executionId, workflowData: workflow } = this;
eventService.emit('node-post-execute', { executionId, workflow, nodeName });
},
],
Copy link
Contributor

Choose a reason for hiding this comment

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

Do these need to be async? Same elsewhere.

Copy link
Member Author

Choose a reason for hiding this comment

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

the async is coming from the typing. I'll change this in the WorkflowHooks refactor.

Comment on lines +472 to +473
hookFunctionsWorkflowTelemetry(userId),
hookFunctionsNodeTelemetry(),
Copy link
Contributor

Choose a reason for hiding this comment

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

Now that these hooks are no longer in WorkflowRunner and in WorkflowExecuteAdditionalData, can it happen that events reach listeners in the wrong sequence? e.g. log streaming writing a node-execute-before event before writing a workflow-execute-before event. Is that a risk?

Copy link
Member Author

Choose a reason for hiding this comment

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

that was what I was concerned about when I originally started this refactor in #12364 .
I've since tested this a bunch already, and haven't seen any issues so far.
also, the order of these was already inconsistent between various modes.
The only thing that I can tell is affected by these bring reordered is the final status determination. but to address that perhaps we should do that before any other workflowExecuteAfter hook in every mode. WDYT?

}

const hookFunctions = mergeHookFunctions(...toMerge);
Copy link
Contributor

Choose a reason for hiding this comment

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

mergeHookFunctions should accept an array or a variadic argument?

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm fine either way. do you prefer one over the other?


// TODO: why are workers pushing to frontend?
// TODO: simplifying this for now to just leave the bare minimum hooks
// hookFunctions.push(hookFunctionsPush());
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we delete those stale TODOs while we're at it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Enhancement outside /nodes-base and /editor-ui n8n team Authored by the n8n team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants