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: Adds extension name as prefix when extension is using logger #872

Merged
merged 1 commit into from
Nov 26, 2022

Conversation

sfrunza13
Copy link

@sfrunza13 sfrunza13 commented Nov 20, 2022

What does this PR do?

I am aiming to create a wrapper for some of the logging so that we can have a prefix for each separate extension.

Screenshot/screencast of this PR

image

Something similar to this

What issues does this PR fix or reference?

#822

How to test this PR?

Check logs originating from extensions folder for the appropriate names

Signed-off-by: Stefan Frunza stefanfrunza@gmail.com

@sfrunza13
Copy link
Author

@benoitf I apologize as I am still relatively new to everything. This is where I am at right now, I thought it would be more productive to open a PR review and ask for some guidance on how to get it right rather than continue shooting in the dark as it were.

Am I on the right track?

@benoitf
Copy link
Collaborator

benoitf commented Nov 21, 2022

Helo @sfrunza13
So the overall idea is that extension does not care about the prefix. Extension should be 'dumb' and just use console.log messages.

It's inside the 'patched' console.log method that we should be 'smarter' and see if it's an extension that is calling us or not.
If yes, we prefix using the extension name, else we display the log as usual.

https://github.com/containers/podman-desktop/blob/df9172a9218366f26fad7b5ee7fb8d9933bba286/packages/main/src/plugin/index.ts#L125-L130

so probably, look at the stack trace, if stack is coming from an extension, grab the name
and we should have a map between the folder name and the extension name so we display the extension name

we probably need to retrieve some values from extensionLoader class (like the name of the root extension folder or all extension folders used

@sfrunza13
Copy link
Author

@benoitf how is this? What would you advise to improve it?

@benoitf
Copy link
Collaborator

benoitf commented Nov 21, 2022

@sfrunza13 I would not hard code the list of possible extensions

I would ask extensionLoader to grab the current root directory for extensions or the set of directories that are currently loaded

@benoitf
Copy link
Collaborator

benoitf commented Nov 21, 2022

but it looks very promising 🎉 thanks for looking at it

@benoitf
Copy link
Collaborator

benoitf commented Nov 21, 2022

And FYI, to get a stack, no need to throw and catch

const stack = new Error().stack

}

