wintercg-fetch.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. Object.defineProperty(exports, '__esModule', { value: true });
  2. const tracing = require('@sentry-internal/tracing');
  3. const core = require('@sentry/core');
  4. const utils = require('@sentry/utils');
  5. const INTEGRATION_NAME = 'WinterCGFetch';
  6. const HAS_CLIENT_MAP = new WeakMap();
  7. const _winterCGFetch = ((options = {}) => {
  8. const breadcrumbs = options.breadcrumbs === undefined ? true : options.breadcrumbs;
  9. const shouldCreateSpanForRequest = options.shouldCreateSpanForRequest;
  10. const _createSpanUrlMap = new utils.LRUMap(100);
  11. const _headersUrlMap = new utils.LRUMap(100);
  12. const spans = {};
  13. /** Decides whether to attach trace data to the outgoing fetch request */
  14. function _shouldAttachTraceData(url) {
  15. const client = core.getClient();
  16. if (!client) {
  17. return false;
  18. }
  19. const clientOptions = client.getOptions();
  20. if (clientOptions.tracePropagationTargets === undefined) {
  21. return true;
  22. }
  23. const cachedDecision = _headersUrlMap.get(url);
  24. if (cachedDecision !== undefined) {
  25. return cachedDecision;
  26. }
  27. const decision = utils.stringMatchesSomePattern(url, clientOptions.tracePropagationTargets);
  28. _headersUrlMap.set(url, decision);
  29. return decision;
  30. }
  31. /** Helper that wraps shouldCreateSpanForRequest option */
  32. function _shouldCreateSpan(url) {
  33. if (shouldCreateSpanForRequest === undefined) {
  34. return true;
  35. }
  36. const cachedDecision = _createSpanUrlMap.get(url);
  37. if (cachedDecision !== undefined) {
  38. return cachedDecision;
  39. }
  40. const decision = shouldCreateSpanForRequest(url);
  41. _createSpanUrlMap.set(url, decision);
  42. return decision;
  43. }
  44. return {
  45. name: INTEGRATION_NAME,
  46. // TODO v8: Remove this again
  47. // eslint-disable-next-line @typescript-eslint/no-empty-function
  48. setupOnce() {
  49. utils.addFetchInstrumentationHandler(handlerData => {
  50. const client = core.getClient();
  51. if (!client || !HAS_CLIENT_MAP.get(client)) {
  52. return;
  53. }
  54. if (core.isSentryRequestUrl(handlerData.fetchData.url, client)) {
  55. return;
  56. }
  57. tracing.instrumentFetchRequest(
  58. handlerData,
  59. _shouldCreateSpan,
  60. _shouldAttachTraceData,
  61. spans,
  62. 'auto.http.wintercg_fetch',
  63. );
  64. if (breadcrumbs) {
  65. createBreadcrumb(handlerData);
  66. }
  67. });
  68. },
  69. setup(client) {
  70. HAS_CLIENT_MAP.set(client, true);
  71. },
  72. };
  73. }) ;
  74. const winterCGFetchIntegration = core.defineIntegration(_winterCGFetch);
  75. /**
  76. * Creates spans and attaches tracing headers to fetch requests on WinterCG runtimes.
  77. *
  78. * @deprecated Use `winterCGFetchIntegration()` instead.
  79. */
  80. // eslint-disable-next-line deprecation/deprecation
  81. const WinterCGFetch = core.convertIntegrationFnToClass(
  82. INTEGRATION_NAME,
  83. winterCGFetchIntegration,
  84. )
  85. ;
  86. // eslint-disable-next-line deprecation/deprecation
  87. function createBreadcrumb(handlerData) {
  88. const { startTimestamp, endTimestamp } = handlerData;
  89. // We only capture complete fetch requests
  90. if (!endTimestamp) {
  91. return;
  92. }
  93. if (handlerData.error) {
  94. const data = handlerData.fetchData;
  95. const hint = {
  96. data: handlerData.error,
  97. input: handlerData.args,
  98. startTimestamp,
  99. endTimestamp,
  100. };
  101. core.addBreadcrumb(
  102. {
  103. category: 'fetch',
  104. data,
  105. level: 'error',
  106. type: 'http',
  107. },
  108. hint,
  109. );
  110. } else {
  111. const data = {
  112. ...handlerData.fetchData,
  113. status_code: handlerData.response && handlerData.response.status,
  114. };
  115. const hint = {
  116. input: handlerData.args,
  117. response: handlerData.response,
  118. startTimestamp,
  119. endTimestamp,
  120. };
  121. core.addBreadcrumb(
  122. {
  123. category: 'fetch',
  124. data,
  125. type: 'http',
  126. },
  127. hint,
  128. );
  129. }
  130. }
  131. exports.WinterCGFetch = WinterCGFetch;
  132. exports.winterCGFetchIntegration = winterCGFetchIntegration;
  133. //# sourceMappingURL=wintercg-fetch.js.map