Skip to content

Commit 7f3a539

Browse files
Restore filtering by instrumentation name and catch instrumentation errors
1 parent 85995ee commit 7f3a539

File tree

2 files changed

+30
-23
lines changed

2 files changed

+30
-23
lines changed

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

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export function wrapModule(
2323
moduleVersion,
2424
oTelInstrumentationPackage,
2525
oTelInstrumentationClass,
26+
instrumentationName,
2627
oTelInstrumentationConstructorArgs = '',
2728
}: ModuleParams
2829
) {
@@ -31,37 +32,42 @@ export function wrapModule(
3132
${originalSource}
3233
})(...arguments);
3334
{
34-
let mod = module.exports;
35-
36-
const { satisfies } = require('semver');
37-
const { ${oTelInstrumentationClass} } = require('${oTelInstrumentationPackage}');
3835
const { diag } = require('@opentelemetry/api');
39-
const instrumentations = new ${oTelInstrumentationClass}(${oTelInstrumentationConstructorArgs}).getModuleDefinitions();
4036
41-
for (const instrumentation of instrumentations) {
42-
if (!instrumentation.supportedVersions.some(v => satisfies('${moduleVersion}', v))) {
43-
diag.debug('Skipping instrumentation ${oTelInstrumentationClass}, because module version ${moduleVersion} does not match supported versions ' + instrumentation.supportedVersions.join(','));
44-
continue;
45-
}
37+
try {
38+
let mod = module.exports;
4639
47-
if (instrumentation.patch) {
48-
diag.debug('Applying instrumentation patch ' + instrumentation.name + ' via esbuild-plugin-node');
49-
mod = instrumentation.patch(mod)
50-
}
40+
const { satisfies } = require('semver');
41+
const { ${oTelInstrumentationClass} } = require('${oTelInstrumentationPackage}');
42+
const instrumentations = new ${oTelInstrumentationClass}(${oTelInstrumentationConstructorArgs}).getModuleDefinitions();
43+
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(','));
47+
continue;
48+
}
5149
52-
if (instrumentation.files?.length) {
53-
for (const file of instrumentation.files.filter(f => f.name === '${path}')) {
54-
if (!file.supportedVersions.some(v => satisfies('${moduleVersion}', v))) {
55-
diag.debug('Skipping instrumentation for ${path}@${moduleVersion} because it does not match supported versions ' + file.supportedVersions.join(','));
56-
continue;
50+
if (instrumentation.patch) {
51+
diag.debug('Applying instrumentation patch ${instrumentationName} via esbuild-plugin-node');
52+
mod = instrumentation.patch(mod)
53+
}
54+
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}');
5763
}
58-
diag.debug('Applying instrumentation patch to ${path}@${moduleVersion} via esbuild-plugin-node');
59-
mod = file.patch(mod, '${moduleVersion}');
6064
}
6165
}
62-
}
6366
64-
module.exports = mod;
67+
module.exports = mod;
68+
} catch (e) {
69+
diag.error('Error applying instrumentation ${instrumentationName}', e);
70+
}
6571
}
6672
`;
6773
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ export function openTelemetryPlugin(
118118
extractedModule.path || ''
119119
),
120120
moduleVersion: pluginData.moduleVersion,
121+
instrumentationName: pluginData.instrumentationName,
121122
oTelInstrumentationClass: config.oTelInstrumentationClass,
122123
oTelInstrumentationPackage: config.oTelInstrumentationPackage,
123124
oTelInstrumentationConstructorArgs:

0 commit comments

Comments
 (0)