| 28 | import * as path from 'path'; |
| 29 | |
| 30 | class MySQL2Plugin implements SwPlugin { |
| 31 | readonly module = 'mysql2'; |
| 32 | readonly versions = '*'; |
| 33 | |
| 34 | getVersion(installer: PluginInstaller): string { |
| 35 | // TODO: this method will not work in a bundle |
| 36 | try { |
| 37 | let indexPath = installer.resolve(this.module); |
| 38 | let packageJsonStr = fs.readFileSync(`${path.dirname(indexPath)}${path.sep}package.json`, { encoding: 'utf-8' }); |
| 39 | const pkg = JSON.parse(packageJsonStr); |
| 40 | return pkg.version; |
| 41 | } catch { |
| 42 | return ''; |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | install(installer: PluginInstaller): void { |
| 47 | const Connection = (installer.require?.('mysql2') ?? require('mysql2')).Connection; |
| 48 | const _query = Connection.prototype.query; |
| 49 | |
| 50 | Connection.prototype.query = function (sql: any, values: any, cb: any) { |
| 51 | let query: any; |
| 52 | |
| 53 | const host = `${this.config.host}:${this.config.port}`; |
| 54 | const span = ContextManager.current.newExitSpan('mysql/query', Component.MYSQL); |
| 55 | |
| 56 | span.start(); |
| 57 | |
| 58 | try { |
| 59 | span.component = Component.MYSQL; |
| 60 | span.layer = SpanLayer.DATABASE; |
| 61 | span.peer = host; |
| 62 | |
| 63 | span.tag(Tag.dbType('Mysql')); |
| 64 | span.tag(Tag.dbInstance(`${this.config.database}`)); |
| 65 | |
| 66 | let _sql: any; |
| 67 | let _values: any; |
| 68 | let streaming: any; |
| 69 | |
| 70 | if (typeof sql === 'function') { |
| 71 | sql = wrapCallback(span, sql, 0); |
| 72 | } else if (typeof sql === 'object') { |
| 73 | _sql = sql.sql; |
| 74 | |
| 75 | if (typeof values === 'function') { |
| 76 | values = wrapCallback(span, values, 0); |
| 77 | _values = sql.values; |
| 78 | } else if (values !== undefined) { |
| 79 | _values = values; |
| 80 | |
| 81 | if (typeof cb === 'function') { |
| 82 | cb = wrapCallback(span, cb, 0); |
| 83 | } else { |
| 84 | streaming = true; |
| 85 | } |
| 86 | } else { |
| 87 | streaming = true; |
nothing calls this directly
no outgoing calls
no test coverage detected