Skip to content

Commit a380b93

Browse files
authored
test(node): Add mysql auto instrumentation tests for @sentry/node-experimental (#10255)
This PR adds auto instrumentation tests for `mysql` for `@sentry/node-experimental`. There are no ESM tests because `mysql` does not support ESM!
1 parent 97c4e3b commit a380b93

File tree

5 files changed

+216
-3
lines changed

5 files changed

+216
-3
lines changed

dev-packages/node-integration-tests/suites/tracing-experimental/mongodb/scenario.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ Sentry.init({
55
dsn: 'https://[email protected]/1337',
66
release: '1.0',
77
tracesSampleRate: 1.0,
8-
debug: true,
98
transport: loggingTransport,
109
});
1110

11+
// Stop the process from exiting before the transaction is sent
12+
setInterval(() => {}, 1000);
13+
1214
// Must be required after Sentry is initialized
1315
const { MongoClient } = require('mongodb');
1416

@@ -40,8 +42,6 @@ async function run() {
4042
}
4143
},
4244
);
43-
44-
Sentry.flush(2000);
4545
}
4646

4747
// eslint-disable-next-line @typescript-eslint/no-floating-promises
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
const { loggingTransport } = require('@sentry-internal/node-integration-tests');
2+
const Sentry = require('@sentry/node-experimental');
3+
4+
Sentry.init({
5+
dsn: 'https://[email protected]/1337',
6+
release: '1.0',
7+
tracesSampleRate: 1.0,
8+
transport: loggingTransport,
9+
});
10+
11+
// Stop the process from exiting before the transaction is sent
12+
setInterval(() => {}, 1000);
13+
14+
const mysql = require('mysql');
15+
16+
const connection = mysql.createConnection({
17+
user: 'root',
18+
password: 'docker',
19+
});
20+
21+
connection.connect(function (err) {
22+
if (err) {
23+
return;
24+
}
25+
});
26+
27+
Sentry.startSpanManual(
28+
{
29+
op: 'transaction',
30+
name: 'Test Transaction',
31+
},
32+
span => {
33+
connection.query('SELECT 1 + 1 AS solution', function () {
34+
connection.query('SELECT NOW()', ['1', '2'], () => {
35+
span.end();
36+
connection.end();
37+
});
38+
});
39+
},
40+
);
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
const { loggingTransport } = require('@sentry-internal/node-integration-tests');
2+
const Sentry = require('@sentry/node-experimental');
3+
4+
Sentry.init({
5+
dsn: 'https://[email protected]/1337',
6+
release: '1.0',
7+
tracesSampleRate: 1.0,
8+
transport: loggingTransport,
9+
});
10+
11+
// Stop the process from exiting before the transaction is sent
12+
setInterval(() => {}, 1000);
13+
14+
const mysql = require('mysql');
15+
16+
const connection = mysql.createConnection({
17+
user: 'root',
18+
password: 'docker',
19+
});
20+
21+
connection.connect(function (err) {
22+
if (err) {
23+
return;
24+
}
25+
});
26+
27+
Sentry.startSpan(
28+
{
29+
op: 'transaction',
30+
name: 'Test Transaction',
31+
},
32+
span => {
33+
const query = connection.query('SELECT 1 + 1 AS solution');
34+
const query2 = connection.query('SELECT NOW()', ['1', '2']);
35+
36+
query.on('end', () => {
37+
query2.on('end', () => {
38+
// Wait a bit to ensure the queries completed
39+
setTimeout(() => {
40+
span.end();
41+
}, 500);
42+
});
43+
});
44+
},
45+
);
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const { loggingTransport } = require('@sentry-internal/node-integration-tests');
2+
const Sentry = require('@sentry/node-experimental');
3+
4+
Sentry.init({
5+
dsn: 'https://[email protected]/1337',
6+
release: '1.0',
7+
tracesSampleRate: 1.0,
8+
transport: loggingTransport,
9+
});
10+
11+
// Stop the process from exiting before the transaction is sent
12+
setInterval(() => {}, 1000);
13+
14+
const mysql = require('mysql');
15+
16+
const connection = mysql.createConnection({
17+
user: 'root',
18+
password: 'docker',
19+
});
20+
21+
Sentry.startSpanManual(
22+
{
23+
op: 'transaction',
24+
name: 'Test Transaction',
25+
},
26+
span => {
27+
connection.query('SELECT 1 + 1 AS solution', function () {
28+
connection.query('SELECT NOW()', ['1', '2'], () => {
29+
span.end();
30+
connection.end();
31+
});
32+
});
33+
},
34+
);
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import { conditionalTest } from '../../../utils';
2+
import { createRunner } from '../../../utils/runner';
3+
4+
conditionalTest({ min: 14 })('mysql auto instrumentation', () => {
5+
test('should auto-instrument `mysql` package when using connection.connect()', done => {
6+
const EXPECTED_TRANSACTION = {
7+
transaction: 'Test Transaction',
8+
spans: expect.arrayContaining([
9+
expect.objectContaining({
10+
description: 'SELECT 1 + 1 AS solution',
11+
op: 'db',
12+
data: expect.objectContaining({
13+
'db.system': 'mysql',
14+
'net.peer.name': 'localhost',
15+
'net.peer.port': 3306,
16+
'db.user': 'root',
17+
}),
18+
}),
19+
expect.objectContaining({
20+
description: 'SELECT NOW()',
21+
op: 'db',
22+
data: expect.objectContaining({
23+
'db.system': 'mysql',
24+
'net.peer.name': 'localhost',
25+
'net.peer.port': 3306,
26+
'db.user': 'root',
27+
}),
28+
}),
29+
]),
30+
};
31+
32+
createRunner(__dirname, 'scenario-withConnect.js').expect({ transaction: EXPECTED_TRANSACTION }).start(done);
33+
});
34+
35+
test('should auto-instrument `mysql` package when using query without callback', done => {
36+
const EXPECTED_TRANSACTION = {
37+
transaction: 'Test Transaction',
38+
spans: expect.arrayContaining([
39+
expect.objectContaining({
40+
description: 'SELECT 1 + 1 AS solution',
41+
op: 'db',
42+
data: expect.objectContaining({
43+
'db.system': 'mysql',
44+
'net.peer.name': 'localhost',
45+
'net.peer.port': 3306,
46+
'db.user': 'root',
47+
}),
48+
}),
49+
expect.objectContaining({
50+
description: 'SELECT NOW()',
51+
op: 'db',
52+
data: expect.objectContaining({
53+
'db.system': 'mysql',
54+
'net.peer.name': 'localhost',
55+
'net.peer.port': 3306,
56+
'db.user': 'root',
57+
}),
58+
}),
59+
]),
60+
};
61+
62+
createRunner(__dirname, 'scenario-withoutCallback.js').expect({ transaction: EXPECTED_TRANSACTION }).start(done);
63+
});
64+
65+
test('should auto-instrument `mysql` package without connection.connect()', done => {
66+
const EXPECTED_TRANSACTION = {
67+
transaction: 'Test Transaction',
68+
spans: expect.arrayContaining([
69+
expect.objectContaining({
70+
description: 'SELECT 1 + 1 AS solution',
71+
op: 'db',
72+
data: expect.objectContaining({
73+
'db.system': 'mysql',
74+
'net.peer.name': 'localhost',
75+
'net.peer.port': 3306,
76+
'db.user': 'root',
77+
}),
78+
}),
79+
expect.objectContaining({
80+
description: 'SELECT NOW()',
81+
op: 'db',
82+
data: expect.objectContaining({
83+
'db.system': 'mysql',
84+
'net.peer.name': 'localhost',
85+
'net.peer.port': 3306,
86+
'db.user': 'root',
87+
}),
88+
}),
89+
]),
90+
};
91+
92+
createRunner(__dirname, 'scenario-withoutConnect.js').expect({ transaction: EXPECTED_TRANSACTION }).start(done);
93+
});
94+
});

0 commit comments

Comments
 (0)