mysql.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. var {
  2. _optionalChain
  3. } = require('@sentry/utils');
  4. Object.defineProperty(exports, '__esModule', { value: true });
  5. const utils = require('@sentry/utils');
  6. const debugBuild = require('../../common/debug-build.js');
  7. const nodeUtils = require('./utils/node-utils.js');
  8. /** Tracing integration for node-mysql package */
  9. class Mysql {
  10. /**
  11. * @inheritDoc
  12. */
  13. static __initStatic() {this.id = 'Mysql';}
  14. /**
  15. * @inheritDoc
  16. */
  17. constructor() {
  18. this.name = Mysql.id;
  19. }
  20. /** @inheritdoc */
  21. loadDependency() {
  22. return (this._module = this._module || utils.loadModule('mysql/lib/Connection.js'));
  23. }
  24. /**
  25. * @inheritDoc
  26. */
  27. setupOnce(_, getCurrentHub) {
  28. if (nodeUtils.shouldDisableAutoInstrumentation(getCurrentHub)) {
  29. debugBuild.DEBUG_BUILD && utils.logger.log('Mysql Integration is skipped because of instrumenter configuration.');
  30. return;
  31. }
  32. const pkg = this.loadDependency();
  33. if (!pkg) {
  34. debugBuild.DEBUG_BUILD && utils.logger.error('Mysql Integration was unable to require `mysql` package.');
  35. return;
  36. }
  37. let mySqlConfig = undefined;
  38. try {
  39. pkg.prototype.connect = new Proxy(pkg.prototype.connect, {
  40. apply(wrappingTarget, thisArg, args) {
  41. if (!mySqlConfig) {
  42. mySqlConfig = thisArg.config;
  43. }
  44. return wrappingTarget.apply(thisArg, args);
  45. },
  46. });
  47. } catch (e) {
  48. debugBuild.DEBUG_BUILD && utils.logger.error('Mysql Integration was unable to instrument `mysql` config.');
  49. }
  50. function spanDataFromConfig() {
  51. if (!mySqlConfig) {
  52. return {};
  53. }
  54. return {
  55. 'server.address': mySqlConfig.host,
  56. 'server.port': mySqlConfig.port,
  57. 'db.user': mySqlConfig.user,
  58. };
  59. }
  60. function finishSpan(span) {
  61. if (!span) {
  62. return;
  63. }
  64. const data = spanDataFromConfig();
  65. Object.keys(data).forEach(key => {
  66. span.setAttribute(key, data[key]);
  67. });
  68. span.end();
  69. }
  70. // The original function will have one of these signatures:
  71. // function (callback) => void
  72. // function (options, callback) => void
  73. // function (options, values, callback) => void
  74. utils.fill(pkg, 'createQuery', function (orig) {
  75. return function ( options, values, callback) {
  76. // eslint-disable-next-line deprecation/deprecation
  77. const scope = getCurrentHub().getScope();
  78. // eslint-disable-next-line deprecation/deprecation
  79. const parentSpan = scope.getSpan();
  80. // eslint-disable-next-line deprecation/deprecation
  81. const span = _optionalChain([parentSpan, 'optionalAccess', _2 => _2.startChild, 'call', _3 => _3({
  82. description: typeof options === 'string' ? options : (options ).sql,
  83. op: 'db',
  84. origin: 'auto.db.mysql',
  85. data: {
  86. 'db.system': 'mysql',
  87. },
  88. })]);
  89. if (typeof callback === 'function') {
  90. return orig.call(this, options, values, function (err, result, fields) {
  91. finishSpan(span);
  92. callback(err, result, fields);
  93. });
  94. }
  95. if (typeof values === 'function') {
  96. return orig.call(this, options, function (err, result, fields) {
  97. finishSpan(span);
  98. values(err, result, fields);
  99. });
  100. }
  101. // streaming, no callback!
  102. const query = orig.call(this, options, values) ;
  103. query.on('end', () => {
  104. finishSpan(span);
  105. });
  106. return query;
  107. };
  108. });
  109. }
  110. }Mysql.__initStatic();
  111. exports.Mysql = Mysql;
  112. //# sourceMappingURL=mysql.js.map