utils.tsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import type {LocationDescriptorObject, Query} from 'history';
  2. import {PAGE_URL_PARAM} from 'sentry/constants/pageFilters';
  3. import type {Organization, OrganizationSummary} from 'sentry/types';
  4. import type {
  5. EventLite,
  6. TraceError,
  7. TraceFull,
  8. TraceFullDetailed,
  9. TraceSplitResults,
  10. } from 'sentry/utils/performance/quickTrace/types';
  11. import {isTraceSplitResult, reduceTrace} from 'sentry/utils/performance/quickTrace/utils';
  12. import {DEFAULT_TRACE_ROWS_LIMIT} from './limitExceededMessage';
  13. import type {TraceInfo} from './types';
  14. export function getTraceDetailsUrl(
  15. organization: OrganizationSummary,
  16. traceSlug: string,
  17. dateSelection,
  18. query: Query,
  19. timestamp?: string | number,
  20. eventId?: string
  21. ): LocationDescriptorObject {
  22. const {start, end, statsPeriod} = dateSelection;
  23. const queryParams = {
  24. ...query,
  25. statsPeriod,
  26. [PAGE_URL_PARAM.PAGE_START]: start,
  27. [PAGE_URL_PARAM.PAGE_END]: end,
  28. };
  29. if (organization.features.includes('trace-view-v1')) {
  30. return {
  31. pathname: `/organizations/${organization.slug}/performance/trace/${traceSlug}/`,
  32. query: {
  33. ...queryParams,
  34. timestamp,
  35. eventId,
  36. },
  37. };
  38. }
  39. if (organization.features.includes('trace-view-load-more')) {
  40. queryParams.limit = DEFAULT_TRACE_ROWS_LIMIT;
  41. }
  42. return {
  43. pathname: `/organizations/${organization.slug}/performance/trace/${traceSlug}/`,
  44. query: queryParams,
  45. };
  46. }
  47. function transactionVisitor() {
  48. return (accumulator: TraceInfo, event: TraceFullDetailed) => {
  49. for (const error of event.errors ?? []) {
  50. accumulator.errors.add(error.event_id);
  51. }
  52. for (const performanceIssue of event.performance_issues ?? []) {
  53. accumulator.performanceIssues.add(performanceIssue.event_id);
  54. }
  55. accumulator.transactions.add(event.event_id);
  56. accumulator.projects.add(event.project_slug);
  57. accumulator.startTimestamp = Math.min(
  58. accumulator.startTimestamp,
  59. event.start_timestamp
  60. );
  61. accumulator.endTimestamp = Math.max(accumulator.endTimestamp, event.timestamp);
  62. accumulator.maxGeneration = Math.max(accumulator.maxGeneration, event.generation);
  63. return accumulator;
  64. };
  65. }
  66. export function hasTraceData(
  67. traces: TraceFullDetailed[] | null | undefined,
  68. orphanErrors: TraceError[] | undefined
  69. ): boolean {
  70. return Boolean(
  71. (traces && traces.length > 0) || (orphanErrors && orphanErrors.length > 0)
  72. );
  73. }
  74. export function getTraceSplitResults<U extends TraceFullDetailed | TraceFull | EventLite>(
  75. trace: TraceSplitResults<U> | U[],
  76. organization: Organization
  77. ) {
  78. let transactions: U[] | undefined;
  79. let orphanErrors: TraceError[] | undefined;
  80. if (
  81. trace &&
  82. organization.features.includes('performance-tracing-without-performance') &&
  83. isTraceSplitResult<TraceSplitResults<U>, U[]>(trace)
  84. ) {
  85. orphanErrors = trace.orphan_errors;
  86. transactions = trace.transactions;
  87. }
  88. return {transactions, orphanErrors};
  89. }
  90. export function getTraceInfo(
  91. traces: TraceFullDetailed[] = [],
  92. orphanErrors: TraceError[] = []
  93. ) {
  94. const initial = {
  95. projects: new Set<string>(),
  96. errors: new Set<string>(),
  97. performanceIssues: new Set<string>(),
  98. transactions: new Set<string>(),
  99. startTimestamp: Number.MAX_SAFE_INTEGER,
  100. endTimestamp: 0,
  101. maxGeneration: 0,
  102. trailingOrphansCount: 0,
  103. };
  104. const transactionsInfo = traces.reduce(
  105. (info: TraceInfo, trace: TraceFullDetailed) =>
  106. reduceTrace<TraceInfo>(trace, transactionVisitor(), info),
  107. initial
  108. );
  109. // Accumulate orphan error information.
  110. return orphanErrors.reduce((accumulator: TraceInfo, event: TraceError) => {
  111. accumulator.errors.add(event.event_id);
  112. accumulator.trailingOrphansCount++;
  113. if (event.timestamp) {
  114. accumulator.startTimestamp = Math.min(accumulator.startTimestamp, event.timestamp);
  115. accumulator.endTimestamp = Math.max(accumulator.endTimestamp, event.timestamp);
  116. }
  117. return accumulator;
  118. }, transactionsInfo);
  119. }
  120. export function shortenErrorTitle(title: string): string {
  121. return title.split(':')[0];
  122. }
  123. export function isRootTransaction(trace: TraceFullDetailed): boolean {
  124. // Root transactions has no parent_span_id
  125. return trace.parent_span_id === null;
  126. }