8000 test(node): Add Mongo otel integration test (#10224) · GingerAdonis/sentry-javascript@1fa4266 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1fa4266

Browse files
authored
test(node): Add Mongo otel integration test (getsentry#10224)
This PR adds a `@sentry/node-experimental` test for Mongo auto-instrumentation. There is no ESM test because `mongodb` doesn't support support ESM.
1 parent a5d1d7b commit 1fa4266

File tree

2 files changed

+135
-0
lines changed

2 files changed

+135
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
const { loggingTransport } = require('@sentry-internal/node-integration-tests');
2+
const Sentry = require('@sentry/node-experimental');
3+
4+
Sentry.init({
5+
dsn: 'https://public@dsn.ingest.sentry.io/1337',
6+
release: '1.0',
7+
tracesSampleRate: 1.0,
8+
debug: true,
9+
transport: loggingTransport,
10+
});
11+
12+
// Must be required after Sentry is initialized
13+
const { MongoClient } = require('mongodb');
14+
15+
const client = new MongoClient(process.env.MONGO_URL || '', {
16+
useUnifiedTopology: true,
17+
});
18+
19+
async function run() {
20+
await Sentry.startSpan(
21+
{
22+
name: 'Test Transaction',
23+
op: 'transaction',
24+
},
25+
async () => {
26+
try {
27+
await client.connect();
28+
29+
const database = client.db('admin');
30+
const collection = database.collection('movies');
31+
32+
await collection.insertOne({ title: 'Rick and Morty' });
33+
await collection.findOne({ title: 'Back to the Future' });
34+
await collection.updateOne({ title: 'Back to the Future' }, { $set: { title: 'South Park' } });
35+
await collection.findOne({ title: 'South Park' });
36+
37+
await collection.find({ title: 'South Park' }).toArray();
38+
} finally {
39+
await client.close();
40+
}
41+
},
42+
);
43+
44+
Sentry.flush(2000);
45+
}
46+
47+
// eslint-disable-next-line @typescript-eslint/no-floating-promises
48+
run();
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import { MongoMemoryServer } from 'mongodb-memory-server-global';
2+
3+
import { conditionalTest } from '../../../utils';
4+
import { cleanupChildProcesses, createRunner } from '../../../utils/runner';
5+
6+
jest.setTimeout(20000);
7+
8+
conditionalTest({ min: 14 })('MongoDB experimental Test', () => {
9+
let mongoServer: MongoMemoryServer;
10+
11+
beforeAll(async () => {
12+
mongoServer = await MongoMemoryServer.create();
13+
process.env.MONGO_URL = mongoServer.getUri();
14+
}, 10000);
15+
16+
afterAll(async () => {
17+
if (mongoServer) {
18+
await mongoServer.stop();
19+
}
20+
cleanupChildProcesses();
21+
});
22+
23+
const EXPECTED_TRANSACTION = {
24+
transaction: 'Test Transaction',
25+
spans: expect.arrayContaining([
26+
expect.objectContaining({
27+
data: expect.objectContaining({
28+
'db.system': 'mongodb',
29+
'db.name': 'admin',
30+
'db.operation': 'insert',
31+
'db.mongodb.collection': 'movies',
32+
}),
33+
description: '{"title":"?","_id":"?"}',
34+
op: 'db',
35+
origin: 'auto.db.otel.mongo',
36+
}),
37+
expect.objectContaining({
38+
data: expect.objectContaining({
39+
'db.system': 'mongodb',
40+
'db.name': 'admin',
41+
'db.operation': 'find',
42+
'db.mongodb.collection': 'movies',
43+
}),
44+
description: '{"title":"?"}',
45+ 57AE
op: 'db',
46+
origin: 'auto.db.otel.mongo',
47+
}),
48+
expect.objectContaining({
49+
data: expect.objectContaining({
50+
'db.system': 'mongodb',
51+
'db.name': 'admin',
52+
'db.operation': 'update',
53+
'db.mongodb.collection': 'movies',
54+
}),
55+
description: '{"title":"?"}',
56+
op: 'db',
57+
origin: 'auto.db.otel.mongo',
58+
}),
59+
expect.objectContaining({
60+
data: expect.objectContaining({
61+
'db.system': 'mongodb',
62+
'db.name': 'admin',
63+
'db.operation': 'find',
64+
'db.mongodb.collection': 'movies',
65+
}),
66+
description: '{"title":"?"}',
67+
op: 'db',
68+
origin: 'auto.db.otel.mongo',
69+
}),
70+
expect.objectContaining({
71+
data: expect.objectContaining({
72+
'db.system': 'mongodb',
73+
'db.name': 'admin',
74+
'db.operation': 'find',
75+
'db.mongodb.collection': 'movies',
76+
}),
77+
description: '{"title":"?"}',
78+
op: 'db',
79+
origin: 'auto.db.otel.mongo',
80+
}),
81+
]),
82+
};
83+
84+
test('CJS - should auto-instrument `mongodb` package.', done => {
85+
createRunner(__dirname, 'scenario.js').expect({ transaction: EXPECTED_TRANSACTION }).start(done);
86+
});
87+
});

0 commit comments

Comments
 (0)
0