-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: exposed preload flags in debug mode (#1268)
refs INSTA-778
- Loading branch information
1 parent
22cb204
commit 775df15
Showing
3 changed files
with
133 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* (c) Copyright IBM Corp. 2024 | ||
*/ | ||
|
||
'use strict'; | ||
|
||
/** @type {import('../logger').GenericLogger} */ | ||
let logger; | ||
logger = require('../logger').getLogger('util/getPreloadFlags', newLogger => { | ||
logger = newLogger; | ||
}); | ||
|
||
exports.getPreloadFlags = function getPreloadFlags() { | ||
const flags = ['--require', '--import', '--experimental-loader']; | ||
|
||
/** | ||
* @param {string[]} optionArray | ||
*/ | ||
function extractOption(optionArray) { | ||
const relevantOptions = []; | ||
|
||
for (let i = 0; i < optionArray.length; i++) { | ||
if (flags.some(flag => optionArray[i].includes(flag))) { | ||
relevantOptions.push(`${optionArray[i]} ${optionArray[i + 1]}`); | ||
i++; | ||
} | ||
} | ||
|
||
return relevantOptions.join(', '); | ||
} | ||
|
||
try { | ||
let nodeOptions = ''; | ||
if (process.env.NODE_OPTIONS) { | ||
const nodeOptionsArray = process.env.NODE_OPTIONS.split(' '); | ||
nodeOptions = extractOption(nodeOptionsArray); | ||
} | ||
|
||
let execArgs = ''; | ||
if (process.execArgv.length > 0) { | ||
execArgs = extractOption(process.execArgv); | ||
} | ||
|
||
const result = [nodeOptions, execArgs].filter(Boolean).join(', ') || 'noFlags'; | ||
return result; | ||
} catch (error) { | ||
logger.error('Error occurred while doing preload flag filtering: %s ', error); | ||
return ''; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* (c) Copyright IBM Corp. 2024 | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const expect = require('chai').expect; | ||
|
||
const { getPreloadFlags } = require('../../src/util/getPreloadFlags'); | ||
|
||
describe('util.getPreloadFlags', () => { | ||
const originalNodeOptions = process.env.NODE_OPTIONS; | ||
const originalExecArgv = process.execArgv.slice(); | ||
|
||
const resetEnvironment = () => { | ||
process.env.NODE_OPTIONS = ''; | ||
process.execArgv = []; | ||
}; | ||
|
||
beforeEach(() => { | ||
resetEnvironment(); | ||
}); | ||
|
||
afterEach(() => { | ||
resetEnvironment(); | ||
}); | ||
|
||
after(() => { | ||
process.env.NODE_OPTIONS = originalNodeOptions; | ||
process.execArgv = originalExecArgv; | ||
}); | ||
|
||
it('should return relevant flags from NODE_OPTIONS', () => { | ||
process.env.NODE_OPTIONS = | ||
"INSTANA_DEBUG=true node --require '@instana/collector/src/immediate.js' ./dummy-app/src/index.js"; | ||
|
||
const result = getPreloadFlags(); | ||
|
||
expect(result).equal("--require '@instana/collector/src/immediate.js'"); | ||
}); | ||
|
||
it('should return relevant flags from execArgv', () => { | ||
process.execArgv = ['--require', '@instana/collector/src/immediate.js']; | ||
|
||
const result = getPreloadFlags(); | ||
|
||
expect(result).equal('--require @instana/collector/src/immediate.js'); | ||
}); | ||
|
||
it('should return relevant flags from both NODE_OPTIONS and execArgv', () => { | ||
process.env.NODE_OPTIONS = '--require /path/to/some/file'; | ||
process.execArgv = ['--import', '/path/to/instana/node_modules/@instana/collector/esm-register.mjs']; | ||
|
||
const result = getPreloadFlags(); | ||
|
||
expect(result).equal( | ||
'--require /path/to/some/file, --import /path/to/instana/node_modules/@instana/collector/esm-register.mjs' | ||
); | ||
}); | ||
|
||
it('should return "noFlags" when no relevant flags are found', () => { | ||
process.env.NODE_OPTIONS = '--inspect value'; | ||
process.execArgv = ['--anotherFlag', 'value']; | ||
|
||
const result = getPreloadFlags(); | ||
|
||
expect(result).equal('noFlags'); | ||
}); | ||
|
||
it('should return "noFlags" when NODE_OPTIONS and execArgv are empty', () => { | ||
const result = getPreloadFlags(); | ||
|
||
expect(result).equal('noFlags'); | ||
}); | ||
}); |