Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(instrumentation): support multiple module definitions with different versions #2120

Merged
merged 3 commits into from
Apr 20, 2021
Merged
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 @@ -59,22 +59,6 @@ export abstract class InstrumentationBase<T = any>
}
}

private _isSupported(name: string, version: string): boolean {
for (const module of this._modules) {
if (module.name === name) {
if (!module.supportedVersions) {
return true;
}

return module.supportedVersions.some(supportedVersion => {
return semver.satisfies(version, supportedVersion);
});
}
}

return false;
}

private _onRequire<T>(
module: InstrumentationModuleDefinition<T>,
exports: T,
Expand All @@ -93,7 +77,10 @@ export abstract class InstrumentationBase<T = any>
module.moduleVersion = version;
if (module.name === name) {
// main module
if (typeof version === 'string' && this._isSupported(name, version)) {
if (
typeof version === 'string' &&
isSupported(module.supportedVersions, version)
) {
if (typeof module.patch === 'function') {
module.moduleExports = exports;
if (this._enabled) {
Expand All @@ -105,12 +92,7 @@ export abstract class InstrumentationBase<T = any>
// internal file
const files = module.files ?? [];
const file = files.find(file => file.name === name);
if (
file &&
file.supportedVersions.some(supportedVersion =>
semver.satisfies(version, supportedVersion)
)
) {
if (file && isSupported(file.supportedVersions, version)) {
file.moduleExports = exports;
if (this._enabled) {
return file.patch(exports, module.moduleVersion);
Expand Down Expand Up @@ -179,3 +161,9 @@ export abstract class InstrumentationBase<T = any>
}
}
}

function isSupported(supportedVersions: string[], version: string): boolean {
return supportedVersions.some(supportedVersion => {
return semver.satisfies(version, supportedVersion);
});
}