|
| 1 | +import { AwsLambdaInstrumentation } from '@opentelemetry/instrumentation-aws-lambda'; |
| 2 | +import * as path from 'path'; |
| 3 | +import * as fs from 'fs'; |
| 4 | +import { InstrumentationNodeModuleDefinition, InstrumentationNodeModuleFile, isWrapped } from '@opentelemetry/instrumentation'; |
| 5 | +import { diag } from '@opentelemetry/api'; |
| 6 | + |
| 7 | +export class AwsLambdaInstrumentationPatch extends AwsLambdaInstrumentation { |
| 8 | + |
| 9 | + override init() { |
| 10 | + // Custom logic before calling the original implementation |
| 11 | + diag.debug('Initializing AwsLambdaInstrumentationPatch'); |
| 12 | + const taskRoot = process.env.LAMBDA_TASK_ROOT; |
| 13 | + const handlerDef = this._config.lambdaHandler ?? process.env._HANDLER; |
| 14 | + |
| 15 | + // _HANDLER and LAMBDA_TASK_ROOT are always defined in Lambda but guard bail out if in the future this changes. |
| 16 | + if (!taskRoot || !handlerDef) { |
| 17 | + this._diag.debug( |
| 18 | + 'Skipping lambda instrumentation: no _HANDLER/lambdaHandler or LAMBDA_TASK_ROOT.', |
| 19 | + { taskRoot, handlerDef } |
| 20 | + ); |
| 21 | + return []; |
| 22 | + } |
| 23 | + |
| 24 | + const handler = path.basename(handlerDef); |
| 25 | + const moduleRoot = handlerDef.substr(0, handlerDef.length - handler.length); |
| 26 | + |
| 27 | + const [module, functionName] = handler.split('.', 2); |
| 28 | + |
| 29 | + // Lambda loads user function using an absolute path. |
| 30 | + let filename = path.resolve(taskRoot, moduleRoot, module); |
| 31 | + if (!filename.endsWith('.js')) { |
| 32 | + // its impossible to know in advance if the user has a cjs or js file. |
| 33 | + // check that the .js file exists otherwise fallback to next known possibility |
| 34 | + try { |
| 35 | + fs.statSync(`${filename}.js`); |
| 36 | + filename += '.js'; |
| 37 | + } catch (e) { |
| 38 | + // fallback to .cjs |
| 39 | + try { |
| 40 | + fs.statSync(`${filename}.cjs`); |
| 41 | + filename += '.cjs'; |
| 42 | + } catch (e) { |
| 43 | + // fall back to .mjs |
| 44 | + filename += '.mjs'; |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + diag.debug('Instrumenting lambda handler', { |
| 50 | + taskRoot, |
| 51 | + handlerDef, |
| 52 | + handler, |
| 53 | + moduleRoot, |
| 54 | + module, |
| 55 | + filename, |
| 56 | + functionName, |
| 57 | + }); |
| 58 | + |
| 59 | + if (filename.endsWith('.mjs') || process.env.IS_ESM) { |
| 60 | + return [ |
| 61 | + new InstrumentationNodeModuleDefinition( |
| 62 | + // NB: The patching infrastructure seems to match names backwards, this must be the filename, while |
| 63 | + // InstrumentationNodeModuleFile must be the module name. |
| 64 | + filename, |
| 65 | + ['*'], |
| 66 | + (moduleExports: any) => { |
| 67 | + diag.debug('Applying patch for lambda esm handler'); |
| 68 | + if (isWrapped(moduleExports[functionName])) { |
| 69 | + this._unwrap(moduleExports, functionName); |
| 70 | + } |
| 71 | + this._wrap( |
| 72 | + moduleExports, |
| 73 | + functionName, |
| 74 | + (this as any)._getHandler() |
| 75 | + ); |
| 76 | + return moduleExports; |
| 77 | + }, |
| 78 | + (moduleExports?: any) => { |
| 79 | + if (moduleExports == null) return; |
| 80 | + diag.debug('Removing patch for lambda esm handler'); |
| 81 | + this._unwrap(moduleExports, functionName); |
| 82 | + } |
| 83 | + ) |
| 84 | + ]; |
| 85 | + } else { |
| 86 | + return [ |
| 87 | + new InstrumentationNodeModuleDefinition( |
| 88 | + // NB: The patching infrastructure seems to match names backwards, this must be the filename, while |
| 89 | + // InstrumentationNodeModuleFile must be the module name. |
| 90 | + filename, |
| 91 | + ['*'], |
| 92 | + undefined, |
| 93 | + undefined, |
| 94 | + [ |
| 95 | + new InstrumentationNodeModuleFile( |
| 96 | + module, |
| 97 | + ['*'], |
| 98 | + (moduleExports: any) => { |
| 99 | + diag.debug('Applying patch for lambda handler'); |
| 100 | + if (isWrapped(moduleExports[functionName])) { |
| 101 | + this._unwrap(moduleExports, functionName); |
| 102 | + } |
| 103 | + this._wrap( |
| 104 | + moduleExports, |
| 105 | + functionName, |
| 106 | + (this as any)._getHandler() |
| 107 | + ); |
| 108 | + return moduleExports; |
| 109 | + }, |
| 110 | + (moduleExports?: any) => { |
| 111 | + if (moduleExports == null) return; |
| 112 | + diag.debug('Removing patch for lambda handler'); |
| 113 | + this._unwrap(moduleExports, functionName); |
| 114 | + } |
| 115 | + ), |
| 116 | + ] |
| 117 | + ), |
| 118 | + ]; |
| 119 | + } |
| 120 | + } |
| 121 | +} |
0 commit comments