Skip to content

Commit

Permalink
fix: .cjs extension support for lambda functions (open-telemetry#1442)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ankcorn authored May 2, 2023
1 parent 2571c37 commit da737f1
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

import * as path from 'path';
import * as fs from 'fs';

import {
InstrumentationBase,
Expand Down Expand Up @@ -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 [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
exports.handler = async function (event, context) {
return "ok";
};

0 comments on commit da737f1

Please sign in to comment.