integration.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. Object.defineProperty(exports, '__esModule', { value: true });
  2. const core = require('@sentry/core');
  3. const utils$1 = require('@sentry/utils');
  4. const debugBuild = require('../debug-build.js');
  5. const hubextensions = require('./hubextensions.js');
  6. const utils = require('./utils.js');
  7. const INTEGRATION_NAME = 'BrowserProfiling';
  8. const _browserProfilingIntegration = (() => {
  9. return {
  10. name: INTEGRATION_NAME,
  11. // TODO v8: Remove this
  12. setupOnce() {}, // eslint-disable-line @typescript-eslint/no-empty-function
  13. setup(client) {
  14. const scope = core.getCurrentScope();
  15. // eslint-disable-next-line deprecation/deprecation
  16. const transaction = scope.getTransaction();
  17. if (transaction && utils.isAutomatedPageLoadTransaction(transaction)) {
  18. if (utils.shouldProfileTransaction(transaction)) {
  19. hubextensions.startProfileForTransaction(transaction);
  20. }
  21. }
  22. if (typeof client.on !== 'function') {
  23. utils$1.logger.warn('[Profiling] Client does not support hooks, profiling will be disabled');
  24. return;
  25. }
  26. client.on('startTransaction', (transaction) => {
  27. if (utils.shouldProfileTransaction(transaction)) {
  28. hubextensions.startProfileForTransaction(transaction);
  29. }
  30. });
  31. client.on('beforeEnvelope', (envelope) => {
  32. // if not profiles are in queue, there is nothing to add to the envelope.
  33. if (!utils.getActiveProfilesCount()) {
  34. return;
  35. }
  36. const profiledTransactionEvents = utils.findProfiledTransactionsFromEnvelope(envelope);
  37. if (!profiledTransactionEvents.length) {
  38. return;
  39. }
  40. const profilesToAddToEnvelope = [];
  41. for (const profiledTransaction of profiledTransactionEvents) {
  42. const context = profiledTransaction && profiledTransaction.contexts;
  43. const profile_id = context && context['profile'] && context['profile']['profile_id'];
  44. const start_timestamp = context && context['profile'] && context['profile']['start_timestamp'];
  45. if (typeof profile_id !== 'string') {
  46. debugBuild.DEBUG_BUILD && utils$1.logger.log('[Profiling] cannot find profile for a transaction without a profile context');
  47. continue;
  48. }
  49. if (!profile_id) {
  50. debugBuild.DEBUG_BUILD && utils$1.logger.log('[Profiling] cannot find profile for a transaction without a profile context');
  51. continue;
  52. }
  53. // Remove the profile from the transaction context before sending, relay will take care of the rest.
  54. if (context && context['profile']) {
  55. delete context.profile;
  56. }
  57. const profile = utils.takeProfileFromGlobalCache(profile_id);
  58. if (!profile) {
  59. debugBuild.DEBUG_BUILD && utils$1.logger.log(`[Profiling] Could not retrieve profile for transaction: ${profile_id}`);
  60. continue;
  61. }
  62. const profileEvent = utils.createProfilingEvent(
  63. profile_id,
  64. start_timestamp ,
  65. profile,
  66. profiledTransaction ,
  67. );
  68. if (profileEvent) {
  69. profilesToAddToEnvelope.push(profileEvent);
  70. }
  71. }
  72. utils.addProfilesToEnvelope(envelope , profilesToAddToEnvelope);
  73. });
  74. },
  75. };
  76. }) ;
  77. const browserProfilingIntegration = core.defineIntegration(_browserProfilingIntegration);
  78. /**
  79. * Browser profiling integration. Stores any event that has contexts["profile"]["profile_id"]
  80. * This exists because we do not want to await async profiler.stop calls as transaction.finish is called
  81. * in a synchronous context. Instead, we handle sending the profile async from the promise callback and
  82. * rely on being able to pull the event from the cache when we need to construct the envelope. This makes the
  83. * integration less reliable as we might be dropping profiles when the cache is full.
  84. *
  85. * @experimental
  86. * @deprecated Use `browserProfilingIntegration()` instead.
  87. */
  88. // eslint-disable-next-line deprecation/deprecation
  89. const BrowserProfilingIntegration = core.convertIntegrationFnToClass(
  90. INTEGRATION_NAME,
  91. browserProfilingIntegration,
  92. ) ;
  93. // eslint-disable-next-line deprecation/deprecation
  94. exports.BrowserProfilingIntegration = BrowserProfilingIntegration;
  95. exports.browserProfilingIntegration = browserProfilingIntegration;
  96. //# sourceMappingURL=integration.js.map