httpSamplesPanel.spec.tsx 14 KB

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