From ddf617f1587fcd0e6606736dbf0d02f02e4a203c Mon Sep 17 00:00:00 2001 From: Trent Mick Date: Wed, 12 Apr 2023 21:57:35 -0700 Subject: [PATCH 1/4] chore(deps): update dependency require-in-the-middle to v7, plus types --- .../package.json | 2 +- .../node/RequireInTheMiddleSingleton.ts | 7 ++-- .../src/platform/node/instrumentation.ts | 12 ++++--- .../platform/node/require-in-the-middle.d.ts | 36 ------------------- 4 files changed, 12 insertions(+), 45 deletions(-) delete mode 100644 experimental/packages/opentelemetry-instrumentation/src/platform/node/require-in-the-middle.d.ts diff --git a/experimental/packages/opentelemetry-instrumentation/package.json b/experimental/packages/opentelemetry-instrumentation/package.json index 150ec1976c..8fc883fc76 100644 --- a/experimental/packages/opentelemetry-instrumentation/package.json +++ b/experimental/packages/opentelemetry-instrumentation/package.json @@ -68,7 +68,7 @@ "url": "https://github.com/open-telemetry/opentelemetry-js/issues" }, "dependencies": { - "require-in-the-middle": "^6.0.0", + "require-in-the-middle": "elastic/require-in-the-middle#trentm/types", "semver": "^7.3.2", "shimmer": "^1.2.1" }, diff --git a/experimental/packages/opentelemetry-instrumentation/src/platform/node/RequireInTheMiddleSingleton.ts b/experimental/packages/opentelemetry-instrumentation/src/platform/node/RequireInTheMiddleSingleton.ts index 0bcea53ea8..086d3d1686 100644 --- a/experimental/packages/opentelemetry-instrumentation/src/platform/node/RequireInTheMiddleSingleton.ts +++ b/experimental/packages/opentelemetry-instrumentation/src/platform/node/RequireInTheMiddleSingleton.ts @@ -14,13 +14,14 @@ * limitations under the License. */ -import * as RequireInTheMiddle from 'require-in-the-middle'; +import type { OnRequireFn } from 'require-in-the-middle'; +import { Hook } from 'require-in-the-middle'; import * as path from 'path'; import { ModuleNameTrie, ModuleNameSeparator } from './ModuleNameTrie'; export type Hooked = { moduleName: string; - onRequire: RequireInTheMiddle.OnRequireFn; + onRequire: OnRequireFn; }; /** @@ -59,7 +60,7 @@ export class RequireInTheMiddleSingleton { } private _initialize() { - RequireInTheMiddle( + new Hook( // Intercept all `require` calls; we will filter the matching ones below null, { internals: true }, diff --git a/experimental/packages/opentelemetry-instrumentation/src/platform/node/instrumentation.ts b/experimental/packages/opentelemetry-instrumentation/src/platform/node/instrumentation.ts index 93fbb4c176..ed24245e9b 100644 --- a/experimental/packages/opentelemetry-instrumentation/src/platform/node/instrumentation.ts +++ b/experimental/packages/opentelemetry-instrumentation/src/platform/node/instrumentation.ts @@ -24,7 +24,8 @@ import { } from './RequireInTheMiddleSingleton'; import { InstrumentationModuleDefinition } from './types'; import { diag } from '@opentelemetry/api'; -import * as RequireInTheMiddle from 'require-in-the-middle'; +import type { OnRequireFn } from 'require-in-the-middle'; +import { Hook } from 'require-in-the-middle'; /** * Base abstract class for instrumenting node plugins @@ -34,7 +35,7 @@ export abstract class InstrumentationBase implements types.Instrumentation { private _modules: InstrumentationModuleDefinition[]; - private _hooks: (Hooked | RequireInTheMiddle.Hooked)[] = []; + private _hooks: (Hooked | Hook)[] = []; private _requireInTheMiddleSingleton: RequireInTheMiddleSingleton = RequireInTheMiddleSingleton.getInstance(); private _enabled = false; @@ -167,7 +168,7 @@ export abstract class InstrumentationBase this._warnOnPreloadedModules(); for (const module of this._modules) { - const onRequire: RequireInTheMiddle.OnRequireFn = ( + const onRequire: OnRequireFn = ( exports, name, baseDir @@ -181,9 +182,10 @@ export abstract class InstrumentationBase }; // `RequireInTheMiddleSingleton` does not support absolute paths. - // For an absolute paths, we must create a separate instance of `RequireInTheMiddle`. + // For an absolute paths, we must create a separate instance of the + // require-in-the-middle `Hook`. const hook = path.isAbsolute(module.name) - ? RequireInTheMiddle([module.name], { internals: true }, onRequire) + ? new Hook([module.name], { internals: true }, onRequire) : this._requireInTheMiddleSingleton.register(module.name, onRequire); this._hooks.push(hook); diff --git a/experimental/packages/opentelemetry-instrumentation/src/platform/node/require-in-the-middle.d.ts b/experimental/packages/opentelemetry-instrumentation/src/platform/node/require-in-the-middle.d.ts deleted file mode 100644 index 93a766b5fb..0000000000 --- a/experimental/packages/opentelemetry-instrumentation/src/platform/node/require-in-the-middle.d.ts +++ /dev/null @@ -1,36 +0,0 @@ -/* - * 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 63996d3df110677ba8663b954234539c4b0f80dd Mon Sep 17 00:00:00 2001 From: Trent Mick Date: Wed, 12 Apr 2023 22:07:43 -0700 Subject: [PATCH 2/4] add a changelog entry --- experimental/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/experimental/CHANGELOG.md b/experimental/CHANGELOG.md index 928a5499cc..2fa19229f7 100644 --- a/experimental/CHANGELOG.md +++ b/experimental/CHANGELOG.md @@ -11,6 +11,7 @@ All notable changes to experimental packages in this project will be documented ### :bug: (Bug Fix) * fix(sdk-node): only set DiagConsoleLogger when OTEL_LOG_LEVEL is set [#3693](https://github.com/open-telemetry/opentelemetry-js/pull/3672) @pichlermarc +* fix(instrumentation): update `require-in-the-middle` to v7 [#3727](https://github.com/open-telemetry/opentelemetry-js/pull/3727) @trentm ### :books: (Refine Doc) From 760dbd269596809933ee83a2ac003fb57a39ab73 Mon Sep 17 00:00:00 2001 From: Trent Mick Date: Wed, 12 Apr 2023 22:23:02 -0700 Subject: [PATCH 3/4] I missed these refs in my test updates --- .../src/platform/node/RequireInTheMiddleSingleton.ts | 4 ++-- .../test/node/RequireInTheMiddleSingleton.test.ts | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/experimental/packages/opentelemetry-instrumentation/src/platform/node/RequireInTheMiddleSingleton.ts b/experimental/packages/opentelemetry-instrumentation/src/platform/node/RequireInTheMiddleSingleton.ts index 086d3d1686..2637e02f7a 100644 --- a/experimental/packages/opentelemetry-instrumentation/src/platform/node/RequireInTheMiddleSingleton.ts +++ b/experimental/packages/opentelemetry-instrumentation/src/platform/node/RequireInTheMiddleSingleton.ts @@ -89,12 +89,12 @@ export class RequireInTheMiddleSingleton { * Register a hook with `require-in-the-middle` * * @param {string} moduleName Module name - * @param {RequireInTheMiddle.OnRequireFn} onRequire Hook function + * @param {OnRequireFn} onRequire Hook function * @returns {Hooked} Registered hook */ register( moduleName: string, - onRequire: RequireInTheMiddle.OnRequireFn + onRequire: OnRequireFn ): Hooked { const hooked = { moduleName, onRequire }; this._moduleNameTrie.insert(hooked); diff --git a/experimental/packages/opentelemetry-instrumentation/test/node/RequireInTheMiddleSingleton.test.ts b/experimental/packages/opentelemetry-instrumentation/test/node/RequireInTheMiddleSingleton.test.ts index df7059987c..d6401d1faa 100644 --- a/experimental/packages/opentelemetry-instrumentation/test/node/RequireInTheMiddleSingleton.test.ts +++ b/experimental/packages/opentelemetry-instrumentation/test/node/RequireInTheMiddleSingleton.test.ts @@ -17,7 +17,7 @@ import * as assert from 'assert'; import * as sinon from 'sinon'; import * as path from 'path'; -import * as RequireInTheMiddle from 'require-in-the-middle'; +import type { OnRequireFn } from 'require-in-the-middle'; import { RequireInTheMiddleSingleton } from '../../src/platform/node/RequireInTheMiddleSingleton'; const requireInTheMiddleSingleton = RequireInTheMiddleSingleton.getInstance(); @@ -31,7 +31,7 @@ const makeOnRequiresStub = (label: string): sinon.SinonStub => exports.__ritmOnRequires ??= []; exports.__ritmOnRequires.push(label); return exports; - }) as RequireInTheMiddle.OnRequireFn); + }) as OnRequireFn); describe('RequireInTheMiddleSingleton', () => { describe('register', () => { From 01dab4b013d2c403012941f0ed42c7f33072c359 Mon Sep 17 00:00:00 2001 From: Trent Mick Date: Thu, 13 Apr 2023 11:17:06 -0700 Subject: [PATCH 4/4] fix prettier formatting --- .../src/platform/node/RequireInTheMiddleSingleton.ts | 5 +---- .../src/platform/node/instrumentation.ts | 6 +----- 2 files changed, 2 insertions(+), 9 deletions(-) diff --git a/experimental/packages/opentelemetry-instrumentation/src/platform/node/RequireInTheMiddleSingleton.ts b/experimental/packages/opentelemetry-instrumentation/src/platform/node/RequireInTheMiddleSingleton.ts index 2637e02f7a..ca74699e90 100644 --- a/experimental/packages/opentelemetry-instrumentation/src/platform/node/RequireInTheMiddleSingleton.ts +++ b/experimental/packages/opentelemetry-instrumentation/src/platform/node/RequireInTheMiddleSingleton.ts @@ -92,10 +92,7 @@ export class RequireInTheMiddleSingleton { * @param {OnRequireFn} onRequire Hook function * @returns {Hooked} Registered hook */ - register( - moduleName: string, - onRequire: OnRequireFn - ): Hooked { + register(moduleName: string, onRequire: OnRequireFn): Hooked { const hooked = { moduleName, onRequire }; this._moduleNameTrie.insert(hooked); return hooked; diff --git a/experimental/packages/opentelemetry-instrumentation/src/platform/node/instrumentation.ts b/experimental/packages/opentelemetry-instrumentation/src/platform/node/instrumentation.ts index ed24245e9b..5790c9564e 100644 --- a/experimental/packages/opentelemetry-instrumentation/src/platform/node/instrumentation.ts +++ b/experimental/packages/opentelemetry-instrumentation/src/platform/node/instrumentation.ts @@ -168,11 +168,7 @@ export abstract class InstrumentationBase this._warnOnPreloadedModules(); for (const module of this._modules) { - const onRequire: OnRequireFn = ( - exports, - name, - baseDir - ) => { + const onRequire: OnRequireFn = (exports, name, baseDir) => { return this._onRequire( module as unknown as InstrumentationModuleDefinition, exports,