httpSamplesPanel.spec.tsx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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: 'span.module:http !has:span.domain transaction:/api/0/users',
  178. referrer: 'api.performance.http.samples-panel-metrics-ribbon',
  179. statsPeriod: '10d',
  180. },
  181. })
  182. );
  183. expect(eventsStatsRequestMock).toHaveBeenNthCalledWith(
  184. 1,
  185. `/organizations/${organization.slug}/events-stats/`,
  186. expect.objectContaining({
  187. method: 'GET',
  188. query: {
  189. cursor: undefined,
  190. dataset: 'spansMetrics',
  191. environment: [],
  192. excludeOther: 0,
  193. field: ['span.status_code', 'count()'],
  194. interval: '30m',
  195. orderby: undefined,
  196. partial: 1,
  197. per_page: 50,
  198. project: [],
  199. query:
  200. 'span.module:http !has:span.domain transaction:/api/0/users span.status_code:[300,301,302,303,304,305,307,308]',
  201. referrer: 'api.performance.http.samples-panel-response-code-chart',
  202. statsPeriod: '10d',
  203. topEvents: '5',
  204. yAxis: 'count()',
  205. },
  206. })
  207. );
  208. expect(samplesRequestMock).toHaveBeenNthCalledWith(
  209. 1,
  210. `/organizations/${organization.slug}/events/`,
  211. expect.objectContaining({
  212. method: 'GET',
  213. query: expect.objectContaining({
  214. dataset: 'spansIndexed',
  215. query:
  216. 'span.module:http transaction:/api/0/users span.status_code:[300,301,302,303,304,305,307,308] ( !has:span.domain OR span.domain:[""] )',
  217. project: [],
  218. field: [
  219. 'project',
  220. 'trace',
  221. 'transaction.id',
  222. 'span_id',
  223. 'timestamp',
  224. 'span.description',
  225. 'span.status_code',
  226. ],
  227. sort: '-span_id',
  228. referrer: 'api.performance.http.samples-panel-response-code-samples',
  229. statsPeriod: '10d',
  230. }),
  231. })
  232. );
  233. await waitForElementToBeRemoved(() => screen.queryAllByTestId('loading-indicator'));
  234. });
  235. it('shows basic transaction info', async () => {
  236. render(<HTTPSamplesPanel />);
  237. // Panel heading
  238. expect(screen.getByRole('heading', {name: 'GET /api/0/users'})).toBeInTheDocument();
  239. // Metrics ribbon
  240. await waitForElementToBeRemoved(() => screen.queryAllByTestId('loading-indicator'));
  241. expect(
  242. screen.getByRole('heading', {name: 'Requests Per Minute'})
  243. ).toBeInTheDocument();
  244. expect(screen.getByRole('heading', {name: 'Avg Duration'})).toBeInTheDocument();
  245. expect(screen.getByRole('heading', {name: '3XXs'})).toBeInTheDocument();
  246. expect(screen.getByRole('heading', {name: '4XXs'})).toBeInTheDocument();
  247. expect(screen.getByRole('heading', {name: '5XXs'})).toBeInTheDocument();
  248. expect(screen.getByRole('heading', {name: 'Time Spent'})).toBeInTheDocument();
  249. expect(screen.getByText('22.2/min')).toBeInTheDocument();
  250. expect(screen.getByText('140.20ms')).toBeInTheDocument();
  251. expect(screen.getByText('1%')).toBeInTheDocument();
  252. expect(screen.getByText('2.5%')).toBeInTheDocument();
  253. expect(screen.getByText('1.5%')).toBeInTheDocument();
  254. expect(screen.getByText('45.15min')).toBeInTheDocument();
  255. });
  256. });
  257. describe('Duration panel', () => {
  258. let chartRequestMock, samplesRequestMock;
  259. beforeEach(() => {
  260. jest.mocked(useLocation).mockReturnValue({
  261. pathname: '',
  262. search: '',
  263. query: {
  264. domain: '*.sentry.dev',
  265. statsPeriod: '10d',
  266. transaction: '/api/0/users',
  267. transactionMethod: 'GET',
  268. panel: 'duration',
  269. },
  270. hash: '',
  271. state: undefined,
  272. action: 'PUSH',
  273. key: '',
  274. });
  275. chartRequestMock = MockApiClient.addMockResponse({
  276. url: `/organizations/${organization.slug}/events-stats/`,
  277. method: 'GET',
  278. match: [
  279. MockApiClient.matchQuery({
  280. referrer: 'api.performance.http.samples-panel-duration-chart',
  281. }),
  282. ],
  283. body: {data: [[1711393200, [{count: 900}]]]},
  284. });
  285. samplesRequestMock = MockApiClient.addMockResponse({
  286. url: `/api/0/organizations/${organization.slug}/spans-samples/`,
  287. method: 'GET',
  288. body: {
  289. data: [
  290. {
  291. span_id: 'b1bf1acde131623a',
  292. trace: '2b60b2eb415c4bfba3efeaf65c21c605',
  293. 'span.description':
  294. 'GET https://sentry.io/api/0/organizations/sentry/info/?projectId=1',
  295. project: 'javascript',
  296. timestamp: '2024-03-25T20:31:36+00:00',
  297. 'span.status_code': '200',
  298. 'transaction.id': '11c910c9c10b3ec4ecf8f209b8c6ce48',
  299. 'span.self_time': 320.300102,
  300. },
  301. ],
  302. },
  303. });
  304. });
  305. it('fetches panel data', async () => {
  306. render(<HTTPSamplesPanel />);
  307. await waitForElementToBeRemoved(() => screen.queryAllByTestId('loading-indicator'));
  308. expect(chartRequestMock).toHaveBeenNthCalledWith(
  309. 1,
  310. `/organizations/${organization.slug}/events-stats/`,
  311. expect.objectContaining({
  312. method: 'GET',
  313. query: expect.objectContaining({
  314. dataset: 'spansMetrics',
  315. environment: [],
  316. interval: '30m',
  317. per_page: 50,
  318. project: [],
  319. query:
  320. 'span.module:http span.domain:"\\*.sentry.dev" transaction:/api/0/users',
  321. referrer: 'api.performance.http.samples-panel-duration-chart',
  322. statsPeriod: '10d',
  323. yAxis: 'avg(span.self_time)',
  324. }),
  325. })
  326. );
  327. expect(samplesRequestMock).toHaveBeenNthCalledWith(
  328. 1,
  329. `/api/0/organizations/${organization.slug}/spans-samples/`,
  330. expect.objectContaining({
  331. method: 'GET',
  332. query: expect.objectContaining({
  333. query:
  334. 'span.module:http transaction:/api/0/users span.domain:"\\*.sentry.dev"',
  335. project: [],
  336. additionalFields: [
  337. 'trace',
  338. 'transaction.id',
  339. 'span.description',
  340. 'span.status_code',
  341. ],
  342. lowerBound: 0,
  343. firstBound: expect.closeTo(333.3333),
  344. secondBound: expect.closeTo(666.6666),
  345. upperBound: 1000,
  346. referrer: 'api.performance.http.samples-panel-duration-samples',
  347. statsPeriod: '10d',
  348. }),
  349. })
  350. );
  351. });
  352. it('show basic transaction info', async () => {
  353. render(<HTTPSamplesPanel />);
  354. // Panel heading
  355. expect(screen.getByRole('heading', {name: 'GET /api/0/users'})).toBeInTheDocument();
  356. // Metrics ribbon
  357. await waitForElementToBeRemoved(() => screen.queryAllByTestId('loading-indicator'));
  358. expect(
  359. screen.getByRole('heading', {name: 'Requests Per Minute'})
  360. ).toBeInTheDocument();
  361. expect(screen.getByRole('heading', {name: 'Avg Duration'})).toBeInTheDocument();
  362. expect(screen.getByRole('heading', {name: '3XXs'})).toBeInTheDocument();
  363. expect(screen.getByRole('heading', {name: '4XXs'})).toBeInTheDocument();
  364. expect(screen.getByRole('heading', {name: '5XXs'})).toBeInTheDocument();
  365. expect(screen.getByRole('heading', {name: 'Time Spent'})).toBeInTheDocument();
  366. expect(screen.getByText('22.2/min')).toBeInTheDocument();
  367. expect(screen.getByText('140.20ms')).toBeInTheDocument();
  368. expect(screen.getByText('1%')).toBeInTheDocument();
  369. expect(screen.getByText('2.5%')).toBeInTheDocument();
  370. expect(screen.getByText('1.5%')).toBeInTheDocument();
  371. expect(screen.getByText('45.15min')).toBeInTheDocument();
  372. // Samples table
  373. expect(screen.getByRole('table', {name: 'Span Samples'})).toBeInTheDocument();
  374. expect(screen.getByRole('columnheader', {name: 'Span ID'})).toBeInTheDocument();
  375. expect(screen.getByRole('columnheader', {name: 'Status'})).toBeInTheDocument();
  376. expect(screen.getByRole('columnheader', {name: 'URL'})).toBeInTheDocument();
  377. expect(screen.getByRole('cell', {name: 'b1bf1acde131623a'})).toBeInTheDocument();
  378. expect(screen.getByRole('link', {name: 'b1bf1acde131623a'})).toHaveAttribute(
  379. 'href',
  380. '/organizations/org-slug/performance/javascript:11c910c9c10b3ec4ecf8f209b8c6ce48/?domain=%2A.sentry.dev&panel=duration&statsPeriod=10d&transactionMethod=GET'
  381. );
  382. expect(screen.getByRole('cell', {name: '200'})).toBeInTheDocument();
  383. });
  384. it('re-fetches samples', async () => {
  385. render(<HTTPSamplesPanel />);
  386. await waitForElementToBeRemoved(() => screen.queryAllByTestId('loading-indicator'));
  387. expect(samplesRequestMock).toHaveBeenCalledTimes(1);
  388. await userEvent.click(screen.getByRole('button', {name: 'Try Different Samples'}));
  389. expect(samplesRequestMock).toHaveBeenCalledTimes(2);
  390. });
  391. });
  392. });