utils.ts 7.7 KB

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