From 1025833e88f5ddab4f916a0cb8eaca7a0104039e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Serkan=20=C3=96ZAL?= <sozal@catchpoint.com>
Date: Mon, 28 Oct 2024 20:30:22 +0300
Subject: [PATCH 1/2] Take care of ESM based (`.mjs`) handlers for the AWS
 Lambda instrumentation

---
 .../src/instrumentation.ts                    | 22 +++++++++++++++----
 1 file changed, 18 insertions(+), 4 deletions(-)

diff --git a/plugins/node/opentelemetry-instrumentation-aws-lambda/src/instrumentation.ts b/plugins/node/opentelemetry-instrumentation-aws-lambda/src/instrumentation.ts
index 3dbe50e7ef..a8494d55d1 100644
--- a/plugins/node/opentelemetry-instrumentation-aws-lambda/src/instrumentation.ts
+++ b/plugins/node/opentelemetry-instrumentation-aws-lambda/src/instrumentation.ts
@@ -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
+      // 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`);
+            // fallback to .mjs (ESM)
+            filename += '.mjs';
+          } catch (e3) {
+            diag.error(
+              'No handler file was able to resolved with one of the known extensions for the file',
+              filename
+            );
+          }
+        }
       }
     }
 

From c0be5ce6849648930918610f8f7b7d24f6db6ad1 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Serkan=20=C3=96ZAL?= <sozal@catchpoint.com>
Date: Wed, 6 Nov 2024 22:34:39 +0300
Subject: [PATCH 2/2] Fix review comments

---
 .../src/instrumentation.ts                     | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)

diff --git a/plugins/node/opentelemetry-instrumentation-aws-lambda/src/instrumentation.ts b/plugins/node/opentelemetry-instrumentation-aws-lambda/src/instrumentation.ts
index a8494d55d1..23755bb264 100644
--- a/plugins/node/opentelemetry-instrumentation-aws-lambda/src/instrumentation.ts
+++ b/plugins/node/opentelemetry-instrumentation-aws-lambda/src/instrumentation.ts
@@ -97,23 +97,23 @@ 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 js, cjs or mjs file.
-      // Check that the .js file exists otherwise fallback to the next known possibilities (.cjs, .mjs).
+      // 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) {
         try {
-          fs.statSync(`${filename}.cjs`);
-          // fallback to .cjs (CommonJS)
-          filename += '.cjs';
+          fs.statSync(`${filename}.mjs`);
+          // fallback to .mjs (ESM)
+          filename += '.mjs';
         } catch (e2) {
           try {
-            fs.statSync(`${filename}.mjs`);
-            // fallback to .mjs (ESM)
-            filename += '.mjs';
+            fs.statSync(`${filename}.cjs`);
+            // fallback to .cjs (CommonJS)
+            filename += '.cjs';
           } catch (e3) {
-            diag.error(
+            this._diag.warn(
               'No handler file was able to resolved with one of the known extensions for the file',
               filename
             );