Skip to content

Commit 4ab3abb

Browse files
authored
test(node): Add integration test for MongoDB auto-instrumentation (#4808)
1 parent adda306 commit 4ab3abb

File tree

4 files changed

+357
-3
lines changed

4 files changed

+357
-3
lines changed

packages/node-integration-tests/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,13 @@
1515
"test:watch": "yarn test --watch"
1616
},
1717
"dependencies": {
18+
"@types/mongodb": "^3.6.20",
1819
"@types/mysql": "^2.15.21",
1920
"@types/pg": "^8.6.5",
2021
"express": "^4.17.3",
2122
"mysql": "^2.18.1",
23+
"mongodb": "^3.7.3",
24+
"mongodb-memory-server": "^8.4.1",
2225
"nock": "^13.1.0",
2326
"pg": "^8.7.3",
2427
"portfinder": "^1.0.28"
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
2+
import '@sentry/tracing';
3+
4+
import * as Sentry from '@sentry/node';
5+
import { MongoClient } from 'mongodb';
6+
7+
Sentry.init({
8+
dsn: 'https://[email protected]/1337',
9+
release: '1.0',
10+
tracesSampleRate: 1.0,
11+
});
12+
13+
const client = new MongoClient(process.env.MONGO_URL || '', {
14+
useUnifiedTopology: true,
15+
});
16+
17+
async function run(): Promise<void> {
18+
const transaction = Sentry.startTransaction({
19+
name: 'Test Transaction',
20+
op: 'transaction',
21+
});
22+
23+
Sentry.configureScope(scope => {
24+
scope.setSpan(transaction);
25+
});
26+
27+
try {
28+
await client.connect();
29+
30+
const database = client.db('admin');
31+
const collection = database.collection('movies');
32+
33+
await collection.insertOne({ title: 'Rick and Morty' });
34+
await collection.findOne({ title: 'Back to the Future' });
35+
await collection.updateOne({ title: 'Back to the Future' }, { $set: { title: 'South Park' } });
36+
await collection.findOne({ title: 'South Park' });
37+
} finally {
38+
if (transaction) transaction.finish();
39+
await client.close();
40+
}
41+
}
42+
43+
void run();
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import { MongoMemoryServer } from 'mongodb-memory-server';
2+
3+
import { assertSentryTransaction, getEnvelopeRequest, runServer } from '../../../../utils';
4+
5+
test('should auto-instrument `mongodb` package.', async () => {
6+
const mongoServer = await MongoMemoryServer.create();
7+
process.env.MONGO_URL = mongoServer.getUri();
8+
9+
const url = await runServer(__dirname);
10+
11+
const envelope = await getEnvelopeRequest(url);
12+
13+
expect(envelope).toHaveLength(3);
14+
15+
assertSentryTransaction(envelope[2], {
16+
transaction: 'Test Transaction',
17+
spans: [
18+
{
19+
data: {
20+
collectionName: 'movies',
21+
dbName: 'admin',
22+
namespace: 'admin.movies',
23+
doc: '{"title":"Rick and Morty"}',
24+
},
25+
description: 'insertOne',
26+
op: 'db',
27+
},
28+
{
29+
data: {
30+
collectionName: 'movies',
31+
dbName: 'admin',
32+
namespace: 'admin.movies',
33+
query: '{"title":"Back to the Future"}',
34+
},
35+
description: 'findOne',
36+
op: 'db',
37+
},
38+
{
39+
data: {
40+
collectionName: 'movies',
41+
dbName: 'admin',
42+
namespace: 'admin.movies',
43+
query: '{"title":"Back to the Future"}',
44+
},
45+
description: 'find',
46+
op: 'db',
47+
},
48+
{
49+
data: {
50+
collectionName: 'movies',
51+
dbName: 'admin',
52+
namespace: 'admin.movies',
53+
filter: '{"title":"Back to the Future"}',
54+
update: '{"$set":{"title":"South Park"}}',
55+
},
56+
description: 'updateOne',
57+
op: 'db',
58+
},
59+
{
60+
data: {
61+
collectionName: 'movies',
62+
dbName: 'admin',
63+
namespace: 'admin.movies',
64+
query: '{"title":"South Park"}',
65+
},
66+
description: 'findOne',
67+
op: 'db',
68+
},
69+
{
70+
data: {
71+
collectionName: 'movies',
72+
dbName: 'admin',
73+
namespace: 'admin.movies',
74+
query: '{"title":"South Park"}',
75+
},
76+
description: 'find',
77+
op: 'db',
78+
},
79+
],
80+
});
81+
});

0 commit comments

Comments
 (0)