From 7f3a539493abb74abe7b10fe4f7d47e4d1f10f27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Sumis=C5=82awski?= Date: Thu, 21 Nov 2024 11:46:33 +0100 Subject: [PATCH] Restore filtering by instrumentation name and catch instrumentation errors --- packages/esbuild-plugin-node/src/common.ts | 52 ++++++++++++---------- packages/esbuild-plugin-node/src/plugin.ts | 1 + 2 files changed, 30 insertions(+), 23 deletions(-) diff --git a/packages/esbuild-plugin-node/src/common.ts b/packages/esbuild-plugin-node/src/common.ts index f3322c7528..0a48abb71c 100644 --- a/packages/esbuild-plugin-node/src/common.ts +++ b/packages/esbuild-plugin-node/src/common.ts @@ -23,6 +23,7 @@ export function wrapModule( moduleVersion, oTelInstrumentationPackage, oTelInstrumentationClass, + instrumentationName, oTelInstrumentationConstructorArgs = '', }: ModuleParams ) { @@ -31,37 +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(); - 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; - } + try { + let mod = module.exports; - if (instrumentation.patch) { - diag.debug('Applying instrumentation patch ' + instrumentation.name + ' via esbuild-plugin-node'); - mod = instrumentation.patch(mod) - } + const { satisfies } = require('semver'); + const { ${oTelInstrumentationClass} } = require('${oTelInstrumentationPackage}'); + const instrumentations = new ${oTelInstrumentationClass}(${oTelInstrumentationConstructorArgs}).getModuleDefinitions(); + + 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; + } - 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.patch) { + diag.debug('Applying instrumentation patch ${instrumentationName} 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; + } + diag.debug('Applying instrumentation patch to ${path}@${moduleVersion} via esbuild-plugin-node'); + mod = file.patch(mod, '${moduleVersion}'); } - diag.debug('Applying instrumentation patch to ${path}@${moduleVersion} via esbuild-plugin-node'); - mod = file.patch(mod, '${moduleVersion}'); } } - } - module.exports = mod; + module.exports = mod; + } catch (e) { + diag.error('Error applying instrumentation ${instrumentationName}', e); + } } `; } diff --git a/packages/esbuild-plugin-node/src/plugin.ts b/packages/esbuild-plugin-node/src/plugin.ts index e4739882d4..18e90d33d3 100644 --- a/packages/esbuild-plugin-node/src/plugin.ts +++ b/packages/esbuild-plugin-node/src/plugin.ts @@ -118,6 +118,7 @@ export function openTelemetryPlugin( extractedModule.path || '' ), moduleVersion: pluginData.moduleVersion, + instrumentationName: pluginData.instrumentationName, oTelInstrumentationClass: config.oTelInstrumentationClass, oTelInstrumentationPackage: config.oTelInstrumentationPackage, oTelInstrumentationConstructorArgs: