httpSamplesPanel.spec.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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/`,
  103. method: 'GET',
  104. match: [
  105. MockApiClient.matchQuery({
  106. referrer: 'api.starfish.http-module-samples-panel-response-bar-chart',
  107. }),
  108. ],
  109. body: {},
  110. });
  111. });
  112. it('fetches panel data', async () => {
  113. render(<HTTPSamplesPanel />);
  114. expect(ribbonRequestMock).toHaveBeenNthCalledWith(
  115. 1,
  116. `/organizations/${organization.slug}/events/`,
  117. expect.objectContaining({
  118. method: 'GET',
  119. query: {
  120. dataset: 'spansMetrics',
  121. environment: [],
  122. field: [
  123. 'spm()',
  124. 'avg(span.self_time)',
  125. 'sum(span.self_time)',
  126. 'http_response_rate(3)',
  127. 'http_response_rate(4)',
  128. 'http_response_rate(5)',
  129. 'time_spent_percentage()',
  130. ],
  131. per_page: 50,
  132. project: [],
  133. query:
  134. 'span.module:http span.domain:"\\*.sentry.dev" transaction:/api/0/users',
  135. referrer: 'api.starfish.http-module-samples-panel-metrics-ribbon',
  136. statsPeriod: '10d',
  137. },
  138. })
  139. );
  140. expect(chartRequestMock).toHaveBeenNthCalledWith(
  141. 1,
  142. `/organizations/${organization.slug}/events/`,
  143. expect.objectContaining({
  144. method: 'GET',
  145. query: {
  146. dataset: 'spansMetrics',
  147. environment: [],
  148. field: ['span.status_code', 'count()'],
  149. per_page: 50,
  150. sort: 'span.status_code',
  151. project: [],
  152. query:
  153. 'span.module:http span.domain:"\\*.sentry.dev" transaction:/api/0/users',
  154. referrer: 'api.starfish.http-module-samples-panel-response-bar-chart',
  155. statsPeriod: '10d',
  156. },
  157. })
  158. );
  159. await waitForElementToBeRemoved(() => screen.queryAllByTestId('loading-indicator'));
  160. });
  161. it('shows basic transaction info', async () => {
  162. render(<HTTPSamplesPanel />);
  163. // Panel heading
  164. expect(screen.getByRole('heading', {name: 'GET /api/0/users'})).toBeInTheDocument();
  165. // Metrics ribbon
  166. await waitForElementToBeRemoved(() => screen.queryAllByTestId('loading-indicator'));
  167. expect(
  168. screen.getByRole('heading', {name: 'Requests Per Minute'})
  169. ).toBeInTheDocument();
  170. expect(screen.getByRole('heading', {name: 'Avg Duration'})).toBeInTheDocument();
  171. expect(screen.getByRole('heading', {name: '3XXs'})).toBeInTheDocument();
  172. expect(screen.getByRole('heading', {name: '4XXs'})).toBeInTheDocument();
  173. expect(screen.getByRole('heading', {name: '5XXs'})).toBeInTheDocument();
  174. expect(screen.getByRole('heading', {name: 'Time Spent'})).toBeInTheDocument();
  175. expect(screen.getByText('22.2/min')).toBeInTheDocument();
  176. expect(screen.getByText('140.20ms')).toBeInTheDocument();
  177. expect(screen.getByText('1%')).toBeInTheDocument();
  178. expect(screen.getByText('2.5%')).toBeInTheDocument();
  179. expect(screen.getByText('1.5%')).toBeInTheDocument();
  180. expect(screen.getByText('45.15min')).toBeInTheDocument();
  181. });
  182. });
  183. describe('Duration panel', () => {
  184. let chartRequestMock, samplesRequestMock;
  185. beforeEach(() => {
  186. jest.mocked(useLocation).mockReturnValue({
  187. pathname: '',
  188. search: '',
  189. query: {
  190. domain: '*.sentry.dev',
  191. statsPeriod: '10d',
  192. transaction: '/api/0/users',
  193. transactionMethod: 'GET',
  194. panel: 'duration',
  195. },
  196. hash: '',
  197. state: undefined,
  198. action: 'PUSH',
  199. key: '',
  200. });
  201. chartRequestMock = MockApiClient.addMockResponse({
  202. url: `/organizations/${organization.slug}/events-stats/`,
  203. method: 'GET',
  204. match: [
  205. MockApiClient.matchQuery({
  206. referrer: 'api.starfish.http-module-samples-panel-duration-chart',
  207. }),
  208. ],
  209. body: {data: [[1711393200, [{count: 900}]]]},
  210. });
  211. samplesRequestMock = MockApiClient.addMockResponse({
  212. url: `/api/0/organizations/${organization.slug}/spans-samples/`,
  213. method: 'GET',
  214. body: {
  215. data: [
  216. {
  217. span_id: 'b1bf1acde131623a',
  218. 'span.description':
  219. 'GET https://sentry.io/api/0/organizations/sentry/info/?projectId=1',
  220. project: 'javascript',
  221. timestamp: '2024-03-25T20:31:36+00:00',
  222. 'span.status_code': '200',
  223. 'transaction.id': '11c910c9c10b3ec4ecf8f209b8c6ce48',
  224. 'span.self_time': 320.300102,
  225. },
  226. ],
  227. },
  228. });
  229. });
  230. it('fetches panel data', async () => {
  231. render(<HTTPSamplesPanel />);
  232. await waitForElementToBeRemoved(() => screen.queryAllByTestId('loading-indicator'));
  233. expect(chartRequestMock).toHaveBeenNthCalledWith(
  234. 1,
  235. `/organizations/${organization.slug}/events-stats/`,
  236. expect.objectContaining({
  237. method: 'GET',
  238. query: expect.objectContaining({
  239. dataset: 'spansMetrics',
  240. environment: [],
  241. interval: '30m',
  242. per_page: 50,
  243. project: [],
  244. query:
  245. 'span.module:http span.domain:"\\*.sentry.dev" transaction:/api/0/users',
  246. referrer: 'api.starfish.http-module-samples-panel-duration-chart',
  247. statsPeriod: '10d',
  248. yAxis: 'avg(span.self_time)',
  249. }),
  250. })
  251. );
  252. expect(samplesRequestMock).toHaveBeenNthCalledWith(
  253. 1,
  254. `/api/0/organizations/${organization.slug}/spans-samples/`,
  255. expect.objectContaining({
  256. method: 'GET',
  257. query: expect.objectContaining({
  258. query:
  259. 'span.module:http span.domain:"\\*.sentry.dev" transaction:/api/0/users',
  260. project: [],
  261. additionalFields: ['transaction.id', 'span.description', 'span.status_code'],
  262. lowerBound: 0,
  263. firstBound: expect.closeTo(333.3333),
  264. secondBound: expect.closeTo(666.6666),
  265. upperBound: 1000,
  266. referrer: 'api.starfish.http-module-samples-panel-samples',
  267. statsPeriod: '10d',
  268. }),
  269. })
  270. );
  271. });
  272. it('show basic transaction info', async () => {
  273. render(<HTTPSamplesPanel />);
  274. // Panel heading
  275. expect(screen.getByRole('heading', {name: 'GET /api/0/users'})).toBeInTheDocument();
  276. // Metrics ribbon
  277. await waitForElementToBeRemoved(() => screen.queryAllByTestId('loading-indicator'));
  278. expect(
  279. screen.getByRole('heading', {name: 'Requests Per Minute'})
  280. ).toBeInTheDocument();
  281. expect(screen.getByRole('heading', {name: 'Avg Duration'})).toBeInTheDocument();
  282. expect(screen.getByRole('heading', {name: '3XXs'})).toBeInTheDocument();
  283. expect(screen.getByRole('heading', {name: '4XXs'})).toBeInTheDocument();
  284. expect(screen.getByRole('heading', {name: '5XXs'})).toBeInTheDocument();
  285. expect(screen.getByRole('heading', {name: 'Time Spent'})).toBeInTheDocument();
  286. expect(screen.getByText('22.2/min')).toBeInTheDocument();
  287. expect(screen.getByText('140.20ms')).toBeInTheDocument();
  288. expect(screen.getByText('1%')).toBeInTheDocument();
  289. expect(screen.getByText('2.5%')).toBeInTheDocument();
  290. expect(screen.getByText('1.5%')).toBeInTheDocument();
  291. expect(screen.getByText('45.15min')).toBeInTheDocument();
  292. // Samples table
  293. expect(screen.getByRole('table', {name: 'Span Samples'})).toBeInTheDocument();
  294. expect(screen.getByRole('columnheader', {name: 'Event ID'})).toBeInTheDocument();
  295. expect(screen.getByRole('columnheader', {name: 'Status'})).toBeInTheDocument();
  296. expect(screen.getByRole('columnheader', {name: 'URL'})).toBeInTheDocument();
  297. expect(screen.getByRole('cell', {name: '11c910c9'})).toBeInTheDocument();
  298. expect(screen.getByRole('link', {name: '11c910c9'})).toHaveAttribute(
  299. 'href',
  300. '/organizations/org-slug/performance/javascript:11c910c9c10b3ec4ecf8f209b8c6ce48#span-b1bf1acde131623a'
  301. );
  302. expect(screen.getByRole('cell', {name: '200'})).toBeInTheDocument();
  303. });
  304. });
  305. });