Skip to content

Commit 0fe030b

Browse files
committed
feat: node-mysql tracing integration
1 parent c15967e commit 0fe030b

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export { Express } from './express';
2+
export { Mysql } from './mysql';
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import { Hub } from '@sentry/hub';
2+
import { EventProcessor, Integration } from '@sentry/types';
3+
import { fill, logger } from '@sentry/utils';
4+
5+
interface MysqlConnection {
6+
prototype: {
7+
query: () => void;
8+
};
9+
}
10+
11+
/** Tracing integration for node-mysql package */
12+
export class Mysql implements Integration {
13+
/**
14+
* @inheritDoc
15+
*/
16+
public static id: string = 'Mysql';
17+
18+
/**
19+
* @inheritDoc
20+
*/
21+
public name: string = Mysql.id;
22+
23+
/**
24+
* @inheritDoc
25+
*/
26+
public setupOnce(_: (callback: EventProcessor) => void, getCurrentHub: () => Hub): void {
27+
let connection: MysqlConnection;
28+
29+
try {
30+
/* eslint-disable @typescript-eslint/no-var-requires, @typescript-eslint/no-unsafe-member-access */
31+
const mysqlModule = require('mysql');
32+
const conn = mysqlModule.createConnection({});
33+
connection = conn.constructor;
34+
conn.end();
35+
/* eslint-enable @typescript-eslint/no-var-requires, @typescript-eslint/no-unsafe-member-access */
36+
} catch (e) {
37+
logger.error('Mysql Integration was unable to require `mysql` package.');
38+
return;
39+
}
40+
41+
/**
42+
* function (query, callback) => void
43+
* function (query, params, callback) => void
44+
* function (query) => Promise
45+
* function (query, params) => Promise
46+
*/
47+
fill(connection, 'query', function(orig: () => void | Promise<unknown>) {
48+
return function(this: unknown, options: unknown, values: unknown, callback: unknown) {
49+
const scope = getCurrentHub().getScope();
50+
const transaction = scope?.getTransaction();
51+
const span = transaction?.startChild({
52+
description: typeof options === 'string' ? options : (options as { sql: string }).sql,
53+
op: `query`,
54+
});
55+
56+
if (typeof callback === 'function') {
57+
return orig.call(this, options, values, function(err: Error, result: unknown, fields: unknown) {
58+
span?.finish();
59+
callback(err, result, fields);
60+
});
61+
}
62+
63+
if (typeof values === 'function') {
64+
return orig.call(this, options, function(err: Error, result: unknown, fields: unknown) {
65+
span?.finish();
66+
values(err, result, fields);
67+
});
68+
}
69+
70+
return orig.call(this, options, values, callback);
71+
};
72+
});
73+
}
74+
}

0 commit comments

Comments
 (0)