Skip to content

Commit

Permalink
Iterate over instrumentations applying those matching the module version
Browse files Browse the repository at this point in the history
  • Loading branch information
RafalSumislawski committed Nov 20, 2024
1 parent b5865a6 commit 85995ee
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 21 deletions.
37 changes: 17 additions & 20 deletions packages/esbuild-plugin-node/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ export function wrapModule(
moduleVersion,
oTelInstrumentationPackage,
oTelInstrumentationClass,
instrumentationName,
oTelInstrumentationConstructorArgs = '',
}: ModuleParams
) {
Expand All @@ -39,31 +38,29 @@ export function wrapModule(
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]'
};
for (const instrumentation of instrumentations) {
if (!instrumentation.supportedVersions.some(v => satisfies('${moduleVersion}', v))) {
diag.debug('Skipping instrumentation ${oTelInstrumentationClass}, because module version ${moduleVersion} does not match supported versions ' + instrumentation.supportedVersions.join(','));
continue;
}
if (instrumentation.patch) {
mod = instrumentation.patch(mod)
}
if (instrumentation.patch) {
diag.debug('Applying instrumentation patch ' + instrumentation.name + ' via esbuild-plugin-node');
mod = instrumentation.patch(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;
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}');
}
mod = file.patch(mod, '${moduleVersion}');
}
}
module.exports = mod;
}
`;
Expand Down
1 change: 0 additions & 1 deletion packages/esbuild-plugin-node/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ export function openTelemetryPlugin(
extractedModule.path || ''
),
moduleVersion: pluginData.moduleVersion,
instrumentationName: pluginData.instrumentationName,
oTelInstrumentationClass: config.oTelInstrumentationClass,
oTelInstrumentationPackage: config.oTelInstrumentationPackage,
oTelInstrumentationConstructorArgs:
Expand Down

0 comments on commit 85995ee

Please sign in to comment.