httpSamplesPanel.spec.tsx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. import {OrganizationFixture} from 'sentry-fixture/organization';
  2. import {
  3. render,
  4. screen,
  5. userEvent,
  6. waitForElementToBeRemoved,
  7. } from 'sentry-test/reactTestingLibrary';
  8. import {useLocation} from 'sentry/utils/useLocation';
  9. import usePageFilters from 'sentry/utils/usePageFilters';
  10. import {HTTPSamplesPanel} from 'sentry/views/insights/http/components/httpSamplesPanel';
  11. jest.mock('sentry/utils/useLocation');
  12. jest.mock('sentry/utils/usePageFilters');
  13. describe('HTTPSamplesPanel', () => {
  14. const organization = OrganizationFixture();
  15. let eventsRequestMock: jest.Mock;
  16. jest.mocked(usePageFilters).mockReturnValue({
  17. isReady: true,
  18. desyncedFilters: new Set(),
  19. pinnedFilters: new Set(),
  20. shouldPersist: true,
  21. selection: {
  22. datetime: {
  23. period: '10d',
  24. start: null,
  25. end: null,
  26. utc: false,
  27. },
  28. environments: [],
  29. projects: [],
  30. },
  31. });
  32. jest.mocked(useLocation).mockReturnValue({
  33. pathname: '',
  34. search: '',
  35. query: {
  36. domain: '*.sentry.dev',
  37. statsPeriod: '10d',
  38. transaction: '/api/0/users',
  39. transactionMethod: 'GET',
  40. panel: 'status',
  41. },
  42. hash: '',
  43. state: undefined,
  44. action: 'PUSH',
  45. key: '',
  46. });
  47. beforeEach(() => {
  48. jest.clearAllMocks();
  49. eventsRequestMock = MockApiClient.addMockResponse({
  50. url: `/organizations/${organization.slug}/events/`,
  51. method: 'GET',
  52. match: [
  53. MockApiClient.matchQuery({
  54. referrer: 'api.performance.http.samples-panel-metrics-ribbon',
  55. }),
  56. ],
  57. body: {
  58. data: [
  59. {
  60. 'project.id': 1,
  61. 'transaction.id': '',
  62. 'spm()': 22.18,
  63. 'http_response_rate(3)': 0.01,
  64. 'http_response_rate(4)': 0.025,
  65. 'http_response_rate(5)': 0.015,
  66. 'avg(span.self_time)': 140.2,
  67. 'sum(span.self_time)': 2709238,
  68. },
  69. ],
  70. meta: {
  71. fields: {
  72. 'spm()': 'rate',
  73. 'avg(span.self_time)': 'duration',
  74. 'http_response_rate(3)': 'percentage',
  75. 'http_response_rate(4)': 'percentage',
  76. 'http_response_rate(5)': 'percentage',
  77. 'sum(span.self_time)': 'duration',
  78. },
  79. },
  80. },
  81. });
  82. });
  83. afterAll(() => {
  84. jest.resetAllMocks();
  85. });
  86. describe('Status panel', () => {
  87. let eventsStatsRequestMock: jest.Mock;
  88. let samplesRequestMock: jest.Mock;
  89. let spanFieldTagsMock: jest.Mock;
  90. beforeEach(() => {
  91. jest.mocked(useLocation).mockReturnValue({
  92. pathname: '',
  93. search: '',
  94. query: {
  95. statsPeriod: '10d',
  96. transaction: '/api/0/users',
  97. transactionMethod: 'GET',
  98. panel: 'status',
  99. responseCodeClass: '3',
  100. },
  101. hash: '',
  102. state: undefined,
  103. action: 'PUSH',
  104. key: '',
  105. });
  106. eventsStatsRequestMock = MockApiClient.addMockResponse({
  107. url: `/organizations/${organization.slug}/events-stats/`,
  108. method: 'GET',
  109. match: [
  110. MockApiClient.matchQuery({
  111. referrer: 'api.performance.http.samples-panel-response-code-chart',
  112. }),
  113. ],
  114. body: {
  115. '301': {
  116. data: [
  117. [1699907700, [{count: 7810.2}]],
  118. [1699908000, [{count: 1216.8}]],
  119. ],
  120. },
  121. '304': {
  122. data: [
  123. [1699907700, [{count: 2701.5}]],
  124. [1699908000, [{count: 78.12}]],
  125. ],
  126. },
  127. },
  128. });
  129. samplesRequestMock = MockApiClient.addMockResponse({
  130. url: `/organizations/${organization.slug}/events/`,
  131. method: 'GET',
  132. match: [
  133. MockApiClient.matchQuery({
  134. referrer: 'api.performance.http.samples-panel-response-code-samples',
  135. }),
  136. ],
  137. body: {
  138. data: [
  139. {
  140. span_id: 'b1bf1acde131623a',
  141. trace: '2b60b2eb415c4bfba3efeaf65c21c605',
  142. 'span.description':
  143. 'GET https://sentry.io/api/0/organizations/sentry/info/?projectId=1',
  144. project: 'javascript',
  145. timestamp: '2024-03-25T20:31:36+00:00',
  146. 'span.status_code': '200',
  147. 'transaction.id': '11c910c9c10b3ec4ecf8f209b8c6ce48',
  148. 'span.self_time': 320.300102,
  149. },
  150. ],
  151. meta: {},
  152. },
  153. });
  154. spanFieldTagsMock = MockApiClient.addMockResponse({
  155. url: `/organizations/${organization.slug}/spans/fields/`,
  156. method: 'GET',
  157. body: [
  158. {
  159. key: 'api_key',
  160. name: 'Api Key',
  161. },
  162. {
  163. key: 'bytes.size',
  164. name: 'Bytes.Size',
  165. },
  166. ],
  167. });
  168. });
  169. it('fetches panel data', async () => {
  170. render(<HTTPSamplesPanel />);
  171. expect(eventsRequestMock).toHaveBeenNthCalledWith(
  172. 1,
  173. `/organizations/${organization.slug}/events/`,
  174. expect.objectContaining({
  175. method: 'GET',
  176. query: {
  177. dataset: 'spansMetrics',
  178. environment: [],
  179. field: [
  180. 'spm()',
  181. 'avg(span.self_time)',
  182. 'sum(span.self_time)',
  183. 'http_response_rate(3)',
  184. 'http_response_rate(4)',
  185. 'http_response_rate(5)',
  186. 'time_spent_percentage()',
  187. ],
  188. per_page: 50,
  189. project: [],
  190. query:
  191. 'span.module:http span.op:http.client !has:span.domain transaction:/api/0/users',
  192. referrer: 'api.performance.http.samples-panel-metrics-ribbon',
  193. statsPeriod: '10d',
  194. },
  195. })
  196. );
  197. expect(eventsStatsRequestMock).toHaveBeenNthCalledWith(
  198. 1,
  199. `/organizations/${organization.slug}/events-stats/`,
  200. expect.objectContaining({
  201. method: 'GET',
  202. query: {
  203. cursor: undefined,
  204. dataset: 'spansMetrics',
  205. environment: [],
  206. excludeOther: 0,
  207. field: ['span.status_code', 'count()'],
  208. interval: '30m',
  209. orderby: '-count()',
  210. partial: 1,
  211. per_page: 50,
  212. project: [],
  213. query:
  214. 'span.module:http span.op:http.client !has:span.domain transaction:/api/0/users span.status_code:[300,301,302,303,304,305,307,308]',
  215. referrer: 'api.performance.http.samples-panel-response-code-chart',
  216. statsPeriod: '10d',
  217. sort: '-count()',
  218. topEvents: '5',
  219. yAxis: 'count()',
  220. },
  221. })
  222. );
  223. expect(samplesRequestMock).toHaveBeenNthCalledWith(
  224. 1,
  225. `/organizations/${organization.slug}/events/`,
  226. expect.objectContaining({
  227. method: 'GET',
  228. query: expect.objectContaining({
  229. dataset: 'spansIndexed',
  230. query:
  231. 'span.module:http span.op:http.client !has:span.domain transaction:/api/0/users span.status_code:[300,301,302,303,304,305,307,308]',
  232. project: [],
  233. field: [
  234. 'project',
  235. 'trace',
  236. 'transaction.id',
  237. 'span_id',
  238. 'timestamp',
  239. 'span.description',
  240. 'span.status_code',
  241. ],
  242. sort: '-span_id',
  243. referrer: 'api.performance.http.samples-panel-response-code-samples',
  244. statsPeriod: '10d',
  245. }),
  246. })
  247. );
  248. expect(spanFieldTagsMock).toHaveBeenNthCalledWith(
  249. 1,
  250. `/organizations/${organization.slug}/spans/fields/`,
  251. expect.objectContaining({
  252. method: 'GET',
  253. query: {
  254. project: [],
  255. environment: [],
  256. statsPeriod: '1h',
  257. },
  258. })
  259. );
  260. await waitForElementToBeRemoved(() => screen.queryAllByTestId('loading-indicator'));
  261. });
  262. it('shows basic transaction info', async () => {
  263. render(<HTTPSamplesPanel />);
  264. // Panel heading
  265. expect(screen.getByRole('heading', {name: 'GET /api/0/users'})).toBeInTheDocument();
  266. // Metrics ribbon
  267. await waitForElementToBeRemoved(() => screen.queryAllByTestId('loading-indicator'));
  268. expect(
  269. screen.getByRole('heading', {name: 'Requests Per Minute'})
  270. ).toBeInTheDocument();
  271. expect(screen.getByRole('heading', {name: 'Avg Duration'})).toBeInTheDocument();
  272. expect(screen.getByRole('heading', {name: '3XXs'})).toBeInTheDocument();
  273. expect(screen.getByRole('heading', {name: '4XXs'})).toBeInTheDocument();
  274. expect(screen.getByRole('heading', {name: '5XXs'})).toBeInTheDocument();
  275. expect(screen.getByRole('heading', {name: 'Time Spent'})).toBeInTheDocument();
  276. expect(screen.getByText('22.2/min')).toBeInTheDocument();
  277. expect(screen.getByText('140.20ms')).toBeInTheDocument();
  278. expect(screen.getByText('1%')).toBeInTheDocument();
  279. expect(screen.getByText('2.5%')).toBeInTheDocument();
  280. expect(screen.getByText('1.5%')).toBeInTheDocument();
  281. expect(screen.getByText('45.15min')).toBeInTheDocument();
  282. });
  283. });
  284. describe('Duration panel', () => {
  285. let chartRequestMock: jest.Mock;
  286. let samplesRequestMock: jest.Mock;
  287. let spanFieldTagsMock: jest.Mock;
  288. beforeEach(() => {
  289. jest.mocked(useLocation).mockReturnValue({
  290. pathname: '',
  291. search: '',
  292. query: {
  293. domain: '*.sentry.dev',
  294. statsPeriod: '10d',
  295. transaction: '/api/0/users',
  296. transactionMethod: 'GET',
  297. panel: 'duration',
  298. },
  299. hash: '',
  300. state: undefined,
  301. action: 'PUSH',
  302. key: '',
  303. });
  304. chartRequestMock = MockApiClient.addMockResponse({
  305. url: `/organizations/${organization.slug}/events-stats/`,
  306. method: 'GET',
  307. match: [
  308. MockApiClient.matchQuery({
  309. referrer: 'api.performance.http.samples-panel-duration-chart',
  310. }),
  311. ],
  312. body: {data: [[1711393200, [{count: 900}]]]},
  313. });
  314. samplesRequestMock = MockApiClient.addMockResponse({
  315. url: `/api/0/organizations/${organization.slug}/spans-samples/`,
  316. method: 'GET',
  317. body: {
  318. data: [
  319. {
  320. span_id: 'b1bf1acde131623a',
  321. trace: '2b60b2eb415c4bfba3efeaf65c21c605',
  322. 'span.description':
  323. 'GET https://sentry.io/api/0/organizations/sentry/info/?projectId=1',
  324. project: 'javascript',
  325. timestamp: '2024-03-25T20:31:36+00:00',
  326. 'span.status_code': '200',
  327. 'transaction.id': '11c910c9c10b3ec4ecf8f209b8c6ce48',
  328. 'span.self_time': 320.300102,
  329. },
  330. ],
  331. },
  332. });
  333. spanFieldTagsMock = MockApiClient.addMockResponse({
  334. url: `/organizations/${organization.slug}/spans/fields/`,
  335. method: 'GET',
  336. body: [
  337. {
  338. key: 'api_key',
  339. name: 'Api Key',
  340. },
  341. {
  342. key: 'bytes.size',
  343. name: 'Bytes.Size',
  344. },
  345. ],
  346. });
  347. });
  348. it('fetches panel data', async () => {
  349. render(<HTTPSamplesPanel />);
  350. await waitForElementToBeRemoved(() => screen.queryAllByTestId('loading-indicator'));
  351. expect(chartRequestMock).toHaveBeenNthCalledWith(
  352. 1,
  353. `/organizations/${organization.slug}/events-stats/`,
  354. expect.objectContaining({
  355. method: 'GET',
  356. query: expect.objectContaining({
  357. dataset: 'spansMetrics',
  358. environment: [],
  359. interval: '30m',
  360. per_page: 50,
  361. project: [],
  362. query:
  363. 'span.module:http span.op:http.client span.domain:"\\*.sentry.dev" transaction:/api/0/users',
  364. referrer: 'api.performance.http.samples-panel-duration-chart',
  365. statsPeriod: '10d',
  366. yAxis: 'avg(span.self_time)',
  367. }),
  368. })
  369. );
  370. expect(samplesRequestMock).toHaveBeenNthCalledWith(
  371. 1,
  372. `/api/0/organizations/${organization.slug}/spans-samples/`,
  373. expect.objectContaining({
  374. method: 'GET',
  375. query: expect.objectContaining({
  376. query:
  377. 'span.module:http span.op:http.client span.domain:"\\*.sentry.dev" transaction:/api/0/users',
  378. project: [],
  379. additionalFields: [
  380. 'trace',
  381. 'transaction.id',
  382. 'span.description',
  383. 'span.status_code',
  384. ],
  385. lowerBound: 0,
  386. firstBound: expect.closeTo(333.3333),
  387. secondBound: expect.closeTo(666.6666),
  388. upperBound: 1000,
  389. referrer: 'api.performance.http.samples-panel-duration-samples',
  390. statsPeriod: '10d',
  391. }),
  392. })
  393. );
  394. expect(spanFieldTagsMock).toHaveBeenNthCalledWith(
  395. 1,
  396. `/organizations/${organization.slug}/spans/fields/`,
  397. expect.objectContaining({
  398. method: 'GET',
  399. query: {
  400. project: [],
  401. environment: [],
  402. statsPeriod: '1h',
  403. },
  404. })
  405. );
  406. });
  407. it('show basic transaction info', async () => {
  408. render(<HTTPSamplesPanel />);
  409. // Panel heading
  410. expect(screen.getByRole('heading', {name: 'GET /api/0/users'})).toBeInTheDocument();
  411. // Metrics ribbon
  412. await waitForElementToBeRemoved(() => screen.queryAllByTestId('loading-indicator'));
  413. expect(
  414. screen.getByRole('heading', {name: 'Requests Per Minute'})
  415. ).toBeInTheDocument();
  416. expect(screen.getByRole('heading', {name: 'Avg Duration'})).toBeInTheDocument();
  417. expect(screen.getByRole('heading', {name: '3XXs'})).toBeInTheDocument();
  418. expect(screen.getByRole('heading', {name: '4XXs'})).toBeInTheDocument();
  419. expect(screen.getByRole('heading', {name: '5XXs'})).toBeInTheDocument();
  420. expect(screen.getByRole('heading', {name: 'Time Spent'})).toBeInTheDocument();
  421. expect(screen.getByText('22.2/min')).toBeInTheDocument();
  422. expect(screen.getByText('140.20ms')).toBeInTheDocument();
  423. expect(screen.getByText('1%')).toBeInTheDocument();
  424. expect(screen.getByText('2.5%')).toBeInTheDocument();
  425. expect(screen.getByText('1.5%')).toBeInTheDocument();
  426. expect(screen.getByText('45.15min')).toBeInTheDocument();
  427. // Samples table
  428. expect(screen.getByRole('table', {name: 'Span Samples'})).toBeInTheDocument();
  429. expect(screen.getByRole('columnheader', {name: 'Span ID'})).toBeInTheDocument();
  430. expect(screen.getByRole('columnheader', {name: 'Status'})).toBeInTheDocument();
  431. expect(screen.getByRole('columnheader', {name: 'URL'})).toBeInTheDocument();
  432. expect(screen.getByRole('cell', {name: 'b1bf1acde131623a'})).toBeInTheDocument();
  433. expect(screen.getByRole('link', {name: 'b1bf1acde131623a'})).toHaveAttribute(
  434. 'href',
  435. '/organizations/org-slug/performance/javascript:11c910c9c10b3ec4ecf8f209b8c6ce48/?domain=%2A.sentry.dev&panel=duration&statsPeriod=10d&transactionMethod=GET#span-b1bf1acde131623a'
  436. );
  437. expect(screen.getByRole('cell', {name: '200'})).toBeInTheDocument();
  438. });
  439. it('re-fetches samples', async () => {
  440. render(<HTTPSamplesPanel />);
  441. await waitForElementToBeRemoved(() => screen.queryAllByTestId('loading-indicator'));
  442. expect(samplesRequestMock).toHaveBeenCalledTimes(1);
  443. await userEvent.click(screen.getByRole('button', {name: 'Try Different Samples'}));
  444. expect(samplesRequestMock).toHaveBeenCalledTimes(2);
  445. });
  446. });
  447. });