From f6b018e6a5481e432d65e0d4cd151357bf308623 Mon Sep 17 00:00:00 2001 From: mhassan1 Date: Tue, 29 Nov 2022 13:00:12 -0500 Subject: [PATCH 1/2] feat(instrumentation-aws-lambda): use `require-in-the-middle` directly --- .../package.json | 3 +- .../src/instrumentation.ts | 46 ++++++------------- .../src/require-in-the-middle.d.ts | 36 +++++++++++++++ 3 files changed, 52 insertions(+), 33 deletions(-) create mode 100644 plugins/node/opentelemetry-instrumentation-aws-lambda/src/require-in-the-middle.d.ts diff --git a/plugins/node/opentelemetry-instrumentation-aws-lambda/package.json b/plugins/node/opentelemetry-instrumentation-aws-lambda/package.json index b0ebe04f97..c1df499253 100644 --- a/plugins/node/opentelemetry-instrumentation-aws-lambda/package.json +++ b/plugins/node/opentelemetry-instrumentation-aws-lambda/package.json @@ -63,7 +63,8 @@ "@opentelemetry/propagator-aws-xray": "^1.1.1", "@opentelemetry/resources": "^1.8.0", "@opentelemetry/semantic-conventions": "^1.0.0", - "@types/aws-lambda": "8.10.81" + "@types/aws-lambda": "8.10.81", + "require-in-the-middle": "^5.0.3" }, "homepage": "https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/plugins/node/opentelemetry-instrumentation-aws-lambda#readme" } diff --git a/plugins/node/opentelemetry-instrumentation-aws-lambda/src/instrumentation.ts b/plugins/node/opentelemetry-instrumentation-aws-lambda/src/instrumentation.ts index 76d4410240..288939cb0b 100644 --- a/plugins/node/opentelemetry-instrumentation-aws-lambda/src/instrumentation.ts +++ b/plugins/node/opentelemetry-instrumentation-aws-lambda/src/instrumentation.ts @@ -18,8 +18,6 @@ import * as path from 'path'; import { InstrumentationBase, - InstrumentationNodeModuleDefinition, - InstrumentationNodeModuleFile, isWrapped, safeExecuteInTheMiddle, } from '@opentelemetry/instrumentation'; @@ -52,6 +50,7 @@ import { Context, Handler, } from 'aws-lambda'; +import * as RequireInTheMiddle from 'require-in-the-middle'; import { AwsLambdaInstrumentationConfig, EventContextExtractor } from './types'; import { VERSION } from './version'; @@ -97,39 +96,22 @@ 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. + // RITM requires a filename when requiring with an absolute path. filename += '.js'; } - return [ - new InstrumentationNodeModuleDefinition( - // NB: The patching infrastructure seems to match names backwards, this must be the filename, while - // InstrumentationNodeModuleFile must be the module name. - filename, - ['*'], - undefined, - undefined, - [ - new InstrumentationNodeModuleFile( - module, - ['*'], - (moduleExports: LambdaModule) => { - diag.debug('Applying patch for lambda handler'); - if (isWrapped(moduleExports[functionName])) { - this._unwrap(moduleExports, functionName); - } - this._wrap(moduleExports, functionName, this._getHandler()); - return moduleExports; - }, - (moduleExports?: LambdaModule) => { - if (moduleExports == undefined) return; - diag.debug('Removing patch for lambda handler'); - this._unwrap(moduleExports, functionName); - } - ), - ] - ), - ]; + // Use RITM directly because `@opentelemetry/instrumentation` does not support absolute paths. + RequireInTheMiddle([filename], ((moduleExports: LambdaModule) => { + diag.debug('Applying patch for lambda handler'); + if (isWrapped(moduleExports[functionName])) { + this._unwrap(moduleExports, functionName); + } + this._wrap(moduleExports, functionName, this._getHandler()); + return moduleExports; + }) as RequireInTheMiddle.OnRequireFn); + + // Do not use patching infrastructure, since we are using RITM directly. + return []; } private _getHandler() { diff --git a/plugins/node/opentelemetry-instrumentation-aws-lambda/src/require-in-the-middle.d.ts b/plugins/node/opentelemetry-instrumentation-aws-lambda/src/require-in-the-middle.d.ts new file mode 100644 index 0000000000..93a766b5fb --- /dev/null +++ b/plugins/node/opentelemetry-instrumentation-aws-lambda/src/require-in-the-middle.d.ts @@ -0,0 +1,36 @@ +/* + * Copyright The OpenTelemetry Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +declare module 'require-in-the-middle' { + namespace hook { + type Options = { + internals?: boolean; + }; + type OnRequireFn = (exports: T, name: string, basedir?: string) => T; + type Hooked = { unhook(): void }; + } + function hook( + modules: string[] | null, + options: hook.Options | null, + onRequire: hook.OnRequireFn + ): hook.Hooked; + function hook( + modules: string[] | null, + onRequire: hook.OnRequireFn + ): hook.Hooked; + function hook(onRequire: hook.OnRequireFn): hook.Hooked; + export = hook; +} From 9c5c6766e6b07c7a66b83805462d2c74c6ac76e5 Mon Sep 17 00:00:00 2001 From: mhassan1 Date: Tue, 29 Nov 2022 13:00:50 -0500 Subject: [PATCH 2/2] feat(instrumentation-aws-lambda): update experimental deps to `^0.34.0` --- .../node/opentelemetry-instrumentation-aws-lambda/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/node/opentelemetry-instrumentation-aws-lambda/package.json b/plugins/node/opentelemetry-instrumentation-aws-lambda/package.json index c1df499253..303d04655c 100644 --- a/plugins/node/opentelemetry-instrumentation-aws-lambda/package.json +++ b/plugins/node/opentelemetry-instrumentation-aws-lambda/package.json @@ -59,7 +59,7 @@ "typescript": "4.3.5" }, "dependencies": { - "@opentelemetry/instrumentation": "^0.32.0", + "@opentelemetry/instrumentation": "^0.34.0", "@opentelemetry/propagator-aws-xray": "^1.1.1", "@opentelemetry/resources": "^1.8.0", "@opentelemetry/semantic-conventions": "^1.0.0",