utils.ts 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. import {RawSpanType} from 'sentry/components/events/interfaces/spans/types';
  2. import {EntryType, EventOrGroupType, EventTransaction, IssueType} from 'sentry/types';
  3. export enum ProblemSpan {
  4. PARENT = 'parent',
  5. OFFENDER = 'offender',
  6. CAUSE = 'cause',
  7. }
  8. export const EXAMPLE_TRANSACTION_TITLE = '/api/0/transaction-test-endpoint/';
  9. type AddSpanOpts = {
  10. endTimestamp: number;
  11. startTimestamp: number;
  12. data?: Record<string, any>;
  13. description?: string;
  14. op?: string;
  15. problemSpan?: ProblemSpan | ProblemSpan[];
  16. status?: string;
  17. };
  18. interface TransactionSettings {
  19. duration?: number;
  20. fcp?: number;
  21. }
  22. export class TransactionEventBuilder {
  23. TRACE_ID = '8cbbc19c0f54447ab702f00263262726';
  24. ROOT_SPAN_ID = '0000000000000000';
  25. #event: EventTransaction;
  26. #spans: RawSpanType[] = [];
  27. constructor(
  28. id?: string,
  29. title?: string,
  30. problemType?: IssueType,
  31. transactionSettings?: TransactionSettings
  32. ) {
  33. this.#event = {
  34. id: id ?? 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
  35. eventID: id ?? 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
  36. title: title ?? EXAMPLE_TRANSACTION_TITLE,
  37. type: EventOrGroupType.TRANSACTION,
  38. startTimestamp: 0,
  39. endTimestamp: transactionSettings?.duration ?? 0,
  40. contexts: {
  41. trace: {
  42. trace_id: this.TRACE_ID,
  43. span_id: this.ROOT_SPAN_ID,
  44. op: 'pageload',
  45. status: 'ok',
  46. type: 'trace',
  47. },
  48. },
  49. entries: [
  50. {
  51. data: this.#spans,
  52. type: EntryType.SPANS,
  53. },
  54. ],
  55. perfProblem: {
  56. causeSpanIds: [],
  57. offenderSpanIds: [],
  58. parentSpanIds: [],
  59. issueType: problemType ?? IssueType.PERFORMANCE_N_PLUS_ONE_DB_QUERIES,
  60. },
  61. // For the purpose of mock data, we don't care as much about the properties below.
  62. // They're here to satisfy the type constraints, but in the future if we need actual values here
  63. // for testing purposes, we can add methods on the builder to set them.
  64. crashFile: null,
  65. culprit: '',
  66. dateReceived: '',
  67. dist: null,
  68. errors: [],
  69. fingerprints: [],
  70. location: null,
  71. message: '',
  72. measurements: {
  73. fcp: {
  74. value: transactionSettings?.fcp ?? 0,
  75. unit: 'millisecond',
  76. },
  77. },
  78. metadata: {
  79. current_level: undefined,
  80. current_tree_label: undefined,
  81. directive: undefined,
  82. display_title_with_tree_label: undefined,
  83. filename: undefined,
  84. finest_tree_label: undefined,
  85. function: undefined,
  86. message: undefined,
  87. origin: undefined,
  88. stripped_crash: undefined,
  89. title: undefined,
  90. type: undefined,
  91. uri: undefined,
  92. value: undefined,
  93. },
  94. occurrence: null,
  95. projectID: '',
  96. size: 0,
  97. tags: [],
  98. user: null,
  99. };
  100. }
  101. generateSpanId() {
  102. // Convert the num of spans to a hex string to get its ID
  103. return (this.#spans.length + 1).toString(16).padStart(16, '0');
  104. }
  105. addEntry(entry: EventTransaction['entries'][number]) {
  106. this.#event.entries.push(entry);
  107. }
  108. addSpan(mockSpan: MockSpan, numSpans = 1, parentSpanId?: string) {
  109. for (let i = 0; i < numSpans; i++) {
  110. const spanId = this.generateSpanId();
  111. const {span} = mockSpan;
  112. const clonedSpan = {...span};
  113. clonedSpan.span_id = spanId;
  114. clonedSpan.trace_id = this.TRACE_ID;
  115. clonedSpan.parent_span_id = parentSpanId ?? this.ROOT_SPAN_ID;
  116. this.#spans.push(clonedSpan);
  117. const problemSpans = Array.isArray(mockSpan.problemSpan)
  118. ? mockSpan.problemSpan
  119. : [mockSpan.problemSpan];
  120. problemSpans.forEach(problemSpan => {
  121. switch (problemSpan) {
  122. case ProblemSpan.PARENT:
  123. this.#event.perfProblem?.parentSpanIds.push(spanId);
  124. break;
  125. case ProblemSpan.OFFENDER:
  126. this.#event.perfProblem?.offenderSpanIds.push(spanId);
  127. break;
  128. case ProblemSpan.CAUSE:
  129. this.#event.perfProblem?.causeSpanIds.push(spanId);
  130. break;
  131. default:
  132. break;
  133. }
  134. });
  135. if (clonedSpan.timestamp > this.#event.endTimestamp) {
  136. this.#event.endTimestamp = clonedSpan.timestamp;
  137. }
  138. mockSpan.children.forEach(child => this.addSpan(child, 1, spanId));
  139. }
  140. return this;
  141. }
  142. getEvent() {
  143. return this.#event;
  144. }
  145. }
  146. /**
  147. * A MockSpan object to be used for testing. This object is intended to be used in tandem with `TransactionEventBuilder`
  148. */
  149. export class MockSpan {
  150. span: RawSpanType;
  151. children: MockSpan[] = [];
  152. problemSpan: ProblemSpan | ProblemSpan[] | undefined;
  153. /**
  154. *
  155. * @param opts.startTimestamp
  156. * @param opts.endTimestamp
  157. * @param opts.op The operation of the span
  158. * @param opts.description The description of the span
  159. * @param opts.status Optional span specific status, defaults to 'ok'
  160. * @param opts.problemSpan If this span should be part of a performance problem, indicates the type of problem span (i.e ProblemSpan.OFFENDER, ProblemSpan.PARENT)
  161. * @param opts.parentSpanId When provided, will explicitly set this span's parent ID. If you are creating nested spans via `addChild` on the `MockSpan` object,
  162. * this will be handled automatically and you do not need to provide an ID. Defaults to the root span's ID.
  163. */
  164. constructor(opts: AddSpanOpts) {
  165. const {startTimestamp, endTimestamp, op, description, status, problemSpan} = opts;
  166. this.span = {
  167. start_timestamp: startTimestamp,
  168. timestamp: endTimestamp,
  169. op,
  170. description,
  171. status: status ?? 'ok',
  172. data: opts.data || {},
  173. // These values are automatically assigned by the TransactionEventBuilder when the spans are added
  174. span_id: '',
  175. trace_id: '',
  176. parent_span_id: '',
  177. };
  178. this.problemSpan = problemSpan;
  179. }
  180. /**
  181. *
  182. * @param opts.numSpans If provided, will create the same span numSpan times
  183. */
  184. addChild(opts: AddSpanOpts, numSpans = 1) {
  185. const {startTimestamp, endTimestamp, op, description, status, problemSpan} = opts;
  186. for (let i = 0; i < numSpans; i++) {
  187. const span = new MockSpan({
  188. startTimestamp,
  189. endTimestamp,
  190. op,
  191. description,
  192. status,
  193. problemSpan,
  194. });
  195. this.children.push(span);
  196. }
  197. return this;
  198. }
  199. /**
  200. * Allows you to create a nested group of duplicate mock spans by duplicating the current span. This is useful for simulating the nested 'autogrouped' condition on the span tree.
  201. * Will create `depth` spans, each span being a child of the previous.
  202. * @param depth
  203. */
  204. addDuplicateNestedChildren(depth = 1) {
  205. let currentSpan: MockSpan = this;
  206. for (let i = 0; i < depth; i++) {
  207. currentSpan.addChild(currentSpan.getOpts());
  208. currentSpan = currentSpan.children[0];
  209. }
  210. return this;
  211. }
  212. getOpts() {
  213. return {
  214. startTimestamp: this.span.start_timestamp,
  215. endTimestamp: this.span.timestamp,
  216. op: this.span.op,
  217. description: this.span.description,
  218. status: this.span.status,
  219. problemSpan: this.problemSpan,
  220. };
  221. }
  222. }