Skip to content

fix(node): Ensure mysql integration works without callback #9222

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 1 commit into from
Oct 11, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ const connection = mysql.createConnection({
password: 'docker',
});

connection.connect(function (err: unknown) {
if (err) {
return;
}
});

const transaction = Sentry.startTransaction({
op: 'transaction',
name: 'Test Transaction',
Expand All @@ -22,10 +28,18 @@ Sentry.configureScope(scope => {
scope.setSpan(transaction);
});

connection.query('SELECT 1 + 1 AS solution');
connection.query('SELECT NOW()', ['1', '2']);
const query = connection.query('SELECT 1 + 1 AS solution');
const query2 = connection.query('SELECT NOW()', ['1', '2']);

query.on('end', () => {
transaction.setTag('result_done', 'yes');

// Wait a bit to ensure the queries completed
setTimeout(() => {
transaction.finish();
}, 500);
query2.on('end', () => {
transaction.setTag('result_done2', 'yes');

// Wait a bit to ensure the queries completed
setTimeout(() => {
transaction.finish();
}, 500);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ test('should auto-instrument `mysql` package when using query without callback',

assertSentryTransaction(envelope[2], {
transaction: 'Test Transaction',
tags: {
result_done: 'yes',
result_done2: 'yes',
},
spans: [
{
description: 'SELECT 1 + 1 AS solution',
Expand Down
9 changes: 7 additions & 2 deletions packages/tracing-internal/src/node/integrations/mysql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export class Mysql implements LazyLoadedIntegration<MysqlConnection> {
}

function finishSpan(span: Span | undefined): void {
if (!span) {
if (!span || span.endTimestamp) {
return;
}

Expand Down Expand Up @@ -128,9 +128,14 @@ export class Mysql implements LazyLoadedIntegration<MysqlConnection> {
});
}

return orig.call(this, options, values, function () {
// streaming, no callback!
const query = orig.call(this, options, values) as { on: (event: string, callback: () => void) => void };

query.on('end', () => {
finishSpan(span);
});

return query;
};
});
}
Expand Down