initializePerformanceData.ts 5.4 KB

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