initializePerformanceData.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. import {initializeOrg} from 'sentry-test/initializeOrg';
  2. import {RawSpanType} from 'sentry/components/events/interfaces/spans/types';
  3. import {EntryType, EventTransaction, Project} from 'sentry/types';
  4. import {defined} from 'sentry/utils';
  5. import EventView from 'sentry/utils/discover/eventView';
  6. import {
  7. ExampleSpan,
  8. ExampleTransaction,
  9. SuspectSpan,
  10. } from 'sentry/utils/performance/suspectSpans/types';
  11. export interface InitializeDataSettings {
  12. features?: string[];
  13. project?: any; // TODO(k-fish): Fix this project type.
  14. projects?: Project[];
  15. query?: {};
  16. selectedProject?: any;
  17. }
  18. export function initializeData(settings?: InitializeDataSettings) {
  19. const _defaultProject = TestStubs.Project();
  20. const _settings = {
  21. query: {},
  22. features: [],
  23. projects: [_defaultProject],
  24. project: _defaultProject,
  25. ...settings,
  26. };
  27. const {query, features, projects, selectedProject: project} = _settings;
  28. const organization = TestStubs.Organization({
  29. features,
  30. projects,
  31. });
  32. const routerLocation: {query: {project?: number}} = {
  33. query: {
  34. ...query,
  35. },
  36. };
  37. if (settings?.selectedProject || settings?.project) {
  38. routerLocation.query.project = (project || settings?.project) as any;
  39. }
  40. const router = {
  41. location: routerLocation,
  42. };
  43. const initialData = initializeOrg({organization, projects, project, router});
  44. const location = initialData.router.location;
  45. const eventView = EventView.fromLocation(location);
  46. return {...initialData, location, eventView};
  47. }
  48. export const SAMPLE_SPANS = [
  49. {
  50. op: 'op1',
  51. group: 'aaaaaaaaaaaaaaaa',
  52. description: 'span-1',
  53. examples: [
  54. {
  55. id: 'abababababababab',
  56. description: 'span-1',
  57. spans: [{id: 'ababab11'}, {id: 'ababab22'}],
  58. },
  59. {
  60. id: 'acacacacacacacac',
  61. description: 'span-2',
  62. spans: [{id: 'acacac11'}, {id: 'acacac22'}],
  63. },
  64. {
  65. id: 'adadadadadadadad',
  66. description: 'span-3',
  67. spans: [{id: 'adadad11'}, {id: 'adadad22'}],
  68. },
  69. ],
  70. },
  71. {
  72. op: 'op2',
  73. group: 'bbbbbbbbbbbbbbbb',
  74. description: 'span-4',
  75. examples: [
  76. {
  77. id: 'bcbcbcbcbcbcbcbc',
  78. description: 'span-4',
  79. spans: [{id: 'bcbcbc11'}, {id: 'bcbcbc11'}],
  80. },
  81. {
  82. id: 'bdbdbdbdbdbdbdbd',
  83. description: 'span-5',
  84. spans: [{id: 'bdbdbd11'}, {id: 'bdbdbd22'}],
  85. },
  86. {
  87. id: 'bebebebebebebebe',
  88. description: 'span-6',
  89. spans: [{id: 'bebebe11'}, {id: 'bebebe22'}],
  90. },
  91. ],
  92. },
  93. ];
  94. type SpanOpt = {
  95. id: string;
  96. };
  97. type ExampleOpt = {
  98. description: string;
  99. id: string;
  100. spans: SpanOpt[];
  101. };
  102. type SuspectOpt = {
  103. description: string;
  104. examples: ExampleOpt[];
  105. group: string;
  106. op: string;
  107. };
  108. function makeSpan(opt: SpanOpt): ExampleSpan {
  109. const {id} = opt;
  110. return {
  111. id,
  112. startTimestamp: 10100,
  113. finishTimestamp: 10200,
  114. exclusiveTime: 100,
  115. };
  116. }
  117. function makeExample(opt: ExampleOpt): ExampleTransaction {
  118. const {id, description, spans} = opt;
  119. return {
  120. id,
  121. description,
  122. startTimestamp: 10000,
  123. finishTimestamp: 12000,
  124. nonOverlappingExclusiveTime: 2000,
  125. spans: spans.map(makeSpan),
  126. };
  127. }
  128. export function makeSuspectSpan(opt: SuspectOpt): SuspectSpan {
  129. const {op, group, description, examples} = opt;
  130. return {
  131. op,
  132. group,
  133. description,
  134. frequency: 1,
  135. count: 1,
  136. avgOccurrences: 1,
  137. sumExclusiveTime: 5,
  138. p50ExclusiveTime: 1,
  139. p75ExclusiveTime: 2,
  140. p95ExclusiveTime: 3,
  141. p99ExclusiveTime: 4,
  142. examples: examples.map(makeExample),
  143. };
  144. }
  145. export function generateSuspectSpansResponse(opts?: {
  146. examples?: number;
  147. examplesOnly?: boolean;
  148. }) {
  149. const {examples, examplesOnly} = opts ?? {};
  150. return SAMPLE_SPANS.map(sampleSpan => {
  151. const span = {...sampleSpan};
  152. if (defined(examples)) {
  153. span.examples = span.examples.slice(0, examples);
  154. }
  155. const suspectSpans = makeSuspectSpan(span);
  156. if (examplesOnly) {
  157. return {
  158. op: suspectSpans.op,
  159. group: suspectSpans.group,
  160. examples: suspectSpans.examples,
  161. };
  162. }
  163. return suspectSpans;
  164. });
  165. }
  166. export function generateSampleEvent(): EventTransaction {
  167. const event = {
  168. id: '2b658a829a21496b87fd1f14a61abf65',
  169. eventID: '2b658a829a21496b87fd1f14a61abf65',
  170. title: '/organizations/:orgId/discover/results/',
  171. type: 'transaction',
  172. startTimestamp: 1622079935.86141,
  173. endTimestamp: 1622079940.032905,
  174. contexts: {
  175. trace: {
  176. trace_id: '8cbbc19c0f54447ab702f00263262726',
  177. span_id: 'a000000000000000',
  178. op: 'pageload',
  179. status: 'unknown',
  180. type: 'trace',
  181. },
  182. },
  183. entries: [
  184. {
  185. data: [],
  186. type: EntryType.SPANS,
  187. },
  188. ],
  189. } as unknown as EventTransaction;
  190. return event;
  191. }
  192. export function generateSampleSpan(
  193. description: string | undefined,
  194. op: string | undefined,
  195. span_id: string,
  196. parent_span_id: string,
  197. event: EventTransaction
  198. ) {
  199. const span: RawSpanType = {
  200. start_timestamp: 1000,
  201. timestamp: 2000,
  202. description,
  203. op,
  204. span_id,
  205. parent_span_id,
  206. trace_id: '8cbbc19c0f54447ab702f00263262726',
  207. status: 'ok',
  208. tags: {
  209. 'http.status_code': '200',
  210. },
  211. data: {},
  212. };
  213. if (!Array.isArray(event.entries[0].data)) {
  214. throw new Error('Event entries data is not an array');
  215. }
  216. event.entries[0].data.push(span);
  217. return span;
  218. }