utils.ts 7.1 KB

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