traceDataSection.spec.tsx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. import {EventFixture} from 'sentry-fixture/event';
  2. import {OrganizationFixture} from 'sentry-fixture/organization';
  3. import {ProjectFixture} from 'sentry-fixture/project';
  4. import {render, screen, userEvent, waitFor} from 'sentry-test/reactTestingLibrary';
  5. import AnalyticsArea from 'sentry/components/analyticsArea';
  6. import ProjectsStore from 'sentry/stores/projectsStore';
  7. import {trackAnalytics} from 'sentry/utils/analytics';
  8. import useRouteAnalyticsParams from 'sentry/utils/routeAnalytics/useRouteAnalyticsParams';
  9. import {getTitleSubtitleMessage} from './traceTimeline/traceIssue';
  10. import {TraceTimeline} from './traceTimeline/traceTimeline';
  11. import type {
  12. TimelineErrorEvent,
  13. TraceEventResponse,
  14. } from './traceTimeline/useTraceTimelineEvents';
  15. import {TraceDataSection} from './traceDataSection';
  16. jest.mock('sentry/utils/routeAnalytics/useRouteAnalyticsParams');
  17. jest.mock('sentry/utils/analytics');
  18. describe('TraceDataSection', () => {
  19. // Paid plans have global-views enabled
  20. // Include project: -1 in all matchQuery calls to ensure we are looking at all projects
  21. const organization = OrganizationFixture({
  22. features: ['global-views'],
  23. });
  24. const firstEventTimestamp = '2024-01-24T09:09:01+00:00';
  25. // This creates the ApiException event
  26. const event = EventFixture({
  27. // This is used to determine the presence of seconds
  28. dateCreated: firstEventTimestamp,
  29. contexts: {
  30. trace: {
  31. // This is used to determine if we should attempt
  32. // to render the trace timeline
  33. trace_id: '123',
  34. },
  35. },
  36. });
  37. const project = ProjectFixture();
  38. const emptyBody: TraceEventResponse = {data: [], meta: {fields: {}, units: {}}};
  39. const issuePlatformBody: TraceEventResponse = {
  40. data: [
  41. {
  42. // In issuePlatform, the message contains the title and the transaction
  43. message: '/api/slow/ Slow DB Query SELECT "sentry_monitorcheckin"."monitor_id"',
  44. timestamp: '2024-01-24T09:09:03+00:00',
  45. 'issue.id': 1000,
  46. project: project.slug,
  47. 'project.name': project.name,
  48. title: 'Slow DB Query',
  49. id: 'abc',
  50. transaction: 'n/a',
  51. culprit: '/api/slow/',
  52. 'event.type': '',
  53. },
  54. ],
  55. meta: {fields: {}, units: {}},
  56. };
  57. const mainError: TimelineErrorEvent = {
  58. culprit: 'n/a',
  59. 'error.value': ['some-other-error-value', 'The last error value'],
  60. timestamp: firstEventTimestamp,
  61. 'issue.id': event['issue.id'],
  62. project: project.slug,
  63. 'project.name': project.name,
  64. title: event.title,
  65. id: event.id,
  66. transaction: 'important.task',
  67. 'event.type': 'error',
  68. 'stack.function': ['important.task', 'task.run'],
  69. };
  70. const secondError: TimelineErrorEvent = {
  71. culprit: 'billiard.pool in foo', // Used for subtitle
  72. 'error.value': ['some-other-error-value', 'The last error value'],
  73. timestamp: '2024-01-24T09:09:04+00:00',
  74. 'issue.id': 9999,
  75. project: project.slug,
  76. 'project.name': project.name,
  77. title: 'someTitle',
  78. id: '12345',
  79. transaction: 'foo',
  80. 'event.type': 'error',
  81. 'stack.function': ['n/a'],
  82. };
  83. const discoverBody: TraceEventResponse = {
  84. data: [mainError],
  85. meta: {fields: {}, units: {}},
  86. };
  87. const twoIssuesBody: TraceEventResponse = {
  88. data: [mainError, secondError],
  89. meta: {fields: {}, units: {}},
  90. };
  91. beforeEach(() => {
  92. ProjectsStore.loadInitialData([project]);
  93. jest.clearAllMocks();
  94. });
  95. it('renders items and highlights the current event', async () => {
  96. MockApiClient.addMockResponse({
  97. url: `/organizations/${organization.slug}/events/`,
  98. body: issuePlatformBody,
  99. match: [MockApiClient.matchQuery({dataset: 'issuePlatform', project: -1})],
  100. });
  101. MockApiClient.addMockResponse({
  102. url: `/organizations/${organization.slug}/events/`,
  103. body: twoIssuesBody,
  104. match: [MockApiClient.matchQuery({dataset: 'discover', project: -1})],
  105. });
  106. render(<TraceDataSection event={event} />, {organization});
  107. expect(await screen.findByLabelText('Current Event')).toBeInTheDocument();
  108. await userEvent.hover(screen.getByTestId('trace-timeline-tooltip-1'));
  109. expect(await screen.findByText('You are here')).toBeInTheDocument();
  110. expect(useRouteAnalyticsParams).toHaveBeenCalledWith({
  111. trace_timeline_status: 'shown',
  112. });
  113. });
  114. it('displays nothing if the only event is the current event', async () => {
  115. MockApiClient.addMockResponse({
  116. url: `/organizations/${organization.slug}/events/`,
  117. body: emptyBody,
  118. match: [MockApiClient.matchQuery({dataset: 'issuePlatform', project: -1})],
  119. });
  120. MockApiClient.addMockResponse({
  121. url: `/organizations/${organization.slug}/events/`,
  122. body: discoverBody,
  123. match: [MockApiClient.matchQuery({dataset: 'discover', project: -1})],
  124. });
  125. const {container} = render(<TraceTimeline event={event} />, {organization});
  126. await waitFor(() =>
  127. expect(useRouteAnalyticsParams).toHaveBeenCalledWith({
  128. trace_timeline_status: 'empty',
  129. })
  130. );
  131. expect(container).toBeEmptyDOMElement();
  132. });
  133. it('displays nothing if there are no events', async () => {
  134. MockApiClient.addMockResponse({
  135. url: `/organizations/${organization.slug}/events/`,
  136. body: emptyBody,
  137. match: [MockApiClient.matchQuery({dataset: 'issuePlatform', project: -1})],
  138. });
  139. MockApiClient.addMockResponse({
  140. url: `/organizations/${organization.slug}/events/`,
  141. body: emptyBody,
  142. match: [MockApiClient.matchQuery({dataset: 'discover', project: -1})],
  143. });
  144. const {container} = render(<TraceTimeline event={event} />, {organization});
  145. await waitFor(() =>
  146. expect(useRouteAnalyticsParams).toHaveBeenCalledWith({
  147. trace_timeline_status: 'empty',
  148. })
  149. );
  150. expect(container).toBeEmptyDOMElement();
  151. });
  152. it('shows seconds for very short timelines', async () => {
  153. MockApiClient.addMockResponse({
  154. url: `/organizations/${organization.slug}/events/`,
  155. body: issuePlatformBody,
  156. match: [MockApiClient.matchQuery({dataset: 'issuePlatform', project: -1})],
  157. });
  158. MockApiClient.addMockResponse({
  159. url: `/organizations/${organization.slug}/events/`,
  160. body: twoIssuesBody,
  161. match: [MockApiClient.matchQuery({dataset: 'discover', project: -1})],
  162. });
  163. render(<TraceDataSection event={event} />, {organization});
  164. // Checking for the presence of seconds
  165. expect(await screen.findAllByText(/\d{1,2}:\d{2}:\d{2} (AM|PM)/)).toHaveLength(5);
  166. });
  167. // useTraceTimelineEvents() adds the current event if missing
  168. it('adds the current event if not in the api response', async () => {
  169. MockApiClient.addMockResponse({
  170. url: `/organizations/${organization.slug}/events/`,
  171. body: issuePlatformBody,
  172. match: [MockApiClient.matchQuery({dataset: 'issuePlatform', project: -1})],
  173. });
  174. MockApiClient.addMockResponse({
  175. url: `/organizations/${organization.slug}/events/`,
  176. body: {
  177. // The event for the mainError is missing, thus, it will get added
  178. data: [secondError],
  179. },
  180. match: [MockApiClient.matchQuery({dataset: 'discover', project: -1})],
  181. });
  182. render(<TraceDataSection event={event} />, {organization});
  183. expect(await screen.findByLabelText('Current Event')).toBeInTheDocument();
  184. });
  185. it('skips the timeline and shows related issues (2 issues)', async () => {
  186. MockApiClient.addMockResponse({
  187. url: `/organizations/${organization.slug}/events/`,
  188. body: issuePlatformBody,
  189. match: [MockApiClient.matchQuery({dataset: 'issuePlatform', project: -1})],
  190. });
  191. MockApiClient.addMockResponse({
  192. url: `/organizations/${organization.slug}/events/`,
  193. body: discoverBody,
  194. match: [MockApiClient.matchQuery({dataset: 'discover', project: -1})],
  195. });
  196. // Used to determine the project badge
  197. MockApiClient.addMockResponse({
  198. url: `/organizations/${organization.slug}/projects/`,
  199. body: [],
  200. });
  201. // Wrapping with the issue_details area so we can test both the new and
  202. // old (pre-Nov 2024) analytics are emitted. In the source code, this
  203. // area is located in GroupEventDetails.
  204. render(
  205. <AnalyticsArea name="issue_details">
  206. <TraceDataSection event={event} />
  207. </AnalyticsArea>,
  208. {organization}
  209. );
  210. // Instead of a timeline, we should see the other related issue
  211. expect(await screen.findByText('Slow DB Query')).toBeInTheDocument(); // The title
  212. expect(await screen.findByText('/api/slow/')).toBeInTheDocument(); // The subtitle/transaction
  213. expect(
  214. await screen.findByText('One other issue appears in the same trace.')
  215. ).toBeInTheDocument();
  216. expect(
  217. await screen.findByText('SELECT "sentry_monitorcheckin"."monitor_id"') // The message
  218. ).toBeInTheDocument();
  219. expect(screen.queryByLabelText('Current Event')).not.toBeInTheDocument();
  220. // Test analytics
  221. await userEvent.click(await screen.findByText('Slow DB Query'));
  222. expect(useRouteAnalyticsParams).toHaveBeenCalledWith({
  223. has_related_trace_issue: true,
  224. });
  225. expect(trackAnalytics).toHaveBeenCalledTimes(2);
  226. expect(trackAnalytics).toHaveBeenCalledWith(
  227. 'issue_details.related_trace_issue.trace_issue_clicked',
  228. {
  229. group_id: issuePlatformBody.data[0]['issue.id'],
  230. organization: organization,
  231. }
  232. );
  233. expect(trackAnalytics).toHaveBeenCalledWith('one_other_related_trace_issue.clicked', {
  234. group_id: issuePlatformBody.data[0]['issue.id'],
  235. organization: organization,
  236. area: 'issue_details',
  237. });
  238. });
  239. it('skips the timeline and shows NO related issues (only 1 issue)', async () => {
  240. MockApiClient.addMockResponse({
  241. url: `/organizations/${organization.slug}/events/`,
  242. body: emptyBody,
  243. match: [MockApiClient.matchQuery({dataset: 'issuePlatform', project: -1})],
  244. });
  245. MockApiClient.addMockResponse({
  246. url: `/organizations/${organization.slug}/events/`,
  247. // Only 1 issue
  248. body: discoverBody,
  249. match: [MockApiClient.matchQuery({dataset: 'discover', project: -1})],
  250. });
  251. // Used to determine the project badge
  252. MockApiClient.addMockResponse({
  253. url: `/organizations/${organization.slug}/projects/`,
  254. body: [],
  255. });
  256. render(<TraceDataSection event={event} />, {organization});
  257. // We do not display any related issues because we only have 1 issue
  258. expect(await screen.queryByText('Slow DB Query')).not.toBeInTheDocument();
  259. expect(
  260. await screen.queryByText('AttributeError: Something Failed')
  261. ).not.toBeInTheDocument();
  262. // We do not display the timeline because we only have 1 event
  263. expect(await screen.queryByLabelText('Current Event')).not.toBeInTheDocument();
  264. expect(useRouteAnalyticsParams).toHaveBeenCalledWith({});
  265. });
  266. it('trace timeline works for plans with no global-views feature', async () => {
  267. // This test will call the endpoint without the global-views feature, thus,
  268. // we will only look at the current project (project: event.projectID) instead of passing -1
  269. MockApiClient.addMockResponse({
  270. url: `/organizations/${organization.slug}/events/`,
  271. body: issuePlatformBody,
  272. match: [
  273. MockApiClient.matchQuery({
  274. dataset: 'issuePlatform',
  275. project: event.projectID,
  276. }),
  277. ],
  278. });
  279. MockApiClient.addMockResponse({
  280. url: `/organizations/${organization.slug}/events/`,
  281. body: twoIssuesBody,
  282. match: [
  283. MockApiClient.matchQuery({
  284. dataset: 'discover',
  285. project: event.projectID,
  286. }),
  287. ],
  288. });
  289. render(<TraceDataSection event={event} />, {
  290. organization: OrganizationFixture({features: []}), // No global-views feature
  291. });
  292. expect(await screen.findByLabelText('Current Event')).toBeInTheDocument();
  293. });
  294. it('trace-related issue works for plans with no global-views feature', async () => {
  295. // This test will call the endpoint without the global-views feature, thus,
  296. // we will only look at the current project (project: event.projectID) instead of passing -1
  297. MockApiClient.addMockResponse({
  298. url: `/organizations/${organization.slug}/events/`,
  299. body: issuePlatformBody,
  300. match: [
  301. MockApiClient.matchQuery({
  302. dataset: 'issuePlatform',
  303. project: event.projectID,
  304. }),
  305. ],
  306. });
  307. MockApiClient.addMockResponse({
  308. url: `/organizations/${organization.slug}/events/`,
  309. body: discoverBody,
  310. match: [
  311. MockApiClient.matchQuery({
  312. dataset: 'discover',
  313. project: event.projectID,
  314. }),
  315. ],
  316. });
  317. // Used to determine the project badge
  318. MockApiClient.addMockResponse({
  319. url: `/organizations/${organization.slug}/projects/`,
  320. body: [],
  321. });
  322. render(<TraceDataSection event={event} />, {
  323. organization: OrganizationFixture({
  324. features: [],
  325. }),
  326. });
  327. expect(await screen.findByText('Slow DB Query')).toBeInTheDocument();
  328. });
  329. });
  330. function createEvent({
  331. culprit,
  332. title,
  333. error_value,
  334. event_type = 'error',
  335. stack_function = [],
  336. message = 'n/a',
  337. }: {
  338. culprit: string;
  339. title: string;
  340. error_value?: string[];
  341. event_type?: 'default' | 'error' | '';
  342. message?: string;
  343. stack_function?: string[];
  344. }) {
  345. const event = {
  346. culprit: culprit,
  347. timestamp: '2024-01-24T09:09:04+00:00',
  348. 'issue.id': 9999,
  349. project: 'foo',
  350. 'project.name': 'bar',
  351. title: title,
  352. id: '12345',
  353. transaction: 'n/a',
  354. 'event.type': event_type,
  355. };
  356. // Using this intermediary variable helps typescript
  357. let return_event;
  358. if (event['event.type'] === 'error') {
  359. return_event = {
  360. ...event,
  361. 'stack.function': stack_function,
  362. 'error.value': error_value,
  363. };
  364. } else if (event['event.type'] === '') {
  365. return_event = {
  366. ...event,
  367. message: message,
  368. };
  369. } else {
  370. return_event = event;
  371. }
  372. return return_event;
  373. }
  374. describe('getTitleSubtitleMessage()', () => {
  375. it('error event', () => {
  376. expect(
  377. getTitleSubtitleMessage(
  378. createEvent({
  379. culprit: '/api/0/sentry-app-installations/{uuid}/',
  380. title:
  381. 'ClientError: 404 Client Error: for url: https://api.clickup.com/sentry/webhook',
  382. error_value: [
  383. '404 Client Error: for url: https://api.clickup.com/sentry/webhook',
  384. ],
  385. })
  386. )
  387. ).toEqual({
  388. title: 'ClientError', // The colon and remainder of string are removed
  389. subtitle: '/api/0/sentry-app-installations/{uuid}/',
  390. message: '404 Client Error: for url: https://api.clickup.com/sentry/webhook',
  391. });
  392. });
  393. it('error event: It keeps the colon', () => {
  394. expect(
  395. getTitleSubtitleMessage(
  396. createEvent({
  397. culprit: 'billiard.pool in foo',
  398. title: 'WorkerLostError: ',
  399. error_value: ['some-other-error-value', 'The last error value'],
  400. })
  401. )
  402. ).toEqual({
  403. title: 'WorkerLostError:', // The colon is kept
  404. subtitle: 'billiard.pool in foo',
  405. message: 'The last error value',
  406. });
  407. });
  408. it('error event: No error_value', () => {
  409. expect(
  410. getTitleSubtitleMessage(
  411. createEvent({
  412. title: 'foo',
  413. culprit: 'bar',
  414. error_value: [''], // We always get a non-empty array
  415. })
  416. )
  417. ).toEqual({
  418. title: 'foo',
  419. subtitle: 'bar',
  420. message: '',
  421. });
  422. });
  423. it('default event', () => {
  424. expect(
  425. getTitleSubtitleMessage(
  426. createEvent({
  427. culprit: '/api/0/organizations/{organization_id_or_slug}/issues/',
  428. title: 'Query from referrer search.group_index is throttled',
  429. event_type: 'default',
  430. })
  431. )
  432. ).toEqual({
  433. title: 'Query from referrer search.group_index is throttled',
  434. subtitle: '',
  435. message: '/api/0/organizations/{organization_id_or_slug}/issues/',
  436. });
  437. });
  438. it('issue platform event', () => {
  439. expect(
  440. getTitleSubtitleMessage(
  441. createEvent({
  442. message: '/api/slow/ Slow DB Query SELECT "sentry_monitorcheckin"."monitor_id"',
  443. culprit: '/api/slow/',
  444. title: 'Slow DB Query',
  445. event_type: '',
  446. })
  447. )
  448. ).toEqual({
  449. title: 'Slow DB Query',
  450. subtitle: '/api/slow/',
  451. message: 'SELECT "sentry_monitorcheckin"."monitor_id"',
  452. });
  453. });
  454. });