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

Add cross-fork logger for runtime-handler #459

Merged
merged 2 commits into from
Mar 26, 2024
Merged

Add cross-fork logger for runtime-handler #459

merged 2 commits into from
Mar 26, 2024

Conversation

dkundel
Copy link
Member

@dkundel dkundel commented Feb 2, 2023

For every invocation of a local Function we spin up a new forked process. The logger instance was passed across the fork but wasn't appropriately serialized/deserialized. As a result you got cryptic error messages like the one in #458 when the process was trying to write to the logger. I changed that by implementing a cross fork logger that has the same interface as the regular instance but then passes the data back via process.send the same way that the fork was already communicating with the main thread basically as an RPC to trigger the original logger instance.

Contributing to Twilio

All third-party contributors acknowledge that any contributions they provide will be made under the same open-source license that the open-source project is provided under.

  • I acknowledge that all my contributions will be made under the project's license.

@changeset-bot
Copy link

changeset-bot bot commented Feb 2, 2023

🦋 Changeset detected

Latest commit: c82f7e8

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@twilio/runtime-handler Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

typeof config.logger[crossForkLogMessage.level] === 'function'
) {
config.logger[crossForkLogMessage.level](
// @ts-ignore
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 not too happy about this but typing here would have been quite the pain and in my opinion would distract from the fact that this is just a pass through.

Copy link

@classactcollin classactcollin left a comment

Choose a reason for hiding this comment

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

Looks good to me! Interesting to read and follow through what's going on here.

}: {
err?: Error | number | string;
reply?: Reply;
debugMessage?: string;
debugArgs?: any[];
crossForkLogMessage?: {
level: keyof LoggerInstance;
args: [string] | [string, number] | [string, string];

Choose a reason for hiding this comment

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

I can't imagine these ever changing, but does it make sense to colocation the potential args shapes with the LoggerInstance type? In case the logger instance type is used similarly elsewhere.

Copy link
Contributor

Choose a reason for hiding this comment

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

args in the sendLog has the type (string | number)[] . This was cleaner to read.

process.send({
crossForkLogMessage: {
level,
args: args,

Choose a reason for hiding this comment

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

Suggested change
args: args,
args,

Comment on lines +321 to +326

if (crossForkLogMessage) {
if (
config.logger &&
typeof config.logger[crossForkLogMessage.level] === 'function'
) {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: crossForkLogMessage && config.logger && typeof config.logger[crossForkLogMessage.level] === 'function'

will typeof config.logger[crossForkLogMessage.level] ever return something other than a function?

Comment on lines +22 to +24
log(msg: string, level: number) {
this.sendLog('log', msg, level);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

curious, when do we use a log level? Also, what does level represent here?

@victoray victoray merged commit 7bebf6f into main Mar 26, 2024
10 checks passed
@victoray victoray deleted the dkundel/fix-458 branch March 26, 2024 07:52
@github-actions github-actions bot mentioned this pull request Mar 26, 2024
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.

6 participants