httpSamplesPanel.spec.tsx 15 KB

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