Skip to content

Commit b891509

Browse files
Ugzuzgvmarchauddyladan
authored
fix(instrumentation): use all provided filepatches (#2963)
Co-authored-by: Valentin Marchaud <[email protected]> Co-authored-by: Daniel Dyla <[email protected]>
1 parent cb642d7 commit b891509

File tree

3 files changed

+52
-11
lines changed

3 files changed

+52
-11
lines changed

experimental/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ All notable changes to experimental packages in this project will be documented
6363
* fix(instrumentation): only patch core modules if enabled #2993 @santigimeno
6464
* fix(otlp-transformer): include esm and esnext in package files and update README #2992 @pichlermarc
6565
* fix(metrics): specification compliant default metric unit #2983 @andyfleming
66+
* fix(opentelemetry-instrumentation): use all provided patches for the same file [#2963](https://github.com/open-telemetry/opentelemetry-js/pull/2963) @Ugzuzg
6667

6768
### :books: (Refine Doc)
6869

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

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -116,18 +116,23 @@ export abstract class InstrumentationBase<T = any>
116116
}
117117
}
118118
}
119-
} else {
120-
// internal file
121-
const files = module.files ?? [];
122-
const file = files.find(f => f.name === name);
123-
if (file && isSupported(file.supportedVersions, version, module.includePrerelease)) {
124-
file.moduleExports = exports;
119+
return exports;
120+
}
121+
// internal file
122+
const files = module.files ?? [];
123+
const supportedFileInstrumentations = files
124+
.filter(f => f.name === name)
125+
.filter(f => isSupported(f.supportedVersions, version, module.includePrerelease));
126+
return supportedFileInstrumentations.reduce<T>(
127+
(patchedExports, file) => {
128+
file.moduleExports = patchedExports;
125129
if (this._enabled) {
126-
return file.patch(exports, module.moduleVersion);
130+
return file.patch(patchedExports, module.moduleVersion);
127131
}
128-
}
129-
}
130-
return exports;
132+
return patchedExports;
133+
},
134+
exports,
135+
);
131136
}
132137

133138
public enable(): void {

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

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ describe('InstrumentationBase', () => {
155155
let filePatchSpy: sinon.SinonSpy;
156156

157157
beforeEach(() => {
158-
filePatchSpy = sinon.spy();
158+
filePatchSpy = sinon.stub().callsFake(exports => exports);
159159
});
160160

161161
describe('AND there is no wildcard supported version', () => {
@@ -217,6 +217,41 @@ describe('InstrumentationBase', () => {
217217
sinon.assert.calledOnceWithExactly(filePatchSpy, moduleExports, undefined);
218218
});
219219
});
220+
221+
describe('AND there is multiple patches for the same file', () => {
222+
it('should patch the same file twice', () => {
223+
const moduleExports = {};
224+
const supportedVersions = [`^${MODULE_VERSION}`, WILDCARD_VERSION];
225+
const instrumentationModule = {
226+
supportedVersions,
227+
name: MODULE_NAME,
228+
patch: modulePatchSpy as unknown,
229+
files: [{
230+
name: MODULE_FILE_NAME,
231+
supportedVersions,
232+
patch: filePatchSpy as unknown
233+
}, {
234+
name: MODULE_FILE_NAME,
235+
supportedVersions,
236+
patch: filePatchSpy as unknown
237+
}]
238+
} as InstrumentationModuleDefinition<unknown>;
239+
240+
// @ts-expect-error access internal property for testing
241+
instrumentation._onRequire<unknown>(
242+
instrumentationModule,
243+
moduleExports,
244+
MODULE_FILE_NAME,
245+
MODULE_DIR
246+
);
247+
248+
assert.strictEqual(instrumentationModule.moduleVersion, undefined);
249+
assert.strictEqual(instrumentationModule.files[0].moduleExports, moduleExports);
250+
assert.strictEqual(instrumentationModule.files[1].moduleExports, moduleExports);
251+
sinon.assert.notCalled(modulePatchSpy);
252+
sinon.assert.calledTwice(filePatchSpy);
253+
});
254+
});
220255
});
221256
});
222257
});

0 commit comments

Comments
 (0)