Skip to content

Commit edf6157

Browse files
santigimenodyladanvmarchaud
authored
fix(instrumentation): only patch core modules if enabled (#2993)
Co-authored-by: Daniel Dyla <[email protected]> Co-authored-by: Valentin Marchaud <[email protected]>
1 parent eda0b09 commit edf6157

File tree

3 files changed

+60
-1
lines changed

3 files changed

+60
-1
lines changed

experimental/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ All notable changes to experimental packages in this project will be documented
3838
* fix(opentelemetry-instrumentation-http): use correct origin when port is `null` #2948 @danielgblanco
3939
* fix(otlp-exporter-base): include esm and esnext in package files #2952 @dyladan
4040
* fix(otlp-http-exporter): update endpoint to match spec #2895 @svetlanabrennan
41+
* fix(instrumentation): only patch core modules if enabled #2993 @santigimeno
4142
* fix(otlp-transformer): include esm and esnext in package files and update README #2992 @pichlermarc
4243
* fix(metrics): specification compliant default metric unit #2983 @andyfleming
4344

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,9 @@ export abstract class InstrumentationBase<T = any>
9595
if (!baseDir) {
9696
if (typeof module.patch === 'function') {
9797
module.moduleExports = exports;
98-
return module.patch(exports);
98+
if (this._enabled) {
99+
return module.patch(exports);
100+
}
99101
}
100102
return exports;
101103
}

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

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const MODULE_FILE_NAME = 'test-module-file';
2323
const MODULE_VERSION = '0.1.0';
2424
const WILDCARD_VERSION = '*';
2525
const MODULE_DIR = '/random/dir';
26+
const CORE_MODULE = 'random_core';
2627

2728
class TestInstrumentation extends InstrumentationBase {
2829
constructor() {
@@ -33,6 +34,61 @@ class TestInstrumentation extends InstrumentationBase {
3334
}
3435

3536
describe('InstrumentationBase', () => {
37+
describe('_onRequire - core module', () => {
38+
let instrumentation: TestInstrumentation;
39+
let modulePatchSpy: sinon.SinonSpy;
40+
beforeEach(() => {
41+
instrumentation = new TestInstrumentation();
42+
modulePatchSpy = sinon.spy();
43+
});
44+
45+
describe('AND module is not enabled', () => {
46+
it('should not patch the module', () => {
47+
// @ts-expect-error access internal property for testing
48+
instrumentation._enabled = false;
49+
const moduleExports = {};
50+
const instrumentationModule = {
51+
name: CORE_MODULE,
52+
patch: modulePatchSpy as unknown,
53+
} as InstrumentationModuleDefinition<unknown>;
54+
55+
// @ts-expect-error access internal property for testing
56+
instrumentation._onRequire<unknown>(
57+
instrumentationModule,
58+
moduleExports,
59+
CORE_MODULE,
60+
undefined
61+
);
62+
63+
assert.strictEqual(instrumentationModule.moduleExports, moduleExports);
64+
sinon.assert.notCalled(modulePatchSpy);
65+
});
66+
});
67+
68+
describe('AND module is enabled', () => {
69+
it('should patch the module', () => {
70+
// @ts-expect-error access internal property for testing
71+
instrumentation._enabled = true;
72+
const moduleExports = {};
73+
const instrumentationModule = {
74+
name: CORE_MODULE,
75+
patch: modulePatchSpy as unknown,
76+
} as InstrumentationModuleDefinition<unknown>;
77+
78+
// @ts-expect-error access internal property for testing
79+
instrumentation._onRequire<unknown>(
80+
instrumentationModule,
81+
moduleExports,
82+
CORE_MODULE,
83+
undefined
84+
);
85+
86+
assert.strictEqual(instrumentationModule.moduleExports, moduleExports);
87+
sinon.assert.calledOnceWithExactly(modulePatchSpy, moduleExports);
88+
});
89+
});
90+
});
91+
3692
describe('_onRequire - module version is not available', () => {
3793
// For all of these cases, there is no indication of the actual module version,
3894
// so we require there to be a wildcard supported version.

0 commit comments

Comments
 (0)