httpSamplesPanel.spec.tsx 15 KB

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