httpSamplesPanel.spec.tsx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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/performance/http/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: undefined,
  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. topEvents: '5',
  216. yAxis: 'count()',
  217. },
  218. })
  219. );
  220. expect(samplesRequestMock).toHaveBeenNthCalledWith(
  221. 1,
  222. `/organizations/${organization.slug}/events/`,
  223. expect.objectContaining({
  224. method: 'GET',
  225. query: expect.objectContaining({
  226. dataset: 'spansIndexed',
  227. query:
  228. '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:[""] )',
  229. project: [],
  230. field: [
  231. 'project',
  232. 'trace',
  233. 'transaction.id',
  234. 'span_id',
  235. 'timestamp',
  236. 'span.description',
  237. 'span.status_code',
  238. ],
  239. sort: '-span_id',
  240. referrer: 'api.performance.http.samples-panel-response-code-samples',
  241. statsPeriod: '10d',
  242. }),
  243. })
  244. );
  245. expect(spanFieldTagsMock).toHaveBeenNthCalledWith(
  246. 1,
  247. `/organizations/${organization.slug}/spans/fields/`,
  248. expect.objectContaining({
  249. method: 'GET',
  250. query: {
  251. project: [],
  252. environment: [],
  253. statsPeriod: '1h',
  254. },
  255. })
  256. );
  257. await waitForElementToBeRemoved(() => screen.queryAllByTestId('loading-indicator'));
  258. });
  259. it('shows basic transaction info', async () => {
  260. render(<HTTPSamplesPanel />);
  261. // Panel heading
  262. expect(screen.getByRole('heading', {name: 'GET /api/0/users'})).toBeInTheDocument();
  263. // Metrics ribbon
  264. await waitForElementToBeRemoved(() => screen.queryAllByTestId('loading-indicator'));
  265. expect(
  266. screen.getByRole('heading', {name: 'Requests Per Minute'})
  267. ).toBeInTheDocument();
  268. expect(screen.getByRole('heading', {name: 'Avg Duration'})).toBeInTheDocument();
  269. expect(screen.getByRole('heading', {name: '3XXs'})).toBeInTheDocument();
  270. expect(screen.getByRole('heading', {name: '4XXs'})).toBeInTheDocument();
  271. expect(screen.getByRole('heading', {name: '5XXs'})).toBeInTheDocument();
  272. expect(screen.getByRole('heading', {name: 'Time Spent'})).toBeInTheDocument();
  273. expect(screen.getByText('22.2/min')).toBeInTheDocument();
  274. expect(screen.getByText('140.20ms')).toBeInTheDocument();
  275. expect(screen.getByText('1%')).toBeInTheDocument();
  276. expect(screen.getByText('2.5%')).toBeInTheDocument();
  277. expect(screen.getByText('1.5%')).toBeInTheDocument();
  278. expect(screen.getByText('45.15min')).toBeInTheDocument();
  279. });
  280. });
  281. describe('Duration panel', () => {
  282. let chartRequestMock, samplesRequestMock, spanFieldTagsMock;
  283. beforeEach(() => {
  284. jest.mocked(useLocation).mockReturnValue({
  285. pathname: '',
  286. search: '',
  287. query: {
  288. domain: '*.sentry.dev',
  289. statsPeriod: '10d',
  290. transaction: '/api/0/users',
  291. transactionMethod: 'GET',
  292. panel: 'duration',
  293. },
  294. hash: '',
  295. state: undefined,
  296. action: 'PUSH',
  297. key: '',
  298. });
  299. chartRequestMock = MockApiClient.addMockResponse({
  300. url: `/organizations/${organization.slug}/events-stats/`,
  301. method: 'GET',
  302. match: [
  303. MockApiClient.matchQuery({
  304. referrer: 'api.performance.http.samples-panel-duration-chart',
  305. }),
  306. ],
  307. body: {data: [[1711393200, [{count: 900}]]]},
  308. });
  309. samplesRequestMock = MockApiClient.addMockResponse({
  310. url: `/api/0/organizations/${organization.slug}/spans-samples/`,
  311. method: 'GET',
  312. body: {
  313. data: [
  314. {
  315. span_id: 'b1bf1acde131623a',
  316. trace: '2b60b2eb415c4bfba3efeaf65c21c605',
  317. 'span.description':
  318. 'GET https://sentry.io/api/0/organizations/sentry/info/?projectId=1',
  319. project: 'javascript',
  320. timestamp: '2024-03-25T20:31:36+00:00',
  321. 'span.status_code': '200',
  322. 'transaction.id': '11c910c9c10b3ec4ecf8f209b8c6ce48',
  323. 'span.self_time': 320.300102,
  324. },
  325. ],
  326. },
  327. });
  328. spanFieldTagsMock = MockApiClient.addMockResponse({
  329. url: `/organizations/${organization.slug}/spans/fields/`,
  330. method: 'GET',
  331. body: [
  332. {
  333. key: 'api_key',
  334. name: 'Api Key',
  335. },
  336. {
  337. key: 'bytes.size',
  338. name: 'Bytes.Size',
  339. },
  340. ],
  341. });
  342. });
  343. it('fetches panel data', async () => {
  344. render(<HTTPSamplesPanel />);
  345. await waitForElementToBeRemoved(() => screen.queryAllByTestId('loading-indicator'));
  346. expect(chartRequestMock).toHaveBeenNthCalledWith(
  347. 1,
  348. `/organizations/${organization.slug}/events-stats/`,
  349. expect.objectContaining({
  350. method: 'GET',
  351. query: expect.objectContaining({
  352. dataset: 'spansMetrics',
  353. environment: [],
  354. interval: '30m',
  355. per_page: 50,
  356. project: [],
  357. query:
  358. 'span.module:http span.op:http.client span.domain:"\\*.sentry.dev" transaction:/api/0/users',
  359. referrer: 'api.performance.http.samples-panel-duration-chart',
  360. statsPeriod: '10d',
  361. yAxis: 'avg(span.self_time)',
  362. }),
  363. })
  364. );
  365. expect(samplesRequestMock).toHaveBeenNthCalledWith(
  366. 1,
  367. `/api/0/organizations/${organization.slug}/spans-samples/`,
  368. expect.objectContaining({
  369. method: 'GET',
  370. query: expect.objectContaining({
  371. query:
  372. 'span.module:http span.op:http.client transaction:/api/0/users span.domain:"\\*.sentry.dev"',
  373. project: [],
  374. additionalFields: [
  375. 'trace',
  376. 'transaction.id',
  377. 'span.description',
  378. 'span.status_code',
  379. ],
  380. lowerBound: 0,
  381. firstBound: expect.closeTo(333.3333),
  382. secondBound: expect.closeTo(666.6666),
  383. upperBound: 1000,
  384. referrer: 'api.performance.http.samples-panel-duration-samples',
  385. statsPeriod: '10d',
  386. }),
  387. })
  388. );
  389. expect(spanFieldTagsMock).toHaveBeenNthCalledWith(
  390. 1,
  391. `/organizations/${organization.slug}/spans/fields/`,
  392. expect.objectContaining({
  393. method: 'GET',
  394. query: {
  395. project: [],
  396. environment: [],
  397. statsPeriod: '1h',
  398. },
  399. })
  400. );
  401. });
  402. it('show basic transaction info', async () => {
  403. render(<HTTPSamplesPanel />);
  404. // Panel heading
  405. expect(screen.getByRole('heading', {name: 'GET /api/0/users'})).toBeInTheDocument();
  406. // Metrics ribbon
  407. await waitForElementToBeRemoved(() => screen.queryAllByTestId('loading-indicator'));
  408. expect(
  409. screen.getByRole('heading', {name: 'Requests Per Minute'})
  410. ).toBeInTheDocument();
  411. expect(screen.getByRole('heading', {name: 'Avg Duration'})).toBeInTheDocument();
  412. expect(screen.getByRole('heading', {name: '3XXs'})).toBeInTheDocument();
  413. expect(screen.getByRole('heading', {name: '4XXs'})).toBeInTheDocument();
  414. expect(screen.getByRole('heading', {name: '5XXs'})).toBeInTheDocument();
  415. expect(screen.getByRole('heading', {name: 'Time Spent'})).toBeInTheDocument();
  416. expect(screen.getByText('22.2/min')).toBeInTheDocument();
  417. expect(screen.getByText('140.20ms')).toBeInTheDocument();
  418. expect(screen.getByText('1%')).toBeInTheDocument();
  419. expect(screen.getByText('2.5%')).toBeInTheDocument();
  420. expect(screen.getByText('1.5%')).toBeInTheDocument();
  421. expect(screen.getByText('45.15min')).toBeInTheDocument();
  422. // Samples table
  423. expect(screen.getByRole('table', {name: 'Span Samples'})).toBeInTheDocument();
  424. expect(screen.getByRole('columnheader', {name: 'Span ID'})).toBeInTheDocument();
  425. expect(screen.getByRole('columnheader', {name: 'Status'})).toBeInTheDocument();
  426. expect(screen.getByRole('columnheader', {name: 'URL'})).toBeInTheDocument();
  427. expect(screen.getByRole('cell', {name: 'b1bf1acde131623a'})).toBeInTheDocument();
  428. expect(screen.getByRole('link', {name: 'b1bf1acde131623a'})).toHaveAttribute(
  429. 'href',
  430. '/organizations/org-slug/performance/javascript:11c910c9c10b3ec4ecf8f209b8c6ce48/?domain=%2A.sentry.dev&panel=duration&statsPeriod=10d&transactionMethod=GET'
  431. );
  432. expect(screen.getByRole('cell', {name: '200'})).toBeInTheDocument();
  433. });
  434. it('re-fetches samples', async () => {
  435. render(<HTTPSamplesPanel />);
  436. await waitForElementToBeRemoved(() => screen.queryAllByTestId('loading-indicator'));
  437. expect(samplesRequestMock).toHaveBeenCalledTimes(1);
  438. await userEvent.click(screen.getByRole('button', {name: 'Try Different Samples'}));
  439. expect(samplesRequestMock).toHaveBeenCalledTimes(2);
  440. });
  441. });
  442. });