traceTreeTestUtils.tsx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. import {uuid4} from '@sentry/utils';
  2. import {EntryType, type Event, type EventTransaction} from 'sentry/types/event';
  3. import type {
  4. TracePerformanceIssue,
  5. TraceSplitResults,
  6. } from 'sentry/utils/performance/quickTrace/types';
  7. import type {TraceMetaQueryResults} from '../traceApi/useTraceMeta';
  8. import {
  9. isAutogroupedNode,
  10. isMissingInstrumentationNode,
  11. isSpanNode,
  12. isTraceErrorNode,
  13. isTraceNode,
  14. isTransactionNode,
  15. } from '../traceGuards';
  16. import {ParentAutogroupNode} from './parentAutogroupNode';
  17. import {SiblingAutogroupNode} from './siblingAutogroupNode';
  18. import type {TraceTree} from './traceTree';
  19. import type {TraceTreeNode} from './traceTreeNode';
  20. export function makeEvent(
  21. overrides: Partial<Event> = {},
  22. spans: TraceTree.Span[] = []
  23. ): Event {
  24. return {
  25. entries: [{type: EntryType.SPANS, data: spans}],
  26. ...overrides,
  27. } as Event;
  28. }
  29. export function makeTrace(
  30. overrides: Partial<TraceSplitResults<TraceTree.Transaction>>
  31. ): TraceSplitResults<TraceTree.Transaction> {
  32. return {
  33. transactions: [],
  34. orphan_errors: [],
  35. ...overrides,
  36. } as TraceSplitResults<TraceTree.Transaction>;
  37. }
  38. export function makeTransaction(
  39. overrides: Partial<TraceTree.Transaction> = {}
  40. ): TraceTree.Transaction {
  41. return {
  42. children: [],
  43. sdk_name: '',
  44. start_timestamp: 0,
  45. timestamp: 1,
  46. transaction: 'transaction',
  47. 'transaction.op': 'transaction.op',
  48. 'transaction.status': '',
  49. performance_issues: [],
  50. errors: [],
  51. ...overrides,
  52. } as TraceTree.Transaction;
  53. }
  54. export function makeSpan(overrides: Partial<TraceTree.Span> = {}): TraceTree.Span {
  55. return {
  56. span_id: overrides.span_id ?? uuid4(),
  57. op: 'span.op',
  58. description: 'span.description',
  59. start_timestamp: 0,
  60. timestamp: 10,
  61. data: {},
  62. trace_id: '',
  63. ...overrides,
  64. };
  65. }
  66. export function makeTraceError(
  67. overrides: Partial<TraceTree.TraceError> = {}
  68. ): TraceTree.TraceError {
  69. return {
  70. title: 'MaybeEncodingError: Error sending result',
  71. level: 'error',
  72. event_type: 'error',
  73. data: {},
  74. ...overrides,
  75. } as TraceTree.TraceError;
  76. }
  77. export function makeTracePerformanceIssue(
  78. overrides: Partial<TracePerformanceIssue> = {}
  79. ): TracePerformanceIssue {
  80. return {
  81. culprit: 'code',
  82. end: new Date().toISOString(),
  83. span: [],
  84. start: new Date().toISOString(),
  85. suspect_spans: ['sus span'],
  86. type: 0,
  87. issue_short_id: 'issue short id',
  88. ...overrides,
  89. } as TracePerformanceIssue;
  90. }
  91. export function makeTraceMetaQueryResults(
  92. overrides: Partial<TraceMetaQueryResults> = {}
  93. ): TraceMetaQueryResults {
  94. return {
  95. data: undefined,
  96. errors: [],
  97. isLoading: false,
  98. status: 'idle',
  99. ...overrides,
  100. } as TraceMetaQueryResults;
  101. }
  102. export function makeEventTransaction(
  103. overrides: Partial<Event> = {},
  104. spans: TraceTree.Span[] = []
  105. ): EventTransaction {
  106. return {
  107. contexts: {},
  108. tags: [],
  109. entries: [{type: EntryType.SPANS, data: spans}],
  110. ...overrides,
  111. } as EventTransaction;
  112. }
  113. export function makeParentAutogroup(
  114. overrides: Partial<TraceTree.ChildrenAutogroup> = {}
  115. ): TraceTree.ChildrenAutogroup {
  116. return {
  117. autogrouped_by: {
  118. op: overrides.op ?? 'op',
  119. },
  120. ...overrides,
  121. } as TraceTree.ChildrenAutogroup;
  122. }
  123. export function makeSiblingAutogroup(
  124. overrides: Partial<TraceTree.SiblingAutogroup> = {}
  125. ): TraceTree.SiblingAutogroup {
  126. return {
  127. autogrouped_by: {
  128. op: overrides.op ?? 'op',
  129. description: overrides.description ?? 'description',
  130. },
  131. ...overrides,
  132. } as TraceTree.SiblingAutogroup;
  133. }
  134. export function assertSpanNode(
  135. node: TraceTreeNode<TraceTree.NodeValue>
  136. ): asserts node is TraceTreeNode<TraceTree.Span> {
  137. if (!isSpanNode(node)) {
  138. throw new Error('node is not a span');
  139. }
  140. }
  141. export function assertTraceNode(
  142. node: TraceTreeNode<TraceTree.NodeValue>
  143. ): asserts node is TraceTreeNode<TraceTree.Trace> {
  144. if (!isTraceNode(node)) {
  145. throw new Error('node is not a trace');
  146. }
  147. }
  148. export function assertTransactionNode(
  149. node: TraceTreeNode<TraceTree.NodeValue> | null
  150. ): asserts node is TraceTreeNode<TraceTree.Transaction> {
  151. if (!node || !isTransactionNode(node)) {
  152. throw new Error('node is not a transaction');
  153. }
  154. }
  155. export function assertMissingInstrumentationNode(
  156. node: TraceTreeNode<TraceTree.NodeValue>
  157. ): asserts node is TraceTreeNode<TraceTree.MissingInstrumentationSpan> {
  158. if (!isMissingInstrumentationNode(node)) {
  159. throw new Error('node is not a missing instrumentation node');
  160. }
  161. }
  162. export function assertTraceErrorNode(
  163. node: TraceTreeNode<TraceTree.NodeValue>
  164. ): asserts node is TraceTreeNode<TraceTree.TraceError> {
  165. if (!isTraceErrorNode(node)) {
  166. throw new Error('node is not a trace error node');
  167. }
  168. }
  169. export function assertAutogroupedNode(
  170. node: TraceTreeNode<TraceTree.NodeValue>
  171. ): asserts node is ParentAutogroupNode | SiblingAutogroupNode {
  172. if (!isAutogroupedNode(node)) {
  173. throw new Error('node is not a autogrouped node');
  174. }
  175. }
  176. export function assertParentAutogroupedNode(
  177. node: TraceTreeNode<TraceTree.NodeValue>
  178. ): asserts node is ParentAutogroupNode {
  179. if (!(node instanceof ParentAutogroupNode)) {
  180. throw new Error('node is not a parent autogrouped node');
  181. }
  182. }
  183. export function assertSiblingAutogroupedNode(
  184. node: TraceTreeNode<TraceTree.NodeValue>
  185. ): asserts node is ParentAutogroupNode {
  186. if (!(node instanceof SiblingAutogroupNode)) {
  187. throw new Error('node is not a parent node');
  188. }
  189. }
  190. export function makeNodeMetadata(
  191. overrides: Partial<TraceTree.Metadata> = {}
  192. ): TraceTree.Metadata {
  193. return {
  194. event_id: undefined,
  195. project_slug: undefined,
  196. ...overrides,
  197. };
  198. }