-
Notifications
You must be signed in to change notification settings - Fork 39
fix: optimized how we receive the package.json information #2176
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
base: main
Are you sure you want to change the base?
Conversation
6512b35 to
54af1ea
Compare
a945e25 to
48a0261
Compare
| metricsModules.forEach(metricsModule => { | ||
| if (metricsModule.activate) { | ||
| metricsModule.activate(config); | ||
| util.applicationUnderMonitoring.getMainPackageJsonStartingAtMainModule((err, packageJsonObj) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a bigger problem in the codebase:
Any metric requested the pkg json (in parallel).
This created too many log entries and also its logically not right.
Any outer pkg calls core.metrics.activate, which activates all registered metrics.
We request the pkg json here once and then forward a payload to the metric.
| util.applicationUnderMonitoring.getMainPackageJsonStartingAtMainModule((err, packageJsonObj) => { | ||
| if (err) { | ||
| logger.warn( | ||
| `Failed to determine main package.json. Some metrics might not work. Reason: ${err?.message} ${err?.stack}` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Log once. And also mention that only some metrics might not work.
|
|
||
| const MAX_ATTEMPTS = 5; | ||
| const DELAY = 1500; | ||
| let attempts = 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Centralized retries (see name.js and version.js etc.).
There is no need to retry from all the single metrics.
We still need the retries because of using require/import as arguments (see comment in line 163!)
| // NOTE: we already cached the package.json | ||
| if (parsedMainPackageJson !== undefined) { | ||
| return cb(null, parsedMainPackageJson); | ||
| return cb(null, { file: parsedMainPackageJson, path: mainPackageJsonPath }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Always return both file + path. Some metrics need the file, some the path.
| * @param {(err: Error, packageJsonPath: string) => void} cb - the callback will be called with an error or the path to | ||
| * the package.json file | ||
| */ | ||
| function getMainPackageJsonPathStartingAtMainModule(cb) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not needed anymore, because we now request pkg json + pkg json path centralized in core metrics index.js. We always provide the found path and the file to the metric.
| // CASE: node --require .../src/immediate.js | ||
| if (!mainModule?.filename) { | ||
| const err = new Error('Application entrypoint could not be identified.'); | ||
| return process.nextTick(cb, err, null); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the actual fix. @aryamohanan started with it already.
We now also forward the error.
| // Install the shared metrics module | ||
| const sharedMetricsPath = path.join(__dirname, '..', '..', '..', 'shared-metrics'); | ||
| runCommandSync('npm pack', sharedMetricsPath); | ||
| execSync('./preinstall.sh', { cwd: __dirname, stdio: 'inherit' }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will outsource to main. Test was failing because shared-metrics was not installed from local folder - it took the version from NPM - this does not work if you do refactorings (such as removing fns)
| execSync('npm install --no-save --no-package-lock --prefix ./ ./shared-metrics.tgz', { | ||
| cwd: __dirname, | ||
| stdio: 'inherit' | ||
| }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will outsource to main. Test was failing because shared-metrics was not installed from local folder - it took the version from NPM - this does not work if you do refactorings (such as removing fns)
refs https://jsw.ibm.com/browse/INSTA-65637
Problem:
Before:

After:
