Skip to content

Commit 285f282

Browse files
authored
fix(instrumentation): add back support for absolute paths via require-in-the-middle (#3457)
* fix(instrumentation): add back support for absolute paths via `require-in-the-middle` * chore: add changelog entry * chore(instrumentation): fix indentation * chore(instrumentation): add enable/disable tests for `InstrumentationBase` * chore(instrumentation): fix linting errors
1 parent 91f94a8 commit 285f282

File tree

4 files changed

+136
-16
lines changed

4 files changed

+136
-16
lines changed

experimental/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ All notable changes to experimental packages in this project will be documented
1616

1717
* fix(instrumentation-xhr): http.url attribute should be absolute [#3200](https://github.com/open-telemetry/opentelemetry-js/pull/3200) @t2t2
1818
* fix(instrumentation-grpc): always set grpc semcov status code attribute with numeric value [#3076](https://github.com/open-telemetry/opentelemetry-js/pull/3076) @blumamir
19+
* fix(instrumentation): add back support for absolute paths via `require-in-the-middle` [#3457](https://github.com/open-telemetry/opentelemetry-js/pull/3457) @mhassan1
1920

2021
### :books: (Refine Doc)
2122

experimental/packages/opentelemetry-instrumentation/src/platform/node/instrumentation.ts

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
} from './RequireInTheMiddleSingleton';
2525
import { InstrumentationModuleDefinition } from './types';
2626
import { diag } from '@opentelemetry/api';
27+
import * as RequireInTheMiddle from 'require-in-the-middle';
2728

2829
/**
2930
* Base abstract class for instrumenting node plugins
@@ -33,7 +34,7 @@ export abstract class InstrumentationBase<T = any>
3334
implements types.Instrumentation
3435
{
3536
private _modules: InstrumentationModuleDefinition<T>[];
36-
private _hooks: Hooked[] = [];
37+
private _hooks: (Hooked | RequireInTheMiddle.Hooked)[] = [];
3738
private _requireInTheMiddleSingleton: RequireInTheMiddleSingleton =
3839
RequireInTheMiddleSingleton.getInstance();
3940
private _enabled = false;
@@ -166,21 +167,26 @@ export abstract class InstrumentationBase<T = any>
166167

167168
this._warnOnPreloadedModules();
168169
for (const module of this._modules) {
169-
this._hooks.push(
170-
this._requireInTheMiddleSingleton.register(
171-
module.name,
172-
(exports, name, baseDir) => {
173-
return this._onRequire<typeof exports>(
174-
module as unknown as InstrumentationModuleDefinition<
175-
typeof exports
176-
>,
177-
exports,
178-
name,
179-
baseDir
180-
);
181-
}
182-
)
183-
);
170+
const onRequire: RequireInTheMiddle.OnRequireFn = (
171+
exports,
172+
name,
173+
baseDir
174+
) => {
175+
return this._onRequire<typeof exports>(
176+
module as unknown as InstrumentationModuleDefinition<typeof exports>,
177+
exports,
178+
name,
179+
baseDir
180+
);
181+
};
182+
183+
// `RequireInTheMiddleSingleton` does not support absolute paths.
184+
// For an absolute paths, we must create a separate instance of `RequireInTheMiddle`.
185+
const hook = path.isAbsolute(module.name)
186+
? RequireInTheMiddle([module.name], { internals: true }, onRequire)
187+
: this._requireInTheMiddleSingleton.register(module.name, onRequire);
188+
189+
this._hooks.push(hook);
184190
}
185191
}
186192

experimental/packages/opentelemetry-instrumentation/test/node/InstrumentationBase.test.ts

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,12 @@
1616

1717
import * as assert from 'assert';
1818
import * as sinon from 'sinon';
19+
import * as path from 'path';
1920
import {
2021
InstrumentationBase,
2122
InstrumentationModuleDefinition,
23+
InstrumentationNodeModuleDefinition,
24+
InstrumentationNodeModuleFile,
2225
} from '../../src';
2326

2427
const MODULE_NAME = 'test-module';
@@ -284,4 +287,97 @@ describe('InstrumentationBase', () => {
284287
});
285288
});
286289
});
290+
291+
describe('enable/disable', () => {
292+
describe('AND a normal module name', () => {
293+
type Exports = Record<string, unknown>;
294+
type ExportsPatched = Exports & { __patched?: boolean };
295+
const moduleName = 'net';
296+
class TestInstrumentation extends InstrumentationBase<Exports> {
297+
constructor() {
298+
super('@opentelemetry/instrumentation-net-test', '0.0.0', {
299+
enabled: false,
300+
});
301+
}
302+
init(): InstrumentationNodeModuleDefinition<Exports>[] {
303+
return [
304+
new InstrumentationNodeModuleDefinition<Exports>(
305+
moduleName,
306+
['*'],
307+
(exports: ExportsPatched) => {
308+
exports.__patched = true;
309+
return exports;
310+
},
311+
(exports: ExportsPatched) => {
312+
exports.__patched = false;
313+
return exports;
314+
}
315+
),
316+
];
317+
}
318+
}
319+
320+
const instrumentation = new TestInstrumentation();
321+
322+
it('should patch the module', () => {
323+
instrumentation.enable();
324+
const exportsPatched = require(moduleName);
325+
assert.equal(exportsPatched.__patched, true, 'after enable');
326+
instrumentation.disable();
327+
assert.equal(exportsPatched.__patched, false, 'after disable');
328+
instrumentation.enable();
329+
assert.equal(exportsPatched.__patched, true, 'after re-enable');
330+
});
331+
});
332+
333+
describe('AND an absolute path module name', () => {
334+
type Exports = Record<string, unknown>;
335+
type ExportsPatched = Exports & { __patched?: boolean };
336+
const moduleName = 'absolutePathTestFixture';
337+
const fileName = path.join(__dirname, 'fixtures', `${moduleName}.js`);
338+
class TestInstrumentation extends InstrumentationBase<Exports> {
339+
constructor() {
340+
super('@opentelemetry/instrumentation-absolute-path-test', '0.0.0', {
341+
enabled: false,
342+
});
343+
}
344+
init(): InstrumentationNodeModuleDefinition<Exports>[] {
345+
return [
346+
new InstrumentationNodeModuleDefinition<Exports>(
347+
fileName,
348+
['*'],
349+
undefined,
350+
undefined,
351+
[
352+
new InstrumentationNodeModuleFile(
353+
moduleName,
354+
['*'],
355+
(exports: ExportsPatched) => {
356+
exports.__patched = true;
357+
return exports;
358+
},
359+
(exports?: ExportsPatched) => {
360+
if (exports) exports.__patched = false;
361+
return exports;
362+
}
363+
),
364+
]
365+
),
366+
];
367+
}
368+
}
369+
370+
const instrumentation = new TestInstrumentation();
371+
372+
it('should patch the module', () => {
373+
instrumentation.enable();
374+
const exportsPatched = require(fileName);
375+
assert.equal(exportsPatched.__patched, true, 'after enable');
376+
instrumentation.disable();
377+
assert.equal(exportsPatched.__patched, false, 'after disable');
378+
instrumentation.enable();
379+
assert.equal(exportsPatched.__patched, true, 'after re-enable');
380+
});
381+
});
382+
});
287383
});
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
module.exports = {};

0 commit comments

Comments
 (0)