/
mysql.ts
63 lines (54 loc) · 1.9 KB
/
mysql.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import { Hub } from '@sentry/hub';
import { EventProcessor, Integration } from '@sentry/types';
import { fill, loadModule, logger } from '@sentry/utils';
interface MysqlConnection {
createQuery: () => void;
}
/** Tracing integration for node-mysql package */
export class Mysql implements Integration {
/**
* @inheritDoc
*/
public static id: string = 'Mysql';
/**
* @inheritDoc
*/
public name: string = Mysql.id;
/**
* @inheritDoc
*/
public setupOnce(_: (callback: EventProcessor) => void, getCurrentHub: () => Hub): void {
const pkg = loadModule<MysqlConnection>('mysql/lib/Connection.js');
if (!pkg) {
logger.error('Mysql Integration was unable to require `mysql` package.');
return;
}
// The original function will have one of these signatures:
// function (callback) => void
// function (options, callback) => void
// function (options, values, callback) => void
fill(pkg, 'createQuery', function(orig: () => void) {
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`,
});
if (typeof callback === 'function') {
return orig.call(this, options, values, function(err: Error, result: unknown, fields: unknown) {
span?.finish();
callback(err, result, fields);
});
}
if (typeof values === 'function') {
return orig.call(this, options, function(err: Error, result: unknown, fields: unknown) {
span?.finish();
values(err, result, fields);
});
}
return orig.call(this, options, values, callback);
};
});
}
}