Skip to content

fix(node): Improve mysql integration #8923

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
Sep 1, 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
@@ -0,0 +1,35 @@
import { assertSentryTransaction, TestEnv } from '../../../../../utils';

test('should auto-instrument `mysql` package when using connection.connect()', async () => {
const env = await TestEnv.init(__dirname);
const envelope = await env.getEnvelopeRequest({ envelopeType: 'transaction' });

expect(envelope).toHaveLength(3);

assertSentryTransaction(envelope[2], {
transaction: 'Test Transaction',
spans: [
{
description: 'SELECT 1 + 1 AS solution',
op: 'db',
data: {
'db.system': 'mysql',
'server.address': 'localhost',
'server.port': 3306,
'db.user': 'root',
},
},

{
description: 'SELECT NOW()',
op: 'db',
data: {
'db.system': 'mysql',
'server.address': 'localhost',
'server.port': 3306,
'db.user': 'root',
},
},
],
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import * as Sentry from '@sentry/node';
import mysql from 'mysql';

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

const connection = mysql.createConnection({
user: 'root',

Check failure

Code scanning / CodeQL

Hard-coded credentials

The hard-coded value "root" is used as [user name](1).
password: 'docker',

Check failure

Code scanning / CodeQL

Hard-coded credentials

The hard-coded value "docker" is used as [password](1).
});

const transaction = Sentry.startTransaction({
op: 'transaction',
name: 'Test Transaction',
});

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

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

// Wait a bit to ensure the queries completed
setTimeout(() => {
transaction.finish();
}, 500);
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { assertSentryTransaction, TestEnv } from '../../../../../utils';

test('should auto-instrument `mysql` package when using query without callback', async () => {
const env = await TestEnv.init(__dirname);
const envelope = await env.getEnvelopeRequest({ envelopeType: 'transaction' });

expect(envelope).toHaveLength(3);

assertSentryTransaction(envelope[2], {
transaction: 'Test Transaction',
spans: [
{
description: 'SELECT 1 + 1 AS solution',
op: 'db',
data: {
'db.system': 'mysql',
'server.address': 'localhost',
'server.port': 3306,
'db.user': 'root',
},
},

{
description: 'SELECT NOW()',
op: 'db',
data: {
'db.system': 'mysql',
'server.address': 'localhost',
'server.port': 3306,
'db.user': 'root',
},
},
],
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import * as Sentry from '@sentry/node';
import mysql from 'mysql';

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

const connection = mysql.createConnection({
user: 'root',

Check failure

Code scanning / CodeQL

Hard-coded credentials

The hard-coded value "root" is used as [user name](1).
password: 'docker',

Check failure

Code scanning / CodeQL

Hard-coded credentials

The hard-coded value "docker" is used as [password](1).
});

const transaction = Sentry.startTransaction({
op: 'transaction',
name: 'Test Transaction',
});

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

connection.query('SELECT 1 + 1 AS solution', function () {
connection.query('SELECT NOW()', ['1', '2'], () => {
if (transaction) transaction.finish();
connection.end();
});
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { assertSentryTransaction, TestEnv } from '../../../../utils';
import { assertSentryTransaction, TestEnv } from '../../../../../utils';

test('should auto-instrument `mysql` package.', async () => {
test('should auto-instrument `mysql` package without connection.connect()', async () => {
const env = await TestEnv.init(__dirname);
const envelope = await env.getEnvelopeRequest({ envelopeType: 'transaction' });

Expand All @@ -14,6 +14,9 @@ test('should auto-instrument `mysql` package.', async () => {
op: 'db',
data: {
'db.system': 'mysql',
'server.address': 'localhost',
'server.port': 3306,
'db.user': 'root',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just to sanity-check (becaus afaict assertSentryTransaction doesn't require strict equality): We don't collect something like db.password, rigth? 😅

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nope, we do not :D

},
},

Expand All @@ -22,6 +25,9 @@ test('should auto-instrument `mysql` package.', async () => {
op: 'db',
data: {
'db.system': 'mysql',
'server.address': 'localhost',
'server.port': 3306,
'db.user': 'root',
},
},
],
Expand Down
25 changes: 20 additions & 5 deletions packages/tracing-internal/src/node/integrations/mysql.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Hub } from '@sentry/core';
import type { EventProcessor } from '@sentry/types';
import type { EventProcessor, Span } from '@sentry/types';
import { fill, loadModule, logger } from '@sentry/utils';

import type { LazyLoadedIntegration } from './lazy';
Expand Down Expand Up @@ -83,6 +83,19 @@ export class Mysql implements LazyLoadedIntegration<MysqlConnection> {
};
}

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

const data = spanDataFromConfig();
Object.keys(data).forEach(key => {
span.setData(key, data[key]);
});

span.finish();
}

// The original function will have one of these signatures:
// function (callback) => void
// function (options, callback) => void
Expand All @@ -91,31 +104,33 @@ export class Mysql implements LazyLoadedIntegration<MysqlConnection> {
return function (this: unknown, options: unknown, values: unknown, callback: unknown) {
const scope = getCurrentHub().getScope();
const parentSpan = scope?.getSpan();

const span = parentSpan?.startChild({
description: typeof options === 'string' ? options : (options as { sql: string }).sql,
op: 'db',
origin: 'auto.db.mysql',
data: {
...spanDataFromConfig(),
'db.system': 'mysql',
},
});

if (typeof callback === 'function') {
return orig.call(this, options, values, function (err: Error, result: unknown, fields: unknown) {
span?.finish();
finishSpan(span);
callback(err, result, fields);
});
}

if (typeof values === 'function') {
return orig.call(this, options, function (err: Error, result: unknown, fields: unknown) {
span?.finish();
finishSpan(span);
values(err, result, fields);
});
}

return orig.call(this, options, values, callback);
return orig.call(this, options, values, function () {
finishSpan(span);
});
};
});
}
Expand Down