Skip to content

Commit 611de0a

Browse files
committed
Add tests for graphql and handle instrumentations that have multiple files
1 parent 818ad5e commit 611de0a

File tree

12 files changed

+455
-192
lines changed

12 files changed

+455
-192
lines changed

package-lock.json

Lines changed: 8 additions & 160 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: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
},
3939
"devDependencies": {
4040
"@opentelemetry/api": "^1.7.0",
41-
"@opentelemetry/sdk-node": "^0.49.1",
41+
"@opentelemetry/sdk-node": "^0.50.0",
4242
"@types/mocha": "7.0.2",
4343
"@types/node": "18.6.5",
4444
"fastify": "4.15.0",
@@ -50,8 +50,8 @@
5050
"typescript": "4.4.4"
5151
},
5252
"dependencies": {
53-
"@opentelemetry/auto-instrumentations-node": "0.42.0",
54-
"@opentelemetry/instrumentation": "^0.49.1",
53+
"@opentelemetry/auto-instrumentations-node": "0.44.0",
54+
"@opentelemetry/instrumentation": "^0.50.0",
5555
"esbuild": "0.20.x"
5656
},
5757
"files": [

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export function wrapModule(
2222
oTelInstrumentationPackage,
2323
oTelInstrumentationClass,
2424
instrumentationName,
25+
instrumentedFileName,
2526
oTelInstrumentationConstructorArgs = '',
2627
}: ModuleParams
2728
) {
@@ -35,6 +36,7 @@ export function wrapModule(
3536
const { ${oTelInstrumentationClass} } = require('${oTelInstrumentationPackage}');
3637
const { diag } = require('@opentelemetry/api');
3738
const instrumentations = new ${oTelInstrumentationClass}(${oTelInstrumentationConstructorArgs}).getModuleDefinitions();
39+
3840
if (instrumentations.length > 1 && !'${instrumentationName}') {
3941
diag.error('instrumentationName must be specified because ${oTelInstrumentationClass} has multiple instrumentations');
4042
return;
@@ -55,8 +57,12 @@ export function wrapModule(
5557
diag.error('No patch nor files exist on instrumentation for ${oTelInstrumentationClass} ${instrumentationName}');
5658
return;
5759
} else if (instrumentation.files.length > 1) {
58-
diag.error('Not sure how to handle multiple files for instrumentations for ${instrumentationName}');
59-
return;
60+
const instrumentationFile = instrumentation.files.find(file => file.name === '${instrumentedFileName}');
61+
if (!instrumentationFile) {
62+
diag.error('Not sure how to handle multiple files for instrumentations for ${instrumentationName} when none is found with name ${instrumentedFileName}');
63+
return;
64+
}
65+
mod = instrumentationFile.patch(mod);
6066
}
6167
}
6268

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ function getModuleDefinitions(instrumentation: Instrumentation) {
3333

3434
export const instrumentations: InstrumentationModuleDefinition<any>[] =
3535
getNodeAutoInstrumentations().flatMap(getModuleDefinitions);
36+
// console.log({ instrumentations });
3637

3738
function configGenerator<T extends { enabled?: boolean }>(
3839
config?: T
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import * as assert from 'assert';
2+
3+
import { extractPackageAndModulePath } from './plugin';
4+
5+
describe('extractPackageAndModulePath', () => {
6+
it('', () => {
7+
assert.deepStrictEqual(
8+
extractPackageAndModulePath(
9+
'/node_modules/@aws-sdk/middleware-stack/dist/cjs/MiddlewareStack.js',
10+
'.'
11+
),
12+
{
13+
path: './node_modules/@aws-sdk/middleware-stack/dist/cjs/MiddlewareStack.js',
14+
extractedModule: {
15+
package: '@aws-sdk/middleware-stack',
16+
path: 'dist/cjs/MiddlewareStack.js',
17+
},
18+
}
19+
);
20+
// @aws-sdk/middleware-stack/dist/cjs/MiddlewareStack.js
21+
});
22+
});

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { OnLoadArgs, OpenTelemetryPluginParams } from './types';
17+
import {
18+
ExtractedModule,
19+
OnLoadArgs,
20+
OpenTelemetryPluginParams,
21+
} from './types';
1822
import { Plugin, PluginBuild } from 'esbuild';
1923
import {
2024
instrumentations,
@@ -31,11 +35,6 @@ const NODE_MODULES = 'node_modules/';
3135

3236
const BUILT_INS = new Set(builtinModules.flatMap(b => [b, `node:${b}`]));
3337

34-
interface ExtractedModule {
35-
package: string | null;
36-
path: string | null;
37-
}
38-
3938
export function openTelemetryPlugin(
4039
pluginConfig?: OpenTelemetryPluginParams
4140
): Plugin {
@@ -76,6 +75,7 @@ export function openTelemetryPlugin(
7675
return {
7776
path,
7877
pluginData: {
78+
extractedModule,
7979
shouldPatchPackage: true,
8080
instrumentation: { name: matchingInstrumentation.name },
8181
},
@@ -103,6 +103,7 @@ export function openTelemetryPlugin(
103103
instrumentationName: pluginData.instrumentation.name,
104104
oTelInstrumentationClass: config.oTelInstrumentationClass,
105105
oTelInstrumentationPackage: config.oTelInstrumentationPackage,
106+
instrumentedFileName: `${pluginData.extractedModule.package}/${pluginData.extractedModule.path}`,
106107
oTelInstrumentationConstructorArgs:
107108
config.configGenerator(packageConfig),
108109
}),
@@ -130,7 +131,7 @@ function dotFriendlyResolve(path: string, directory: string): string {
130131
* input: '/foo/node_modules/@co/stuff/foo/bar/baz.js'
131132
* output: { package: '@co/stuff', path: 'foo/bar/baz.js' }
132133
*/
133-
function extractPackageAndModulePath(
134+
export function extractPackageAndModulePath(
134135
path: string,
135136
resolveDir: string
136137
): { path: string; extractedModule: ExtractedModule } {

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,34 @@
1717
import type { OnLoadArgs as EsbuildOnLoadArgs } from 'esbuild';
1818
import type { InstrumentationConfigMap } from '@opentelemetry/auto-instrumentations-node';
1919

20+
// TODO: Get all names
2021
export type InstrumentedPackage =
2122
| 'fastify'
2223
| 'pino'
2324
| '@smithy/smithy-client'
2425
| '@smithy/middleware-stack';
2526

27+
export interface ExtractedModule {
28+
package: string | null;
29+
path: string | null;
30+
}
31+
2632
export type OnLoadArgs = Omit<EsbuildOnLoadArgs, 'pluginData'> & {
2733
pluginData?: {
2834
shouldPatchPackage: boolean;
2935
package: InstrumentedPackage;
3036
instrumentation: {
3137
name: InstrumentedPackage;
3238
};
39+
extractedModule: ExtractedModule;
3340
};
3441
};
3542

3643
export interface ModuleParams {
3744
oTelInstrumentationPackage: string;
3845
oTelInstrumentationClass: string;
3946
oTelInstrumentationConstructorArgs?: string;
47+
instrumentedFileName: string;
4048
instrumentationName?: string;
4149
}
4250

0 commit comments

Comments
 (0)