if(stack!=''){
var extensionGroupings = stack.match(/(podman\-desktop\\extensions)\\(lima|crc|docker|kube\-context|podman)\\(dist\\extension\.js)+:/)
Copy link
Collaborator

Choose a reason for hiding this comment

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

note that it could be other JavaScript object than the entrypoint and it's not forced to be named extension.js

So here probably we should just check if parent folder is part of an extension

@sfrunza13
Copy link
Author

Currently, I can think of 2 ways of going about it, both of which involve creating a private variable in index.ts to have the extension ids in a scope I can work with. Either have my knowledge of the extension ids tied to extensionLoader.start() by retrieving them with .listExtensions(), in which case I can not append the prefix to logs that occur before this, or import path to index.ts and duplicate the read logic found within .start() right after the creation of the extensionLoader.

Are either of these acceptable? I am certain I am missing a better solution.

@benoitf
Copy link
Collaborator

benoitf commented Nov 22, 2022

Currently, I can think of 2 ways of going about it, both of which involve creating a private variable in index.ts to have the extension ids in a scope I can work with. Either have my knowledge of the extension ids tied to extensionLoader.start() by retrieving them with .listExtensions(), in which case I can not append the prefix to logs that occur before this, or import path to index.ts and duplicate the read logic found within .start() right after the creation of the extensionLoader.

Are either of these acceptable? I am certain I am missing a better solution.

Hello @sfrunza13

We want to prefix the logging for extensions so it's ok to wait that extensionLoader is created to add a reference
Extensions won't be started during that time.

packages/main/src/plugin/index.ts
// redirect main process logs to the extension loader
this.redirectLogging();

const extensionLoader = new ExtensionLoader(
(add extensionLoader as a private variable)

then in redirectConsole() method you'll access to extensionLoader

from there (if extensionLoader is not defined, we don't try to add a prefix
if it's there, then listExtensions (as you suggested can be used) or we can add another method that returns a mapping between extensionName and root folders or the opposite

So redirectConsole should have all elements before an extension is started.

@benoitf benoitf changed the title Each extension with a simple wrapper feat: Adds extension name as prefix when extension is using logger Nov 22, 2022
@sfrunza13
Copy link
Author

@benoitf I tried using listExtensions() but the analyzedExtensions member that it is trying to return do not seem to be populated until after the extensionLoader.start() method and not the extensionLoader creation. So what I have done is I factored out the extension file reading from the start() method and I used that to build my list of extensionNames to check against. Is this ok?

@benoitf
Copy link
Collaborator

benoitf commented Nov 22, 2022

Hi @sfrunza13 I'm not sure to get all of it. But I would assume that it's ok to only try to call listExtensions only when extensionLoader is defined.

it's ok if it's not populated until the start of extension loader as we populate the list before activating the extensions (and this is where the extension is starting)

@sfrunza13
Copy link
Author

@benoitf I think this time I might have got the idea

//Take the id information from extensionLoader
let validExtObj = await this.extensionLoader.listExtensions();

validExtObj.forEach((extInfo: any) => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

AFAIK the type is not any, we have a known type returned by listExtensions()

let validExtObj = await this.extensionLoader.listExtensions();

validExtObj.forEach((extInfo: any) => {
validExt.push(extInfo.id);
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we should grab the path of the extension (add that information in the info object)
so we don't hardcode some path in the regexp

const re = new RegExp(`(podman\-desktop\\\\extensions)\\\\(${insert})`)
var extensionGroupings = stack.match(re)
if (extensionGroupings != null)
return '[' + extensionGroupings[2] + ']';
Copy link
Collaborator

Choose a reason for hiding this comment

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

we need to display the extension name, not the id, so we should keep the mapping extension path<--> extension name

@sfrunza13
Copy link
Author

How's this?

@benoitf
Copy link
Collaborator

benoitf commented Nov 24, 2022

FYI I tested it and it does not work on my computer

main ↪️ Activating extension (crc)
index.ts:106 main ↪️ Can not find CRC binary!
(anonymous) @ index.ts:106
index.ts:104 main ↪️ Activation extension (crc) ended
index.ts:104 main ↪️ Activating extension (lima)
index.ts:113 main ↪️ Could not find podman socket at /Users/benoitf/.lima/podman-lima/sock/podman.sock nor /Users/benoitf/.lima/podman-lima/sock/podman.sock
(anonymous) @ index.ts:113
index.ts:104 main ↪️ Activation extension (lima) ended
index.ts:104 main ↪️ Activating extension (kube-context)
index.ts:104 main ↪️ starting extension kube-context

Comment on lines 135 to 150
//Check if index.ts private member extensionLoader initialized
//If it is grab listExtensions()
if(this.extensionLoader != undefined){
let validExtObj = await this.extensionLoader.listExtensions();
//start setting path => name
validExtObj.forEach((extInfo: ExtensionInfo) => {
extensions.set(extInfo.path, extInfo.name)
})
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

as an improvement, I think we should build objects only if extension list is changing.
It'll cost too many iteration to do that on each log call.

Comment on lines 152 to 154
let searchString = path.replace("/","\\")
searchString = searchString.replaceAll("\\", "\\\\")
let re = new RegExp(`${searchString}`)
Copy link
Collaborator

Choose a reason for hiding this comment

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

FYI on my computer, it's not working

})

if (toAppend != '')
return '[' + toAppend + ']';
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
return '[' + toAppend + ']';
return `[${toAppend}]`;

@benoitf
Copy link
Collaborator

benoitf commented Nov 24, 2022

@sfrunza13 you'll need to sign off the commits

And apply linter/formatter
https://github.com/containers/podman-desktop/blob/main/CONTRIBUTING.md#submitting-pull-requests

@benoitf
Copy link
Collaborator

benoitf commented Nov 24, 2022

@benoitf
Copy link
Collaborator

benoitf commented Nov 24, 2022

thanks for linter and formatter. The DCO check is still need. You may have to squash all your commits into a single one

Check stack for extensions retrieved from extensionloader, then append them to log (podman-desktop#822)

Signed-off-by: stefanaz2 <sfrunza@seneca.ca>
Copy link
Collaborator

@benoitf benoitf left a comment

Choose a reason for hiding this comment

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

thanks @sfrunza13 for the contribution 👍

Activating extension (crc)
[crc] Can not find CRC binary!
Activation extension (crc) ended
Activating extension (lima)
[lima] Could not find podman socket at /Users/benoitf/.lima/podman-lima/sock/podman.sock nor /Users/benoitf/.lima/podman-lima/sock/podman.sock

in a next improvement (not this PR), we may reformat the logging of Activating/Deactivating extensions using the same pattern

so instead of

Activating extension (lima)
[lima] Could not find podman socket at /Users/benoitf/.lima/podman-lima/sock/podman.sock nor /Users/benoitf/.lima/podman-lima/sock/podman.sock
Activation extension (lima) ended
[lima] Activating extension...
[lima] Could not find podman socket at /Users/benoitf/.lima/podman-lima/sock/podman.sock nor /Users/benoitf/.lima/podman-lima/sock/podman.sock
[lima] Activation extension succeed

@sfrunza13
Copy link
Author

@benoitf Thank you for your patience and guidance

@benoitf benoitf merged commit e94ee39 into podman-desktop:main Nov 26, 2022
@podman-desktop-bot podman-desktop-bot added this to the 0.10.0 milestone Nov 26, 2022
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.

3 participants