Skip to content

Commit f412b86

Browse files
authored
Merge pull request #3 from coralogix/multiple-instrumentation-definitions-with-same-name
fix: handling of multiple`InstrumentationNodeModuleDefinition` with the same name
2 parents b5865a6 + 7f3a539 commit f412b86

File tree

1 file changed

+28
-25
lines changed

1 file changed

+28
-25
lines changed

packages/esbuild-plugin-node/src/common.ts

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -32,39 +32,42 @@ export function wrapModule(
3232
${originalSource}
3333
})(...arguments);
3434
{
35-
let mod = module.exports;
36-
37-
const { satisfies } = require('semver');
38-
const { ${oTelInstrumentationClass} } = require('${oTelInstrumentationPackage}');
3935
const { diag } = require('@opentelemetry/api');
40-
const instrumentations = new ${oTelInstrumentationClass}(${oTelInstrumentationConstructorArgs}).getModuleDefinitions();
4136
42-
if (instrumentations.length > 1 && !'${instrumentationName}') {
43-
diag.error('instrumentationName must be specified because ${oTelInstrumentationClass} has multiple instrumentations');
44-
return;
45-
}
46-
const instrumentation = ${
47-
instrumentationName
48-
? `instrumentations.find(i => i.name === '${instrumentationName}')`
49-
: 'instrumentations[0]'
50-
};
37+
try {
38+
let mod = module.exports;
5139
52-
if (instrumentation.patch) {
53-
mod = instrumentation.patch(mod)
54-
}
40+
const { satisfies } = require('semver');
41+
const { ${oTelInstrumentationClass} } = require('${oTelInstrumentationPackage}');
42+
const instrumentations = new ${oTelInstrumentationClass}(${oTelInstrumentationConstructorArgs}).getModuleDefinitions();
5543
56-
if (instrumentation.files?.length) {
57-
for (const file of instrumentation.files.filter(f => f.name === '${path}')) {
58-
if (!file.supportedVersions.some(v => satisfies('${moduleVersion}', v))) {
59-
diag.debug('Skipping instrumentation for ${path}@${moduleVersion} because it does not match supported versions ' + file.supportedVersions.join(','));
44+
for (const instrumentation of instrumentations.filter(i => i.name === '${instrumentationName}')) {
45+
if (!instrumentation.supportedVersions.some(v => satisfies('${moduleVersion}', v))) {
46+
diag.debug('Skipping instrumentation ${instrumentationName}, because module version ${moduleVersion} does not match supported versions ' + instrumentation.supportedVersions.join(','));
6047
continue;
6148
}
62-
mod = file.patch(mod, '${moduleVersion}');
63-
}
64-
}
6549
50+
if (instrumentation.patch) {
51+
diag.debug('Applying instrumentation patch ${instrumentationName} via esbuild-plugin-node');
52+
mod = instrumentation.patch(mod)
53+
}
6654
67-
module.exports = mod;
55+
if (instrumentation.files?.length) {
56+
for (const file of instrumentation.files.filter(f => f.name === '${path}')) {
57+
if (!file.supportedVersions.some(v => satisfies('${moduleVersion}', v))) {
58+
diag.debug('Skipping instrumentation for ${path}@${moduleVersion} because it does not match supported versions' + file.supportedVersions.join(','));
59+
continue;
60+
}
61+
diag.debug('Applying instrumentation patch to ${path}@${moduleVersion} via esbuild-plugin-node');
62+
mod = file.patch(mod, '${moduleVersion}');
63+
}
64+
}
65+
}
66+
67+
module.exports = mod;
68+
} catch (e) {
69+
diag.error('Error applying instrumentation ${instrumentationName}', e);
70+
}
6871
}
6972
`;
7073
}

0 commit comments

Comments
 (0)