Skip to content

Commit bc0d404

Browse files
committed
Support packages with patch and multiple files like redis
1 parent 2daa897 commit bc0d404

File tree

8 files changed

+283
-63
lines changed

8 files changed

+283
-63
lines changed

package-lock.json

Lines changed: 175 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/esbuild-plugin-node/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
"mocha": "7.2.0",
4646
"nyc": "15.1.0",
4747
"pino": "^8.19.0",
48+
"redis": "^4.7.0",
4849
"rimraf": "5.0.5",
4950
"ts-mocha": "10.0.0",
5051
"typescript": "4.4.4"

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

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@ import type { ModuleParams } from './types';
1919
export function wrapModule(
2020
originalSource: string,
2121
{
22+
path,
23+
moduleVersion,
2224
oTelInstrumentationPackage,
2325
oTelInstrumentationClass,
2426
instrumentationName,
25-
instrumentedFileName,
2627
oTelInstrumentationConstructorArgs = '',
2728
}: ModuleParams
2829
) {
@@ -33,6 +34,7 @@ export function wrapModule(
3334
{
3435
let mod = module.exports;
3536
37+
const { satisfies } = require('semver');
3638
const { ${oTelInstrumentationClass} } = require('${oTelInstrumentationPackage}');
3739
const { diag } = require('@opentelemetry/api');
3840
const instrumentations = new ${oTelInstrumentationClass}(${oTelInstrumentationConstructorArgs}).getModuleDefinitions();
@@ -47,29 +49,21 @@ export function wrapModule(
4749
: 'instrumentations[0]'
4850
};
4951
50-
if (instrumentation.patch && instrumentation.files?.length) {
51-
diag.error('Not sure how to handle patch and files on instrumentation for ${oTelInstrumentationClass} ${instrumentationName}');
52-
return;
53-
}
54-
5552
if (instrumentation.patch) {
5653
mod = instrumentation.patch(mod)
57-
} else {
58-
if (!instrumentation.files?.length) {
59-
diag.error('No patch nor files exist on instrumentation for ${oTelInstrumentationClass} ${instrumentationName}');
60-
return;
61-
} else if (instrumentation.files.length === 1) {
62-
mod = instrumentation.files[0].patch(mod);
63-
} else {
64-
const instrumentationFile = instrumentation.files.find(file => file.name === '${instrumentedFileName}');
65-
if (!instrumentationFile) {
66-
diag.error('Not sure how to handle multiple files for instrumentations for ${instrumentationName} when none is found with name ${instrumentedFileName}');
67-
return;
54+
}
55+
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' + f.supportedVersions.join(','));
60+
continue;
6861
}
69-
mod = instrumentationFile.patch(mod);
62+
mod = file.patch({ ...mod }, '${moduleVersion}');
7063
}
7164
}
7265
66+
7367
module.exports = mod;
7468
}
7569
`;

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ export function getOtelPackageToInstrumentationConfig() {
7272
];
7373

7474
for (const instrumentationModuleDefinition of moduleDefinitions) {
75+
// For some reason @opentelemetry/instrumentation-generic-pool reports its name as just Instrumentation
76+
// TODO: See if this goes away with an upgrade
77+
if (instrumentation.constructor.name === 'Instrumentation') continue;
7578
otelPackageToInstrumentationConfig[instrumentationModuleDefinition.name] =
7679
{
7780
oTelInstrumentationPackage:

0 commit comments

Comments
 (0)