-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat: MySQL Tracing Support #3088
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
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
bdcc8fc
feat: node-mysql tracing integration
kamilogorek 09566ed
Use // instead of /** */
lobsterkatie 949c724
Merge branch 'master' into node-mysql
lobsterkatie d5c2464
fix: Require mysql.Connection directly through mysql/lib/Connection.js
kamilogorek 4b502fe
ref: Align span description
HazAT 1df0a02
Merge branch 'master' into node-mysql
HazAT File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 +1,2 @@ | ||
export { Express } from './express'; | ||
export { Mysql } from './mysql'; |
This file contains hidden or 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,75 @@ | ||
import { Hub } from '@sentry/hub'; | ||
import { EventProcessor, Integration } from '@sentry/types'; | ||
import { dynamicRequire, fill, logger } from '@sentry/utils'; | ||
|
||
interface MysqlConnection { | ||
end: () => void; | ||
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 { | ||
const mysqlModule = dynamicRequire(module, 'mysql') as { | ||
createConnection: (options: unknown) => MysqlConnection; | ||
}; | ||
const conn = mysqlModule.createConnection({}); | ||
connection = (conn.constructor as unknown) as MysqlConnection; | ||
conn.end(); | ||
} catch (e) { | ||
logger.error('Mysql Integration was unable to require `mysql` package.'); | ||
return; | ||
} | ||
|
||
/** | ||
* function (query, callback) => void | ||
* function (query, params, callback) => void | ||
* function (query) => Promise | ||
* function (query, params) => Promise | ||
*/ | ||
lobsterkatie marked this conversation as resolved.
Show resolved
Hide resolved
|
||
fill(connection, 'query', function(orig: () => void | Promise<unknown>) { | ||
return function(this: unknown, options: unknown, values: unknown, callback: unknown) { | ||
const scope = getCurrentHub().getScope(); | ||
const transaction = scope?.getTransaction(); | ||
const span = transaction?.startChild({ | ||
description: typeof options === 'string' ? options : (options as { sql: string }).sql, | ||
op: `query`, | ||
}); | ||
|
||
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); | ||
}; | ||
}); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.