Skip to content

Commit f1a5fdf

Browse files
authored
Merge pull request #2 from coralogix/work-with-optional-dependencies
fix: Work with optional dependencies
2 parents f412b86 + 715d18a commit f1a5fdf

File tree

5 files changed

+165
-8
lines changed

5 files changed

+165
-8
lines changed

package-lock.json

Lines changed: 124 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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,14 @@
3737
"esbuild": "0.21.x"
3838
},
3939
"devDependencies": {
40+
"@fastify/rate-limit": "^9.1.0",
4041
"@opentelemetry/api": "^1.9.0",
4142
"@opentelemetry/sdk-node": "^0.54.0",
4243
"@types/mocha": "7.0.2",
4344
"@types/node": "18.6.5",
4445
"fastify": "4.15.0",
4546
"mocha": "7.2.0",
47+
"mongodb": "5.9.2",
4648
"nyc": "15.1.0",
4749
"pino": "^8.19.0",
4850
"redis": "^4.7.0",

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,20 @@ export function openTelemetryPlugin(
5656
return;
5757
}
5858

59-
const { path, extractedModule } = extractPackageAndModulePath(
60-
args.path,
61-
args.resolveDir
62-
);
59+
let path;
60+
let extractedModule;
61+
62+
try {
63+
const result = extractPackageAndModulePath(
64+
args.path,
65+
args.resolveDir
66+
);
67+
path = result.path;
68+
extractedModule = result.extractedModule;
69+
} catch (e) {
70+
// Some libraries like `mongodb` require optional dependencies, which may not be present and their absence doesn't break the code
71+
// Currently esbuild doesn't provide any better way to handle this in plugins: https://github.com/evanw/esbuild/issues/1127
72+
}
6373

6474
// If it's a local import, don't patch it
6575
if (!extractedModule) return;

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,12 @@ describe('Esbuild can instrument packages via a plugin', function () {
105105
)
106106
);
107107

108-
const traceId = getTraceId(stdOutLines, 'request handler - fastify');
108+
const traceId = getTraceId(
109+
stdOutLines,
110+
'request handler - fastify -> @fastify/rate-limit'
111+
);
109112

110-
assert.ok(traceId, 'console span output in stdout contains a traceId');
113+
assert.ok(traceId, 'console span output in stdout contains a fastify span');
111114

112115
const requestHandlerLogMessage = stdOutLines.find(line =>
113116
line.includes('Log message from handler')
@@ -121,7 +124,7 @@ describe('Esbuild can instrument packages via a plugin', function () {
121124
it('redis', async () => {
122125
const traceId = getTraceId(stdOutLines, 'redis-GET');
123126

124-
assert.ok(traceId, 'console span output in stdout contains a traceId');
127+
assert.ok(traceId, 'console span output in stdout contains a redis span');
125128
});
126129

127130
describe('graphql', () => {
@@ -140,4 +143,6 @@ describe('Esbuild can instrument packages via a plugin', function () {
140143
assert.ok(parseSpan, 'There is a span for query');
141144
});
142145
});
146+
147+
// TODO: Add tests for mongodb spans. This will require running a mongodb instance.
143148
});

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,34 @@ import { createClient } from 'redis';
1919
import fastify from 'fastify';
2020
import { graphql } from './graphql/adapter';
2121
import pino from 'pino';
22+
import { MongoClient } from 'mongodb';
23+
24+
const redisClient = createClient({ url: process.env.REDIS_URL });
2225

2326
export const server = fastify({
2427
logger: pino({}, pino.destination(1)),
2528
disableRequestLogging: true,
29+
}).register(require('@fastify/rate-limit'), {
30+
// Use @fastify/rate-limit to avoid CodeQL "Missing rate limiting" error in CI
31+
global: true,
32+
max: 100,
33+
timeWindow: '1 minute',
2634
});
27-
const redisClient = createClient({ url: process.env.REDIS_URL });
2835

2936
server.get('/test', async req => {
3037
req.log.info({ hi: 'there' }, 'Log message from handler');
3138

3239
await redisClient.get('key').catch(() => void 0);
3340

41+
try {
42+
const mongoClient = await MongoClient.connect(`${process.env.MONGO_URL}`);
43+
const db = mongoClient.db('sample-database');
44+
const col = db.collection('sample-collection');
45+
await col.insertOne({ hello: 'test' });
46+
} catch (e) {
47+
req.log.info('Error connecting to MongoDB');
48+
}
49+
3450
return { hi: 'there' };
3551
});
3652

0 commit comments

Comments
 (0)