Skip to content

Commit d47af7b

Browse files
committed
Diag channels based esbuild plugin for auto instrumentation
1 parent a96ef26 commit d47af7b

File tree

5 files changed

+38
-72
lines changed

5 files changed

+38
-72
lines changed

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

Lines changed: 12 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -18,56 +18,24 @@ import type { ModuleParams } from './types';
1818

1919
export function wrapModule(
2020
originalSource: string,
21-
{
22-
path,
23-
moduleVersion,
24-
oTelInstrumentationPackage,
25-
oTelInstrumentationClass,
26-
instrumentationName,
27-
oTelInstrumentationConstructorArgs = '',
28-
}: ModuleParams
21+
{ path, moduleVersion, instrumentationName }: ModuleParams
2922
) {
3023
return `
3124
(function() {
3225
${originalSource}
3326
})(...arguments);
3427
{
35-
const { diag } = require('@opentelemetry/api');
36-
37-
try {
38-
let mod = module.exports;
39-
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-
}
49-
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}');
63-
}
64-
}
65-
}
66-
67-
module.exports = mod;
68-
} catch (e) {
69-
diag.error('Error applying instrumentation ${instrumentationName}', e);
70-
}
28+
const diagch = require('diagnostics_channel');
29+
const ch = diagch.channel('otel:bundle:load');
30+
31+
const pathMessage = {
32+
name: '${instrumentationName}' || undefined,
33+
file: '${path}' || undefined,
34+
version: '${moduleVersion}',
35+
exports: module.exports,
36+
};
37+
ch.publish(pathMessage);
38+
module.exports = pathMessage.exports;
7139
}
7240
`;
7341
}

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

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,6 @@ export function openTelemetryPlugin(
114114
otelPackageToInstrumentationConfig[pluginData.instrumentationName];
115115
if (!config) return;
116116

117-
// console.log('config is', config);
118-
const packageConfig =
119-
pluginConfig?.instrumentationConfig?.[
120-
config.oTelInstrumentationPackage
121-
];
122117
const extractedModule = pluginData.extractedModule;
123118

124119
return {
@@ -129,10 +124,6 @@ export function openTelemetryPlugin(
129124
),
130125
moduleVersion: pluginData.moduleVersion,
131126
instrumentationName: pluginData.instrumentationName,
132-
oTelInstrumentationClass: config.oTelInstrumentationClass,
133-
oTelInstrumentationPackage: config.oTelInstrumentationPackage,
134-
oTelInstrumentationConstructorArgs:
135-
config.configGenerator(packageConfig),
136127
}),
137128
resolveDir: dirname(path),
138129
};

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,6 @@ export type OnLoadArgs = Omit<EsbuildOnLoadArgs, 'pluginData'> & {
3535

3636
export interface ModuleParams {
3737
path?: string;
38-
oTelInstrumentationPackage: string;
39-
oTelInstrumentationClass: string;
40-
oTelInstrumentationConstructorArgs?: string;
4138
instrumentationName?: string;
4239
moduleVersion: string;
4340
}
@@ -68,8 +65,6 @@ export type EsbuildInstrumentationConfigMap = {
6865
};
6966

7067
export interface OpenTelemetryPluginParams {
71-
instrumentationConfig?: EsbuildInstrumentationConfigMap;
72-
7368
/** Modules to consider external and ignore from the plugin */
7469
externalModules?: string[];
7570

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

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ describe('Esbuild can instrument packages via a plugin', function () {
7979
await exec(`ts-node ${__dirname}/test-app/build.ts`);
8080

8181
const proc = startTestApp();
82-
8382
assert.ifError(proc.error);
8483
assert.equal(proc.status, 0, `proc.status (${proc.status})`);
8584
assert.equal(proc.signal, null, `proc.signal (${proc.signal})`);
@@ -96,15 +95,7 @@ describe('Esbuild can instrument packages via a plugin', function () {
9695
);
9796
});
9897

99-
it('fastify and pino', async () => {
100-
assert.ok(
101-
stdOutLines.find(
102-
logLine =>
103-
logLine ===
104-
'OpenTelemetry automatic instrumentation started successfully'
105-
)
106-
);
107-
98+
it('fastify and pino', () => {
10899
const traceId = getTraceId(
109100
stdOutLines,
110101
'request handler - fastify -> @fastify/rate-limit'
@@ -121,13 +112,13 @@ describe('Esbuild can instrument packages via a plugin', function () {
121112
assert.equal(traceId, trace_id, 'Pino logs include trace ID');
122113
});
123114

124-
it('redis', async () => {
115+
it('redis', () => {
125116
const traceId = getTraceId(stdOutLines, 'redis-GET');
126117

127118
assert.ok(traceId, 'console span output in stdout contains a redis span');
128119
});
129120

130-
it('mongodb', async () => {
121+
it('mongodb', () => {
131122
const traceId = getTraceId(stdOutLines, 'mongodb.find');
132123

133124
assert.ok(traceId, 'console span output in stdout contains a traceId');

packages/esbuild-plugin-node/test/test-app/register.js

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,36 @@
1616

1717
const opentelemetry = require('@opentelemetry/sdk-node');
1818
const { DiagConsoleLogger, diag } = require('@opentelemetry/api');
19+
const {
20+
getNodeAutoInstrumentations,
21+
} = require('@opentelemetry/auto-instrumentations-node');
22+
const {
23+
FastifyInstrumentation,
24+
} = require('@opentelemetry/instrumentation-fastify');
25+
const {
26+
RedisInstrumentation: RedisInstrumentationV4,
27+
} = require('@opentelemetry/instrumentation-redis-4');
28+
const { PinoInstrumentation } = require('@opentelemetry/instrumentation-pino');
29+
const {
30+
MongoDBInstrumentation,
31+
} = require('@opentelemetry/instrumentation-mongodb');
32+
const {
33+
GraphQLInstrumentation,
34+
} = require('@opentelemetry/instrumentation-graphql');
1935

2036
diag.setLogger(
2137
new DiagConsoleLogger(),
2238
opentelemetry.core.getEnv().OTEL_LOG_LEVEL
2339
);
2440

2541
const sdk = new opentelemetry.NodeSDK({
26-
// Notably instrumentation fastify, pino and graphql are not in here
27-
instrumentations: [],
42+
instrumentations: [
43+
new FastifyInstrumentation(),
44+
new RedisInstrumentationV4(),
45+
new PinoInstrumentation(),
46+
new MongoDBInstrumentation(),
47+
new GraphQLInstrumentation(),
48+
],
2849
});
2950

3051
try {

0 commit comments

Comments
 (0)