httpSamplesPanel.spec.tsx 11 KB

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