Skip to content

Commit

Permalink
Support packages with patch and multiple files like redis
Browse files Browse the repository at this point in the history
  • Loading branch information
drewcorlin1 committed Nov 9, 2024
1 parent 2daa897 commit bc0d404
Show file tree
Hide file tree
Showing 8 changed files with 283 additions and 63 deletions.
175 changes: 175 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/esbuild-plugin-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"mocha": "7.2.0",
"nyc": "15.1.0",
"pino": "^8.19.0",
"redis": "^4.7.0",
"rimraf": "5.0.5",
"ts-mocha": "10.0.0",
"typescript": "4.4.4"
Expand Down
30 changes: 12 additions & 18 deletions packages/esbuild-plugin-node/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ import type { ModuleParams } from './types';
export function wrapModule(
originalSource: string,
{
path,
moduleVersion,
oTelInstrumentationPackage,
oTelInstrumentationClass,
instrumentationName,
instrumentedFileName,
oTelInstrumentationConstructorArgs = '',
}: ModuleParams
) {
Expand All @@ -33,6 +34,7 @@ export function wrapModule(
{
let mod = module.exports;
const { satisfies } = require('semver');
const { ${oTelInstrumentationClass} } = require('${oTelInstrumentationPackage}');
const { diag } = require('@opentelemetry/api');
const instrumentations = new ${oTelInstrumentationClass}(${oTelInstrumentationConstructorArgs}).getModuleDefinitions();
Expand All @@ -47,29 +49,21 @@ export function wrapModule(
: 'instrumentations[0]'
};
if (instrumentation.patch && instrumentation.files?.length) {
diag.error('Not sure how to handle patch and files on instrumentation for ${oTelInstrumentationClass} ${instrumentationName}');
return;
}
if (instrumentation.patch) {
mod = instrumentation.patch(mod)
} else {
if (!instrumentation.files?.length) {
diag.error('No patch nor files exist on instrumentation for ${oTelInstrumentationClass} ${instrumentationName}');
return;
} else if (instrumentation.files.length === 1) {
mod = instrumentation.files[0].patch(mod);
} else {
const instrumentationFile = instrumentation.files.find(file => file.name === '${instrumentedFileName}');
if (!instrumentationFile) {
diag.error('Not sure how to handle multiple files for instrumentations for ${instrumentationName} when none is found with name ${instrumentedFileName}');
return;
}
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' + f.supportedVersions.join(','));
continue;
}
mod = instrumentationFile.patch(mod);
mod = file.patch({ ...mod }, '${moduleVersion}');
}
}
module.exports = mod;
}
`;
Expand Down
3 changes: 3 additions & 0 deletions packages/esbuild-plugin-node/src/config/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ export function getOtelPackageToInstrumentationConfig() {
];

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

0 comments on commit bc0d404

Please sign in to comment.