utils.ts 6.2 KB

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