Skip to content

Commit 118eb8e

Browse files
committed
Take care of ESM based (.mjs) handlers for the AWS Lambda instrumentation
1 parent 9a20e15 commit 118eb8e

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

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

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,14 +117,28 @@ export class AwsLambdaInstrumentation extends InstrumentationBase<AwsLambdaInstr
117117
// Lambda loads user function using an absolute path.
118118
let filename = path.resolve(taskRoot, moduleRoot, module);
119119
if (!filename.endsWith('.js')) {
120-
// its impossible to know in advance if the user has a cjs or js file.
121-
// check that the .js file exists otherwise fallback to next known possibility
120+
// Its impossible to know in advance if the user has a js, cjs or mjs file.
121+
// Check that the .js file exists otherwise fallback to the next known possibilities (.cjs, .mjs).
122122
try {
123123
fs.statSync(`${filename}.js`);
124124
filename += '.js';
125125
} catch (e) {
126-
// fallback to .cjs
127-
filename += '.cjs';
126+
try {
127+
fs.statSync(`${filename}.cjs`);
128+
// fallback to .cjs (CommonJS)
129+
filename += '.cjs';
130+
} catch (e2) {
131+
try {
132+
fs.statSync(`${filename}.mjs`);
133+
// fallback to .mjs (ESM)
134+
filename += '.mjs';
135+
} catch (e3) {
136+
diag.error(
137+
'No handler file was able to resolved with one of the known extensions for the file',
138+
filename
139+
);
140+
}
141+
}
128142
}
129143
}
130144

0 commit comments

Comments
 (0)