postgres.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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-postgres package */
  9. class Postgres {
  10. /**
  11. * @inheritDoc
  12. */
  13. static __initStatic() {this.id = 'Postgres';}
  14. /**
  15. * @inheritDoc
  16. */
  17. constructor(options = {}) {
  18. this.name = Postgres.id;
  19. this._usePgNative = !!options.usePgNative;
  20. this._module = options.module;
  21. }
  22. /** @inheritdoc */
  23. loadDependency() {
  24. return (this._module = this._module || utils.loadModule('pg'));
  25. }
  26. /**
  27. * @inheritDoc
  28. */
  29. setupOnce(_, getCurrentHub) {
  30. if (nodeUtils.shouldDisableAutoInstrumentation(getCurrentHub)) {
  31. debugBuild.DEBUG_BUILD && utils.logger.log('Postgres Integration is skipped because of instrumenter configuration.');
  32. return;
  33. }
  34. const pkg = this.loadDependency();
  35. if (!pkg) {
  36. debugBuild.DEBUG_BUILD && utils.logger.error('Postgres Integration was unable to require `pg` package.');
  37. return;
  38. }
  39. const Client = this._usePgNative ? _optionalChain([pkg, 'access', _2 => _2.native, 'optionalAccess', _3 => _3.Client]) : pkg.Client;
  40. if (!Client) {
  41. debugBuild.DEBUG_BUILD && utils.logger.error("Postgres Integration was unable to access 'pg-native' bindings.");
  42. return;
  43. }
  44. /**
  45. * function (query, callback) => void
  46. * function (query, params, callback) => void
  47. * function (query) => Promise
  48. * function (query, params) => Promise
  49. * function (pg.Cursor) => pg.Cursor
  50. */
  51. utils.fill(Client.prototype, 'query', function (orig) {
  52. return function ( config, values, callback) {
  53. // eslint-disable-next-line deprecation/deprecation
  54. const scope = getCurrentHub().getScope();
  55. // eslint-disable-next-line deprecation/deprecation
  56. const parentSpan = scope.getSpan();
  57. const data = {
  58. 'db.system': 'postgresql',
  59. };
  60. try {
  61. if (this.database) {
  62. data['db.name'] = this.database;
  63. }
  64. if (this.host) {
  65. data['server.address'] = this.host;
  66. }
  67. if (this.port) {
  68. data['server.port'] = this.port;
  69. }
  70. if (this.user) {
  71. data['db.user'] = this.user;
  72. }
  73. } catch (e) {
  74. // ignore
  75. }
  76. // eslint-disable-next-line deprecation/deprecation
  77. const span = _optionalChain([parentSpan, 'optionalAccess', _4 => _4.startChild, 'call', _5 => _5({
  78. description: typeof config === 'string' ? config : (config ).text,
  79. op: 'db',
  80. origin: 'auto.db.postgres',
  81. data,
  82. })]);
  83. if (typeof callback === 'function') {
  84. return orig.call(this, config, values, function (err, result) {
  85. _optionalChain([span, 'optionalAccess', _6 => _6.end, 'call', _7 => _7()]);
  86. callback(err, result);
  87. });
  88. }
  89. if (typeof values === 'function') {
  90. return orig.call(this, config, function (err, result) {
  91. _optionalChain([span, 'optionalAccess', _8 => _8.end, 'call', _9 => _9()]);
  92. values(err, result);
  93. });
  94. }
  95. const rv = typeof values !== 'undefined' ? orig.call(this, config, values) : orig.call(this, config);
  96. if (utils.isThenable(rv)) {
  97. return rv.then((res) => {
  98. _optionalChain([span, 'optionalAccess', _10 => _10.end, 'call', _11 => _11()]);
  99. return res;
  100. });
  101. }
  102. _optionalChain([span, 'optionalAccess', _12 => _12.end, 'call', _13 => _13()]);
  103. return rv;
  104. };
  105. });
  106. }
  107. }Postgres.__initStatic();
  108. exports.Postgres = Postgres;
  109. //# sourceMappingURL=postgres.js.map