Skip to content
Open
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 @@ -35,8 +35,7 @@ export abstract class InstrumentationBase<
{
private _modules: InstrumentationModuleDefinition[];
private _hooks: (Hooked | HookRequire)[] = [];
private _requireInTheMiddleSingleton: RequireInTheMiddleSingleton =
RequireInTheMiddleSingleton.getInstance();
private _requireInTheMiddleSingleton?: RequireInTheMiddleSingleton;
private _enabled = false;

constructor(
Expand Down Expand Up @@ -166,6 +165,14 @@ export abstract class InstrumentationBase<
return undefined;
}

private _getRequireInTheMiddleSingleton(): RequireInTheMiddleSingleton {
return (
this._requireInTheMiddleSingleton ??
(this._requireInTheMiddleSingleton =
RequireInTheMiddleSingleton.getInstance())
);
}

private _onRequire<T>(
module: InstrumentationModuleDefinition,
exports: T,
Expand Down Expand Up @@ -276,6 +283,10 @@ export abstract class InstrumentationBase<
return;
}

if (this._modules.length === 0) {
return;
}

this._warnOnPreloadedModules();
for (const module of this._modules) {
const hookFn: HookFn = (exports, name, baseDir) => {
Expand All @@ -298,7 +309,9 @@ export abstract class InstrumentationBase<
// require-in-the-middle `Hook`.
const hook = path.isAbsolute(module.name)
? new HookRequire([module.name], { internals: true }, onRequire)
: this._requireInTheMiddleSingleton.register(module.name, onRequire);
: this
._getRequireInTheMiddleSingleton()
.register(module.name, onRequire);

this._hooks.push(hook);
const esmHook = new HookImport(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import * as sinon from 'sinon';
import * as path from 'path';
import { InstrumentationBase } from '../../src';
import type { InstrumentationModuleDefinition } from '../../src/';
import { RequireInTheMiddleSingleton } from '../../src/platform/node/RequireInTheMiddleSingleton';
import {
InstrumentationNodeModuleDefinition,
InstrumentationNodeModuleFile,
Expand All @@ -29,6 +30,37 @@ class TestInstrumentation extends InstrumentationBase {
}

describe('InstrumentationBase', function () {
describe('constructor/enable without module definitions', function () {
let getInstanceStub: sinon.SinonStub;

class EmptyInstrumentation extends InstrumentationBase {
constructor(config = {}) {
super('@opentelemetry/instrumentation-empty-test', '0.0.0', config);
}

init() {}
}

beforeEach(() => {
getInstanceStub = sinon.stub(RequireInTheMiddleSingleton, 'getInstance');
});

afterEach(() => {
getInstanceStub.restore();
});

it('should not initialize require-in-the-middle in the constructor', function () {
new EmptyInstrumentation();
sinon.assert.notCalled(getInstanceStub);
});

it('should not initialize require-in-the-middle on enable', function () {
const instrumentation = new EmptyInstrumentation({ enabled: false });
instrumentation.enable();
sinon.assert.notCalled(getInstanceStub);
});
});

describe('_onRequire - core module', function () {
let instrumentation: TestInstrumentation;
let modulePatchSpy: sinon.SinonSpy;
Expand Down
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading