Skip to content

Commit

Permalink
Merge pull request #3 from coralogix/multiple-instrumentation-definit…
Browse files Browse the repository at this point in the history
…ions-with-same-name

fix: handling of multiple`InstrumentationNodeModuleDefinition` with the same name
  • Loading branch information
drewcorlin1 authored Nov 21, 2024
2 parents b5865a6 + 7f3a539 commit f412b86
Showing 1 changed file with 28 additions and 25 deletions.
53 changes: 28 additions & 25 deletions packages/esbuild-plugin-node/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,39 +32,42 @@ export function wrapModule(
${originalSource}
})(...arguments);
{
let mod = module.exports;
const { satisfies } = require('semver');
const { ${oTelInstrumentationClass} } = require('${oTelInstrumentationPackage}');
const { diag } = require('@opentelemetry/api');
const instrumentations = new ${oTelInstrumentationClass}(${oTelInstrumentationConstructorArgs}).getModuleDefinitions();
if (instrumentations.length > 1 && !'${instrumentationName}') {
diag.error('instrumentationName must be specified because ${oTelInstrumentationClass} has multiple instrumentations');
return;
}
const instrumentation = ${
instrumentationName
? `instrumentations.find(i => i.name === '${instrumentationName}')`
: 'instrumentations[0]'
};
try {
let mod = module.exports;
if (instrumentation.patch) {
mod = instrumentation.patch(mod)
}
const { satisfies } = require('semver');
const { ${oTelInstrumentationClass} } = require('${oTelInstrumentationPackage}');
const instrumentations = new ${oTelInstrumentationClass}(${oTelInstrumentationConstructorArgs}).getModuleDefinitions();
if (instrumentation.files?.length) {
for (const file of instrumentation.files.filter(f => f.name === '${path}')) {
if (!file.supportedVersions.some(v => satisfies('${moduleVersion}', v))) {
diag.debug('Skipping instrumentation for ${path}@${moduleVersion} because it does not match supported versions ' + file.supportedVersions.join(','));
for (const instrumentation of instrumentations.filter(i => i.name === '${instrumentationName}')) {
if (!instrumentation.supportedVersions.some(v => satisfies('${moduleVersion}', v))) {
diag.debug('Skipping instrumentation ${instrumentationName}, because module version ${moduleVersion} does not match supported versions ' + instrumentation.supportedVersions.join(','));
continue;
}
mod = file.patch(mod, '${moduleVersion}');
}
}
if (instrumentation.patch) {
diag.debug('Applying instrumentation patch ${instrumentationName} via esbuild-plugin-node');
mod = instrumentation.patch(mod)
}
module.exports = mod;
if (instrumentation.files?.length) {
for (const file of instrumentation.files.filter(f => f.name === '${path}')) {
if (!file.supportedVersions.some(v => satisfies('${moduleVersion}', v))) {
diag.debug('Skipping instrumentation for ${path}@${moduleVersion} because it does not match supported versions' + file.supportedVersions.join(','));
continue;
}
diag.debug('Applying instrumentation patch to ${path}@${moduleVersion} via esbuild-plugin-node');
mod = file.patch(mod, '${moduleVersion}');
}
}
}
module.exports = mod;
} catch (e) {
diag.error('Error applying instrumentation ${instrumentationName}', e);
}
}
`;
}

0 comments on commit f412b86

Please sign in to comment.