-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: node-mysql tracing integration * Use // instead of /** */ * fix: Require mysql.Connection directly through mysql/lib/Connection.js * ref: Align span description Co-authored-by: Katie Byers <katie.byers@sentry.io> Co-authored-by: Daniel Griesser <daniel.griesser.86@gmail.com>
- Loading branch information
1 parent
f7fc733
commit a1934b4
Showing
2 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export { Express } from './express'; | ||
export { Mysql } from './mysql'; | ||
export { Mongo } from './mongo'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { Hub } from '@sentry/hub'; | ||
import { EventProcessor, Integration } from '@sentry/types'; | ||
import { dynamicRequire, fill, logger } from '@sentry/utils'; | ||
|
||
interface MysqlConnection { | ||
prototype: { | ||
query: () => 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 { | ||
let connection: MysqlConnection; | ||
|
||
try { | ||
// Unfortunatelly mysql is using some custom loading system and `Connection` is not exported directly. | ||
connection = dynamicRequire(module, 'mysql/lib/Connection.js'); | ||
} catch (e) { | ||
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(connection.prototype, 'query', 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); | ||
}; | ||
}); | ||
} | ||
} |