|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +import * as assert from 'assert'; |
| 4 | +import * as sinon from 'sinon'; |
| 5 | +import * as path from 'path'; |
| 6 | +import * as fs from 'fs'; |
| 7 | +import { diag } from '@opentelemetry/api'; |
| 8 | +import { InstrumentationNodeModuleDefinition } from '@opentelemetry/instrumentation'; |
| 9 | +import { AwsLambdaInstrumentationPatch } from '../../../../src/patches/aws/services/aws-lambda'; |
| 10 | + |
| 11 | +describe('AwsLambdaInstrumentationPatch', () => { |
| 12 | + let instrumentation: AwsLambdaInstrumentationPatch; |
| 13 | + |
| 14 | + beforeEach(() => { |
| 15 | + instrumentation = new AwsLambdaInstrumentationPatch({}); |
| 16 | + }); |
| 17 | + |
| 18 | + afterEach(() => { |
| 19 | + sinon.restore(); |
| 20 | + }); |
| 21 | + |
| 22 | + describe('init', () => { |
| 23 | + it('should skip instrumentation when LAMBDA_TASK_ROOT and _HANDLER are not set', () => { |
| 24 | + process.env.LAMBDA_TASK_ROOT = ''; |
| 25 | + process.env._HANDLER = ''; |
| 26 | + |
| 27 | + const result = instrumentation.init(); |
| 28 | + |
| 29 | + assert.strictEqual(result.length, 0); |
| 30 | + }); |
| 31 | + |
| 32 | + it('should fallback to .cjs if .js does not exist', () => { |
| 33 | + process.env.LAMBDA_TASK_ROOT = '/var/task'; |
| 34 | + process.env._HANDLER = 'src/index.handler'; |
| 35 | + |
| 36 | + sinon.stub(path, 'basename').returns('index.handler'); |
| 37 | + sinon |
| 38 | + .stub(fs, 'statSync') |
| 39 | + .onFirstCall() |
| 40 | + .throws(new Error('File not found')) // .js file does not exist |
| 41 | + .onSecondCall() |
| 42 | + .returns({} as any); // .cjs file exists |
| 43 | + |
| 44 | + const result = instrumentation.init(); |
| 45 | + |
| 46 | + assert.strictEqual(result[0].name, '/var/task/src/index.cjs'); |
| 47 | + }); |
| 48 | + |
| 49 | + it('should fallback to .mjs when .js and .cjs do not exist', () => { |
| 50 | + process.env.LAMBDA_TASK_ROOT = '/var/task'; |
| 51 | + process.env._HANDLER = 'src/index.handler'; |
| 52 | + |
| 53 | + sinon.stub(path, 'basename').returns('index.handler'); |
| 54 | + sinon |
| 55 | + .stub(fs, 'statSync') |
| 56 | + .onFirstCall() |
| 57 | + .throws(new Error('File not found')) // .js not found |
| 58 | + .onSecondCall() |
| 59 | + .throws(new Error('File not found')); // .cjs not found |
| 60 | + |
| 61 | + const result = instrumentation.init(); |
| 62 | + |
| 63 | + assert.strictEqual(result[0].name, '/var/task/src/index.mjs'); |
| 64 | + }); |
| 65 | + |
| 66 | + it('should instrument CommonJS handler correctly', () => { |
| 67 | + process.env.LAMBDA_TASK_ROOT = '/var/task'; |
| 68 | + process.env._HANDLER = 'src/index.handler'; |
| 69 | + |
| 70 | + sinon.stub(path, 'basename').returns('index.handler'); |
| 71 | + sinon.stub(fs, 'statSync').returns({} as any); // Mock that the .js file exists |
| 72 | + const debugStub = sinon.stub(diag, 'debug'); |
| 73 | + |
| 74 | + const result = instrumentation.init(); |
| 75 | + |
| 76 | + assert.strictEqual(result.length, 1); |
| 77 | + assert.strictEqual(result[0].name, '/var/task/src/index.js'); |
| 78 | + assert(result[0] instanceof InstrumentationNodeModuleDefinition); |
| 79 | + assert.strictEqual(result[0].files.length, 1); |
| 80 | + assert(debugStub.calledWithMatch('Instrumenting lambda handler', sinon.match.object)); |
| 81 | + }); |
| 82 | + |
| 83 | + it('should return ESM instrumentation for .mjs files or when HANDLER_IS_ESM is set', () => { |
| 84 | + process.env.LAMBDA_TASK_ROOT = '/var/task'; |
| 85 | + process.env._HANDLER = 'src/index.handler'; |
| 86 | + process.env.HANDLER_IS_ESM = 'true'; // ESM environment variable set |
| 87 | + |
| 88 | + sinon.stub(path, 'basename').returns('index.handler'); |
| 89 | + sinon.stub(fs, 'statSync').throws(new Error('File not found')); // No .js or .cjs file exists |
| 90 | + |
| 91 | + const result = instrumentation.init(); |
| 92 | + |
| 93 | + assert.strictEqual(result.length, 1); |
| 94 | + assert.strictEqual(result[0].name, '/var/task/src/index.mjs'); |
| 95 | + assert(result[0] instanceof InstrumentationNodeModuleDefinition); |
| 96 | + assert.strictEqual(result[0].files.length, 0); // |
| 97 | + delete process.env.HANDLER_IS_ESM; |
| 98 | + }); |
| 99 | + }); |
| 100 | + |
| 101 | + it('should apply and remove patches correctly for a MJS handler', () => { |
| 102 | + process.env.LAMBDA_TASK_ROOT = '/var/task'; |
| 103 | + process.env._HANDLER = 'src/index.handler'; |
| 104 | + process.env.HANDLER_IS_ESM = 'true'; // ESM environment variable set |
| 105 | + |
| 106 | + // Mock the module exports object with a sample function |
| 107 | + const fakeModuleExports = { handler: sinon.stub() }; |
| 108 | + |
| 109 | + const wrapSpy = sinon.spy(instrumentation, '_wrap' as any); |
| 110 | + const unwrapSpy = sinon.spy(instrumentation, '_unwrap' as any); |
| 111 | + |
| 112 | + const result = instrumentation.init()[0]; |
| 113 | + // Ensure result contains patch and unpatch functions |
| 114 | + assert(result.patch, 'patch function should be defined'); |
| 115 | + assert(result.unpatch, 'unpatch function should be defined'); |
| 116 | + |
| 117 | + // Call the patch function with the mocked module exports |
| 118 | + result.patch(fakeModuleExports); |
| 119 | + |
| 120 | + // Assert that wrap is called after patching |
| 121 | + assert(wrapSpy.calledOnce, '_wrap should be called once when patch is applied'); |
| 122 | + |
| 123 | + // Call the unpatch function with the mocked module exports |
| 124 | + result.unpatch(fakeModuleExports); |
| 125 | + |
| 126 | + // Assert that unwrap is called after unpatching |
| 127 | + assert(unwrapSpy.calledOnce, '_unwrap should be called once when unpatch is called'); |
| 128 | + |
| 129 | + delete process.env.HANDLER_IS_ESM; |
| 130 | + }); |
| 131 | + |
| 132 | + it('should apply and remove patches correctly for a CJS handler', () => { |
| 133 | + process.env.LAMBDA_TASK_ROOT = '/var/task'; |
| 134 | + process.env._HANDLER = 'src/index.handler'; |
| 135 | + |
| 136 | + // Mock the module exports object with a sample function |
| 137 | + const fakeModuleExports = { handler: sinon.stub() }; |
| 138 | + sinon.stub(fs, 'statSync').returns({} as any); // Mock that the .js file exists |
| 139 | + |
| 140 | + const wrapSpy = sinon.spy(instrumentation, '_wrap' as any); |
| 141 | + const unwrapSpy = sinon.spy(instrumentation, '_unwrap' as any); |
| 142 | + |
| 143 | + const result = instrumentation.init()[0]; |
| 144 | + // Ensure result contains patch and unpatch functions |
| 145 | + assert(result.files[0].patch, 'patch function should be defined'); |
| 146 | + assert(result.files[0].unpatch, 'unpatch function should be defined'); |
| 147 | + |
| 148 | + // Call the patch function with the mocked module exports |
| 149 | + result.files[0].patch(fakeModuleExports); |
| 150 | + |
| 151 | + // Assert that wrap is called after patching |
| 152 | + assert(wrapSpy.calledOnce, '_wrap should be called once when patch is applied'); |
| 153 | + |
| 154 | + // Call the unpatch function with the mocked module exports |
| 155 | + result.files[0].unpatch(fakeModuleExports); |
| 156 | + |
| 157 | + // Assert that unwrap is called after unpatching |
| 158 | + assert(unwrapSpy.calledOnce, '_unwrap should be called once when unpatch is called'); |
| 159 | + }); |
| 160 | +}); |
0 commit comments