Skip to content

Commit

Permalink
Take care of ESM based (.mjs) handlers for the AWS Lambda instrumen…
Browse files Browse the repository at this point in the history
…tation
  • Loading branch information
serkan-ozal committed Oct 28, 2024
1 parent 9a20e15 commit 118eb8e
Showing 1 changed file with 18 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,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
// Its impossible to know in advance if the user has a js, cjs or mjs file.
// Check that the .js file exists otherwise fallback to the next known possibilities (.cjs, .mjs).
try {
fs.statSync(`${filename}.js`);
filename += '.js';
} catch (e) {
// fallback to .cjs
filename += '.cjs';
try {
fs.statSync(`${filename}.cjs`);
// fallback to .cjs (CommonJS)
filename += '.cjs';
} catch (e2) {
try {
fs.statSync(`${filename}.mjs`);

Check warning on line 132 in plugins/node/opentelemetry-instrumentation-aws-lambda/src/instrumentation.ts

View check run for this annotation

Codecov / codecov/patch

plugins/node/opentelemetry-instrumentation-aws-lambda/src/instrumentation.ts#L131-L132

Added lines #L131 - L132 were not covered by tests
// fallback to .mjs (ESM)
filename += '.mjs';

Check warning on line 134 in plugins/node/opentelemetry-instrumentation-aws-lambda/src/instrumentation.ts

View check run for this annotation

Codecov / codecov/patch

plugins/node/opentelemetry-instrumentation-aws-lambda/src/instrumentation.ts#L134

Added line #L134 was not covered by tests
} catch (e3) {
diag.error(

Check warning on line 136 in plugins/node/opentelemetry-instrumentation-aws-lambda/src/instrumentation.ts

View check run for this annotation

Codecov / codecov/patch

plugins/node/opentelemetry-instrumentation-aws-lambda/src/instrumentation.ts#L136

Added line #L136 was not covered by tests
'No handler file was able to resolved with one of the known extensions for the file',
filename
);
}
}
}
}

Expand Down

0 comments on commit 118eb8e

Please sign in to comment.