From da737f1c1eda59d7e340c4026a212d21abcb72d6 Mon Sep 17 00:00:00 2001 From: Thomas Ankcorn Date: Tue, 2 May 2023 12:59:04 +0100 Subject: [PATCH] fix: .cjs extension support for lambda functions (#1442) --- .../src/instrumentation.ts | 12 ++++++++++-- .../test/integrations/lambda-handler.test.ts | 16 ++++++++++++++++ .../test/lambda-test/commonjs.cjs | 3 +++ 3 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 plugins/node/opentelemetry-instrumentation-aws-lambda/test/lambda-test/commonjs.cjs diff --git a/plugins/node/opentelemetry-instrumentation-aws-lambda/src/instrumentation.ts b/plugins/node/opentelemetry-instrumentation-aws-lambda/src/instrumentation.ts index 04b51a9842d..0110293ef76 100644 --- a/plugins/node/opentelemetry-instrumentation-aws-lambda/src/instrumentation.ts +++ b/plugins/node/opentelemetry-instrumentation-aws-lambda/src/instrumentation.ts @@ -15,6 +15,7 @@ */ import * as path from 'path'; +import * as fs from 'fs'; import { InstrumentationBase, @@ -99,8 +100,15 @@ export class AwsLambdaInstrumentation extends InstrumentationBase { // Lambda loads user function using an absolute path. let filename = path.resolve(taskRoot, moduleRoot, module); if (!filename.endsWith('.js')) { - // Patching infrastructure currently requires a filename when requiring with an absolute path. - filename += '.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 + try { + fs.statSync(`${filename}.js`); + filename += '.js'; + } catch (e) { + // fallback to .cjs + filename += '.cjs'; + } } return [ diff --git a/plugins/node/opentelemetry-instrumentation-aws-lambda/test/integrations/lambda-handler.test.ts b/plugins/node/opentelemetry-instrumentation-aws-lambda/test/integrations/lambda-handler.test.ts index b84f834c799..681922fbee3 100644 --- a/plugins/node/opentelemetry-instrumentation-aws-lambda/test/integrations/lambda-handler.test.ts +++ b/plugins/node/opentelemetry-instrumentation-aws-lambda/test/integrations/lambda-handler.test.ts @@ -861,5 +861,21 @@ describe('lambda handler', () => { assert.strictEqual(span.attributes[ERR_ATTR], error!.message); }); }); + + describe('.cjs lambda bundle', () => { + it('should export a valid span', async () => { + initializeHandler('lambda-test/commonjs.handler'); + const result = await lambdaRequire('lambda-test/commonjs.cjs').handler( + 'arg', + ctx + ); + assert.strictEqual(result, 'ok'); + const spans = memoryExporter.getFinishedSpans(); + const [span] = spans; + assert.strictEqual(spans.length, 1); + assertSpanSuccess(span); + assert.strictEqual(span.parentSpanId, undefined); + }); + }); }); }); diff --git a/plugins/node/opentelemetry-instrumentation-aws-lambda/test/lambda-test/commonjs.cjs b/plugins/node/opentelemetry-instrumentation-aws-lambda/test/lambda-test/commonjs.cjs new file mode 100644 index 00000000000..3fc61adccf2 --- /dev/null +++ b/plugins/node/opentelemetry-instrumentation-aws-lambda/test/lambda-test/commonjs.cjs @@ -0,0 +1,3 @@ +exports.handler = async function (event, context) { + return "ok"; +}; \ No newline at end of file