utils.tsx 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. import type {Location, LocationDescriptorObject} from 'history';
  2. import {PAGE_URL_PARAM} from 'sentry/constants/pageFilters';
  3. import type {Organization} from 'sentry/types';
  4. import {getTimeStampFromTableDateField} from 'sentry/utils/dates';
  5. import type {
  6. EventLite,
  7. TraceError,
  8. TraceFull,
  9. TraceFullDetailed,
  10. TraceSplitResults,
  11. } from 'sentry/utils/performance/quickTrace/types';
  12. import {isTraceSplitResult, reduceTrace} from 'sentry/utils/performance/quickTrace/utils';
  13. import normalizeUrl from 'sentry/utils/url/normalizeUrl';
  14. import {DEFAULT_TRACE_ROWS_LIMIT} from './limitExceededMessage';
  15. import type {TraceInfo} from './types';
  16. export function getTraceDetailsUrl({
  17. organization,
  18. traceSlug,
  19. dateSelection,
  20. timestamp,
  21. spanId,
  22. eventId,
  23. demo,
  24. location,
  25. source,
  26. }: {
  27. dateSelection;
  28. location: Location;
  29. organization: Organization;
  30. traceSlug: string;
  31. demo?: string;
  32. eventId?: string;
  33. source?: string;
  34. spanId?: string;
  35. timestamp?: string | number;
  36. }): LocationDescriptorObject {
  37. const {start, end, statsPeriod} = dateSelection;
  38. const queryParams = {
  39. ...location.query,
  40. statsPeriod,
  41. [PAGE_URL_PARAM.PAGE_START]: start,
  42. [PAGE_URL_PARAM.PAGE_END]: end,
  43. };
  44. const oldTraceUrl = {
  45. pathname: normalizeUrl(
  46. `/organizations/${organization.slug}/performance/trace/${traceSlug}/`
  47. ),
  48. query: queryParams,
  49. };
  50. if (shouldForceRouteToOldView(organization, timestamp)) {
  51. return oldTraceUrl;
  52. }
  53. if (organization.features.includes('trace-view-v1')) {
  54. if (spanId) {
  55. queryParams.node = [`span-${spanId}`, `txn-${eventId}`];
  56. }
  57. return {
  58. pathname: normalizeUrl(
  59. `/organizations/${organization.slug}/performance/trace/${traceSlug}/`
  60. ),
  61. query: {
  62. ...queryParams,
  63. timestamp: getTimeStampFromTableDateField(timestamp),
  64. eventId,
  65. demo,
  66. source,
  67. },
  68. };
  69. }
  70. if (organization.features.includes('trace-view-load-more')) {
  71. queryParams.limit = DEFAULT_TRACE_ROWS_LIMIT;
  72. }
  73. return oldTraceUrl;
  74. }
  75. /**
  76. * Single tenant, on-premise etc. users may not have span extraction enabled.
  77. *
  78. * This code can be removed at the time we're sure all STs have rolled out span extraction.
  79. */
  80. export function shouldForceRouteToOldView(
  81. organization: Organization,
  82. timestamp: string | number | undefined
  83. ) {
  84. const usableTimestamp = getTimeStampFromTableDateField(timestamp);
  85. if (!usableTimestamp) {
  86. // Timestamps must always be provided for the new view, if it doesn't exist, fall back to the old view.
  87. return true;
  88. }
  89. return (
  90. organization.extraOptions?.traces.checkSpanExtractionDate &&
  91. organization.extraOptions?.traces.spansExtractionDate <= usableTimestamp
  92. );
  93. }
  94. function transactionVisitor() {
  95. return (accumulator: TraceInfo, event: TraceFullDetailed) => {
  96. for (const error of event.errors ?? []) {
  97. accumulator.errors.add(error.event_id);
  98. }
  99. for (const performanceIssue of event.performance_issues ?? []) {
  100. accumulator.performanceIssues.add(performanceIssue.event_id);
  101. }
  102. accumulator.transactions.add(event.event_id);
  103. accumulator.projects.add(event.project_slug);
  104. accumulator.startTimestamp = Math.min(
  105. accumulator.startTimestamp,
  106. event.start_timestamp
  107. );
  108. accumulator.endTimestamp = Math.max(accumulator.endTimestamp, event.timestamp);
  109. accumulator.maxGeneration = Math.max(accumulator.maxGeneration, event.generation);
  110. return accumulator;
  111. };
  112. }
  113. export function hasTraceData(
  114. traces: TraceFullDetailed[] | null | undefined,
  115. orphanErrors: TraceError[] | undefined
  116. ): boolean {
  117. return Boolean(
  118. (traces && traces.length > 0) || (orphanErrors && orphanErrors.length > 0)
  119. );
  120. }
  121. export function getTraceSplitResults<U extends TraceFullDetailed | TraceFull | EventLite>(
  122. trace: TraceSplitResults<U> | U[],
  123. organization: Organization
  124. ) {
  125. let transactions: U[] | undefined;
  126. let orphanErrors: TraceError[] | undefined;
  127. if (
  128. trace &&
  129. organization.features.includes('performance-tracing-without-performance') &&
  130. isTraceSplitResult<TraceSplitResults<U>, U[]>(trace)
  131. ) {
  132. orphanErrors = trace.orphan_errors;
  133. transactions = trace.transactions;
  134. }
  135. return {transactions, orphanErrors};
  136. }
  137. export function getTraceInfo(
  138. traces: TraceFullDetailed[] = [],
  139. orphanErrors: TraceError[] = []
  140. ) {
  141. const initial = {
  142. projects: new Set<string>(),
  143. errors: new Set<string>(),
  144. performanceIssues: new Set<string>(),
  145. transactions: new Set<string>(),
  146. startTimestamp: Number.MAX_SAFE_INTEGER,
  147. endTimestamp: 0,
  148. maxGeneration: 0,
  149. trailingOrphansCount: 0,
  150. };
  151. const transactionsInfo = traces.reduce(
  152. (info: TraceInfo, trace: TraceFullDetailed) =>
  153. reduceTrace<TraceInfo>(trace, transactionVisitor(), info),
  154. initial
  155. );
  156. // Accumulate orphan error information.
  157. return orphanErrors.reduce((accumulator: TraceInfo, event: TraceError) => {
  158. accumulator.errors.add(event.event_id);
  159. accumulator.trailingOrphansCount++;
  160. if (event.timestamp) {
  161. accumulator.startTimestamp = Math.min(accumulator.startTimestamp, event.timestamp);
  162. accumulator.endTimestamp = Math.max(accumulator.endTimestamp, event.timestamp);
  163. }
  164. return accumulator;
  165. }, transactionsInfo);
  166. }
  167. export function shortenErrorTitle(title: string): string {
  168. return title.split(':')[0];
  169. }
  170. export function isRootTransaction(trace: TraceFullDetailed): boolean {
  171. // Root transactions has no parent_span_id
  172. return trace.parent_span_id === null;
  173. }