Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(instrumentation-aws-lambda): use require-in-the-middle directly #1314

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,12 @@
"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",
"@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"
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ import * as path from 'path';

import {
InstrumentationBase,
InstrumentationNodeModuleDefinition,
InstrumentationNodeModuleFile,
isWrapped,
safeExecuteInTheMiddle,
} from '@opentelemetry/instrumentation';
Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -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.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it might make more sense for this to happen inside an enable override, rather than in init.

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() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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 = <T>(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;
}