utils.ts 7.8 KB

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