Skip to content

Commit 0309cae

Browse files
authored
feat(instrumentation-aws-lambda): take care of ESM based (.mjs) handlers (#2508)
1 parent 80d0c74 commit 0309cae

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
@@ -97,14 +97,28 @@ export class AwsLambdaInstrumentation extends InstrumentationBase<AwsLambdaInstr
9797
// Lambda loads user function using an absolute path.
9898
let filename = path.resolve(taskRoot, moduleRoot, module);
9999
if (!filename.endsWith('.js')) {
100-
// its impossible to know in advance if the user has a cjs or js file.
101-
// check that the .js file exists otherwise fallback to next known possibility
100+
// It's impossible to know in advance if the user has a js, mjs or cjs file.
101+
// Check that the .js file exists otherwise fallback to the next known possibilities (.mjs, .cjs).
102102
try {
103103
fs.statSync(`${filename}.js`);
104104
filename += '.js';
105105
} catch (e) {
106-
// fallback to .cjs
107-
filename += '.cjs';
106+
try {
107+
fs.statSync(`${filename}.mjs`);
108+
// fallback to .mjs (ESM)
109+
filename += '.mjs';
110+
} catch (e2) {
111+
try {
112+
fs.statSync(`${filename}.cjs`);
113+
// fallback to .cjs (CommonJS)
114+
filename += '.cjs';
115+
} catch (e3) {
116+
this._diag.warn(
117+
'No handler file was able to resolved with one of the known extensions for the file',
118+
filename
119+
);
120+
}
121+
}
108122
}
109123
}
110124

0 commit comments

Comments
 (0)