hubextensions.js 4.7 KB

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