Skip to content

Commit

Permalink
feat(instrumentation-aws-lambda): take care of ESM based (.mjs) han…
Browse files Browse the repository at this point in the history
…dlers (#2508)
  • Loading branch information
serkan-ozal authored Nov 7, 2024
1 parent 80d0c74 commit 0309cae
Showing 1 changed file with 18 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,28 @@ export class AwsLambdaInstrumentation extends InstrumentationBase<AwsLambdaInstr
// Lambda loads user function using an absolute path.
let filename = path.resolve(taskRoot, moduleRoot, module);
if (!filename.endsWith('.js')) {
// its impossible to know in advance if the user has a cjs or js file.
// check that the .js file exists otherwise fallback to next known possibility
// It's impossible to know in advance if the user has a js, mjs or cjs file.
// Check that the .js file exists otherwise fallback to the next known possibilities (.mjs, .cjs).
try {
fs.statSync(`${filename}.js`);
filename += '.js';
} catch (e) {
// fallback to .cjs
filename += '.cjs';
try {
fs.statSync(`${filename}.mjs`);
// fallback to .mjs (ESM)
filename += '.mjs';
} catch (e2) {
try {
fs.statSync(`${filename}.cjs`);
// fallback to .cjs (CommonJS)
filename += '.cjs';
} catch (e3) {
this._diag.warn(
'No handler file was able to resolved with one of the known extensions for the file',
filename
);
}
}
}
}

Expand Down

0 comments on commit 0309cae

Please sign in to comment.