hubextensions.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. Object.defineProperty(exports, '__esModule', { value: true });
  2. const utils = require('@sentry/utils');
  3. const debugBuild = require('../debug-build.js');
  4. const hub = require('../hub.js');
  5. const spanUtils = require('../utils/spanUtils.js');
  6. const errors = require('./errors.js');
  7. const idletransaction = require('./idletransaction.js');
  8. const sampling = require('./sampling.js');
  9. const transaction = require('./transaction.js');
  10. /** Returns all trace headers that are currently on the top scope. */
  11. function traceHeaders() {
  12. // eslint-disable-next-line deprecation/deprecation
  13. const scope = this.getScope();
  14. // eslint-disable-next-line deprecation/deprecation
  15. const span = scope.getSpan();
  16. return span
  17. ? {
  18. 'sentry-trace': spanUtils.spanToTraceHeader(span),
  19. }
  20. : {};
  21. }
  22. /**
  23. * Creates a new transaction and adds a sampling decision if it doesn't yet have one.
  24. *
  25. * The Hub.startTransaction method delegates to this method to do its work, passing the Hub instance in as `this`, as if
  26. * it had been called on the hub directly. Exists as a separate function so that it can be injected into the class as an
  27. * "extension method."
  28. *
  29. * @param this: The Hub starting the transaction
  30. * @param transactionContext: Data used to configure the transaction
  31. * @param CustomSamplingContext: Optional data to be provided to the `tracesSampler` function (if any)
  32. *
  33. * @returns The new transaction
  34. *
  35. * @see {@link Hub.startTransaction}
  36. */
  37. function _startTransaction(
  38. transactionContext,
  39. customSamplingContext,
  40. ) {
  41. // eslint-disable-next-line deprecation/deprecation
  42. const client = this.getClient();
  43. const options = (client && client.getOptions()) || {};
  44. const configInstrumenter = options.instrumenter || 'sentry';
  45. const transactionInstrumenter = transactionContext.instrumenter || 'sentry';
  46. if (configInstrumenter !== transactionInstrumenter) {
  47. debugBuild.DEBUG_BUILD &&
  48. utils.logger.error(
  49. `A transaction was started with instrumenter=\`${transactionInstrumenter}\`, but the SDK is configured with the \`${configInstrumenter}\` instrumenter.
  50. The transaction will not be sampled. Please use the ${configInstrumenter} instrumentation to start transactions.`,
  51. );
  52. // eslint-disable-next-line deprecation/deprecation
  53. transactionContext.sampled = false;
  54. }
  55. // eslint-disable-next-line deprecation/deprecation
  56. let transaction$1 = new transaction.Transaction(transactionContext, this);
  57. transaction$1 = sampling.sampleTransaction(transaction$1, options, {
  58. name: transactionContext.name,
  59. parentSampled: transactionContext.parentSampled,
  60. transactionContext,
  61. attributes: {
  62. // eslint-disable-next-line deprecation/deprecation
  63. ...transactionContext.data,
  64. ...transactionContext.attributes,
  65. },
  66. ...customSamplingContext,
  67. });
  68. if (transaction$1.isRecording()) {
  69. transaction$1.initSpanRecorder(options._experiments && (options._experiments.maxSpans ));
  70. }
  71. if (client && client.emit) {
  72. client.emit('startTransaction', transaction$1);
  73. }
  74. return transaction$1;
  75. }
  76. /**
  77. * Create new idle transaction.
  78. */
  79. function startIdleTransaction(
  80. hub,
  81. transactionContext,
  82. idleTimeout,
  83. finalTimeout,
  84. onScope,
  85. customSamplingContext,
  86. heartbeatInterval,
  87. delayAutoFinishUntilSignal = false,
  88. ) {
  89. // eslint-disable-next-line deprecation/deprecation
  90. const client = hub.getClient();
  91. const options = (client && client.getOptions()) || {};
  92. // eslint-disable-next-line deprecation/deprecation
  93. let transaction = new idletransaction.IdleTransaction(
  94. transactionContext,
  95. hub,
  96. idleTimeout,
  97. finalTimeout,
  98. heartbeatInterval,
  99. onScope,
  100. delayAutoFinishUntilSignal,
  101. );
  102. transaction = sampling.sampleTransaction(transaction, options, {
  103. name: transactionContext.name,
  104. parentSampled: transactionContext.parentSampled,
  105. transactionContext,
  106. attributes: {
  107. // eslint-disable-next-line deprecation/deprecation
  108. ...transactionContext.data,
  109. ...transactionContext.attributes,
  110. },
  111. ...customSamplingContext,
  112. });
  113. if (transaction.isRecording()) {
  114. transaction.initSpanRecorder(options._experiments && (options._experiments.maxSpans ));
  115. }
  116. if (client && client.emit) {
  117. client.emit('startTransaction', transaction);
  118. }
  119. return transaction;
  120. }
  121. /**
  122. * Adds tracing extensions to the global hub.
  123. */
  124. function addTracingExtensions() {
  125. const carrier = hub.getMainCarrier();
  126. if (!carrier.__SENTRY__) {
  127. return;
  128. }
  129. carrier.__SENTRY__.extensions = carrier.__SENTRY__.extensions || {};
  130. if (!carrier.__SENTRY__.extensions.startTransaction) {
  131. carrier.__SENTRY__.extensions.startTransaction = _startTransaction;
  132. }
  133. if (!carrier.__SENTRY__.extensions.traceHeaders) {
  134. carrier.__SENTRY__.extensions.traceHeaders = traceHeaders;
  135. }
  136. errors.registerErrorInstrumentation();
  137. }
  138. exports.addTracingExtensions = addTracingExtensions;
  139. exports.startIdleTransaction = startIdleTransaction;
  140. //# sourceMappingURL=hubextensions.js.map