Skip to content

test(node): Add integration test for MongoDB auto-instrumentation. #4808

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/node-integration-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@
"test:watch": "yarn test --watch"
},
"dependencies": {
"@types/mongodb": "^3.6.20",
"@types/mysql": "^2.15.21",
"@types/pg": "^8.6.5",
"express": "^4.17.3",
"mysql": "^2.18.1",
"mongodb": "^3.7.3",
"mongodb-memory-server": "^8.4.1",
"nock": "^13.1.0",
"pg": "^8.7.3",
"portfinder": "^1.0.28"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
import '@sentry/tracing';

import * as Sentry from '@sentry/node';
import { MongoClient } from 'mongodb';

Sentry.init({
dsn: 'https://[email protected]/1337',
release: '1.0',
tracesSampleRate: 1.0,
});

const client = new MongoClient(process.env.MONGO_URL || '', {
useUnifiedTopology: true,
});

async function run(): Promise<void> {
const transaction = Sentry.startTransaction({
name: 'Test Transaction',
op: 'transaction',
});

Sentry.configureScope(scope => {
scope.setSpan(transaction);
});

try {
await client.connect();

const database = client.db('admin');
const collection = database.collection('movies');

await collection.insertOne({ title: 'Rick and Morty' });
await collection.findOne({ title: 'Back to the Future' });
await collection.updateOne({ title: 'Back to the Future' }, { $set: { title: 'South Park' } });
await collection.findOne({ title: 'South Park' });
} finally {
if (transaction) transaction.finish();
await client.close();
}
}

void run();
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { MongoMemoryServer } from 'mongodb-memory-server';

import { assertSentryTransaction, getEnvelopeRequest, runServer } from '../../../../utils';

test('should auto-instrument `mongodb` package.', async () => {
const mongoServer = await MongoMemoryServer.create();
process.env.MONGO_URL = mongoServer.getUri();

const url = await runServer(__dirname);

const envelope = await getEnvelopeRequest(url);

expect(envelope).toHaveLength(3);

assertSentryTransaction(envelope[2], {
transaction: 'Test Transaction',
spans: [
{
data: {
collectionName: 'movies',
dbName: 'admin',
namespace: 'admin.movies',
doc: '{"title":"Rick and Morty"}',
},
description: 'insertOne',
op: 'db',
},
{
data: {
collectionName: 'movies',
dbName: 'admin',
namespace: 'admin.movies',
query: '{"title":"Back to the Future"}',
},
description: 'findOne',
op: 'db',
},
{
data: {
collectionName: 'movies',
dbName: 'admin',
namespace: 'admin.movies',
query: '{"title":"Back to the Future"}',
},
description: 'find',
op: 'db',
},
{
data: {
collectionName: 'movies',
dbName: 'admin',
namespace: 'admin.movies',
filter: '{"title":"Back to the Future"}',
update: '{"$set":{"title":"South Park"}}',
},
description: 'updateOne',
op: 'db',
},
{
data: {
collectionName: 'movies',
dbName: 'admin',
namespace: 'admin.movies',
query: '{"title":"South Park"}',
},
description: 'findOne',
op: 'db',
},
{
data: {
collectionName: 'movies',
dbName: 'admin',
namespace: 'admin.movies',
query: '{"title":"South Park"}',
},
description: 'find',
op: 'db',
},
],
});
});
Loading