Skip to content

Commit

Permalink
Merge 4123476 into 2f1e316
Browse files Browse the repository at this point in the history
  • Loading branch information
trentm authored May 2, 2023
2 parents 2f1e316 + 4123476 commit 91fdca2
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 56 deletions.
1 change: 1 addition & 0 deletions experimental/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ All notable changes to experimental packages in this project will be documented

### :bug: (Bug Fix)

* fix(instrumentation): update `require-in-the-middle` to v7.1.0 [#3727](https://github.com/open-telemetry/opentelemetry-js/pull/3727) @trentm
* fix(instrumentation): update `require-in-the-middle` to v7.0.1 [#3743](https://github.com/open-telemetry/opentelemetry-js/pull/3743) @trentm

### :books: (Refine Doc)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
"url": "https://github.com/open-telemetry/opentelemetry-js/issues"
},
"dependencies": {
"require-in-the-middle": "^7.0.1",
"require-in-the-middle": "^7.1.0",
"semver": "^7.3.2",
"shimmer": "^1.2.1"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

/**
Expand Down Expand Up @@ -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 },
Expand Down Expand Up @@ -88,13 +89,10 @@ 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
): Hooked {
register(moduleName: string, onRequire: OnRequireFn): Hooked {
const hooked = { moduleName, onRequire };
this._moduleNameTrie.insert(hooked);
return hooked;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -34,7 +35,7 @@ export abstract class InstrumentationBase<T = any>
implements types.Instrumentation
{
private _modules: InstrumentationModuleDefinition<T>[];
private _hooks: (Hooked | RequireInTheMiddle.Hooked)[] = [];
private _hooks: (Hooked | Hook)[] = [];
private _requireInTheMiddleSingleton: RequireInTheMiddleSingleton =
RequireInTheMiddleSingleton.getInstance();
private _enabled = false;
Expand Down Expand Up @@ -167,11 +168,7 @@ export abstract class InstrumentationBase<T = any>

this._warnOnPreloadedModules();
for (const module of this._modules) {
const onRequire: RequireInTheMiddle.OnRequireFn = (
exports,
name,
baseDir
) => {
const onRequire: OnRequireFn = (exports, name, baseDir) => {
return this._onRequire<typeof exports>(
module as unknown as InstrumentationModuleDefinition<typeof exports>,
exports,
Expand All @@ -181,9 +178,10 @@ export abstract class InstrumentationBase<T = any>
};

// `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);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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', () => {
Expand Down

0 comments on commit 91fdca2

Please sign in to comment.