test_project_settings_sampling.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. from selenium.webdriver.common.action_chains import ActionChains
  2. from selenium.webdriver.common.keys import Keys
  3. from sentry.api.endpoints.project_details import DynamicSamplingSerializer
  4. from sentry.models import ProjectOption
  5. from sentry.testutils import AcceptanceTestCase
  6. FEATURE_NAME = ["organizations:server-side-sampling", "organizations:server-side-sampling-ui"]
  7. uniform_rule_with_recommended_sampling_values = {
  8. "id": 1,
  9. "active": False,
  10. "type": "trace",
  11. "condition": {
  12. "op": "and",
  13. "inner": [],
  14. },
  15. "sampleRate": 0.1,
  16. }
  17. uniform_rule_with_custom_sampling_values = {
  18. "id": 1,
  19. "active": False,
  20. "type": "trace",
  21. "condition": {
  22. "op": "and",
  23. "inner": [],
  24. },
  25. "sampleRate": 0.5,
  26. }
  27. specific_rule_with_all_current_trace_conditions = {
  28. "id": 2,
  29. "type": "trace",
  30. "active": False,
  31. "condition": {
  32. "op": "and",
  33. "inner": [
  34. {
  35. "op": "eq",
  36. "name": "trace.environment",
  37. "value": ["prod", "production"],
  38. "options": {"ignoreCase": True},
  39. },
  40. {"op": "glob", "name": "trace.release", "value": ["frontend@22*"]},
  41. ],
  42. },
  43. "sampleRate": 0.3,
  44. }
  45. class ProjectSettingsSamplingTest(AcceptanceTestCase):
  46. def setUp(self):
  47. super().setUp()
  48. self.user = self.create_user("foo@example.com")
  49. self.org = self.create_organization(name="Rowdy Tiger", owner=None)
  50. self.team = self.create_team(organization=self.org, name="Mariachi Band")
  51. self.project = self.create_project(organization=self.org, teams=[self.team], name="Bengal")
  52. self.project.update_option(
  53. "sentry:dynamic_sampling",
  54. {
  55. "next_id": 1,
  56. "rules": [],
  57. },
  58. )
  59. self.create_member(user=self.user, organization=self.org, role="owner", teams=[self.team])
  60. self.login_as(self.user)
  61. self.path = f"/settings/{self.org.slug}/projects/{self.project.slug}/server-side-sampling/"
  62. def wait_until_page_loaded(self):
  63. self.browser.get(self.path)
  64. self.browser.wait_until_not('[data-test-id="loading-indicator"]')
  65. def test_add_uniform_rule_with_recommended_sampling_values(self):
  66. with self.feature(FEATURE_NAME):
  67. self.wait_until_page_loaded()
  68. # Open uniform rate modal
  69. self.browser.click_when_visible('[aria-label="Start Setup"]')
  70. self.browser.wait_until('[id="recommended-client-sampling"]')
  71. # Click on the recommended sampling values option
  72. self.browser.click_when_visible('[id="sampling-recommended"]')
  73. # Click on done button
  74. self.browser.click_when_visible('[aria-label="Done"]')
  75. # Wait the success message to show up
  76. self.browser.wait_until('[data-test-id="toast-success"]')
  77. # Validate the payload
  78. project_option = ProjectOption.objects.get(
  79. key="sentry:dynamic_sampling", project=self.project
  80. )
  81. saved_sampling_setting = project_option.value
  82. serializer = DynamicSamplingSerializer(data=saved_sampling_setting)
  83. assert serializer.is_valid()
  84. assert len(serializer.validated_data["rules"]) == 1
  85. assert saved_sampling_setting == serializer.validated_data
  86. assert (
  87. uniform_rule_with_recommended_sampling_values
  88. == serializer.validated_data["rules"][0]
  89. )
  90. def test_add_uniform_rule_with_custom_sampling_values(self):
  91. with self.feature(FEATURE_NAME):
  92. self.wait_until_page_loaded()
  93. # Open uniform rate modal
  94. self.browser.click_when_visible('[aria-label="Start Setup"]')
  95. self.browser.wait_until('[id="recommended-client-sampling"]')
  96. # Enter a custom value for client side sampling
  97. self.browser.element('[id="recommended-client-sampling"]').clear()
  98. self.browser.element('[id="recommended-client-sampling"]').send_keys(80, Keys.ENTER)
  99. # Enter a custom value for server side sampling
  100. self.browser.element('[id="recommended-server-sampling"]').clear()
  101. self.browser.element('[id="recommended-server-sampling"]').send_keys(50, Keys.ENTER)
  102. # Click on next button
  103. self.browser.click_when_visible('[aria-label="Next"]')
  104. # Click on done button
  105. self.browser.click_when_visible('[aria-label="Done"]')
  106. # Wait the success message to show up
  107. self.browser.wait_until('[data-test-id="toast-success"]')
  108. # Validate the payload
  109. project_option = ProjectOption.objects.get(
  110. key="sentry:dynamic_sampling", project=self.project
  111. )
  112. saved_sampling_setting = project_option.value
  113. serializer = DynamicSamplingSerializer(data=saved_sampling_setting)
  114. assert serializer.is_valid()
  115. assert len(serializer.validated_data["rules"]) == 1
  116. assert saved_sampling_setting == serializer.validated_data
  117. assert uniform_rule_with_custom_sampling_values == serializer.validated_data["rules"][0]
  118. def test_activate_uniform_rule(self):
  119. with self.feature(FEATURE_NAME):
  120. self.project.update_option(
  121. "sentry:dynamic_sampling",
  122. {
  123. "next_id": 2,
  124. "rules": [uniform_rule_with_recommended_sampling_values],
  125. },
  126. )
  127. self.wait_until_page_loaded()
  128. # Click on activate rule button
  129. self.browser.element('[aria-label="Activate Rule"]').click()
  130. # Wait the success message to show up
  131. self.browser.wait_until('[data-test-id="toast-success"]')
  132. # Validate the payload
  133. project_option = ProjectOption.objects.get(
  134. key="sentry:dynamic_sampling", project=self.project
  135. )
  136. saved_sampling_setting = project_option.value
  137. serializer = DynamicSamplingSerializer(data=saved_sampling_setting)
  138. assert serializer.is_valid()
  139. assert len(serializer.validated_data["rules"]) == 1
  140. assert saved_sampling_setting == serializer.validated_data
  141. assert {
  142. **uniform_rule_with_recommended_sampling_values,
  143. "active": True,
  144. "id": 2,
  145. } == serializer.validated_data["rules"][0]
  146. def test_deactivate_uniform_rule(self):
  147. with self.feature(FEATURE_NAME):
  148. self.project.update_option(
  149. "sentry:dynamic_sampling",
  150. {
  151. "next_id": 2,
  152. "rules": [{**uniform_rule_with_recommended_sampling_values, "active": True}],
  153. },
  154. )
  155. self.wait_until_page_loaded()
  156. # Click on deactivate rule button
  157. self.browser.element('[aria-label="Deactivate Rule"]').click()
  158. # Wait the success message to show up
  159. self.browser.wait_until('[data-test-id="toast-success"]')
  160. # Validate the payload
  161. project_option = ProjectOption.objects.get(
  162. key="sentry:dynamic_sampling", project=self.project
  163. )
  164. saved_sampling_setting = project_option.value
  165. serializer = DynamicSamplingSerializer(data=saved_sampling_setting)
  166. assert serializer.is_valid()
  167. assert len(serializer.validated_data["rules"]) == 1
  168. assert saved_sampling_setting == serializer.validated_data
  169. assert {
  170. **uniform_rule_with_recommended_sampling_values,
  171. "active": False,
  172. "id": 2,
  173. } == serializer.validated_data["rules"][0]
  174. def test_add_specific_rule(self):
  175. with self.feature(FEATURE_NAME):
  176. self.project.update_option(
  177. "sentry:dynamic_sampling",
  178. {
  179. "next_id": 2,
  180. "rules": [uniform_rule_with_recommended_sampling_values],
  181. },
  182. )
  183. self.wait_until_page_loaded()
  184. # Open specific rule modal
  185. self.browser.element('[aria-label="Add Rule"]').click()
  186. # Open conditions dropdown
  187. self.browser.element('[aria-label="Add Condition"]').click()
  188. # Add Environment
  189. self.browser.element('[data-test-id="trace.environment"]').click()
  190. # Add Release
  191. self.browser.element('[data-test-id="trace.release"]').click()
  192. # Fill in Environment
  193. self.browser.element('[aria-label="Search or add an environment"]').send_keys("prod")
  194. self.browser.wait_until_not('[data-test-id="loading-indicator"]')
  195. self.browser.element('[aria-label="Search or add an environment"]').send_keys(
  196. Keys.ENTER
  197. )
  198. self.browser.element('[aria-label="Search or add an environment"]').send_keys(
  199. "production"
  200. )
  201. self.browser.wait_until_not('[data-test-id="loading-indicator"]')
  202. self.browser.element('[aria-label="Search or add an environment"]').send_keys(
  203. Keys.ENTER
  204. )
  205. # Fill in Release
  206. self.browser.element('[aria-label="Search or add a release"]').send_keys("frontend@22*")
  207. self.browser.wait_until_not('[data-test-id="loading-indicator"]')
  208. self.browser.element('[aria-label="Search or add a release"]').send_keys(Keys.ENTER)
  209. # Fill in sample rate
  210. self.browser.element('[placeholder="%"]').send_keys("30")
  211. # Save rule
  212. self.browser.element('[aria-label="Save Rule"]').click()
  213. # Wait the success message to show up
  214. self.browser.wait_until('[data-test-id="toast-success"]')
  215. # Take a screenshot
  216. self.browser.snapshot("sampling settings rule with current trace conditions")
  217. # Validate the payload
  218. project_option = ProjectOption.objects.get(
  219. key="sentry:dynamic_sampling", project=self.project
  220. )
  221. saved_sampling_setting = project_option.value
  222. serializer = DynamicSamplingSerializer(data=saved_sampling_setting)
  223. assert serializer.is_valid()
  224. assert len(serializer.validated_data["rules"]) == 2
  225. assert saved_sampling_setting == serializer.validated_data
  226. assert (
  227. specific_rule_with_all_current_trace_conditions
  228. == serializer.validated_data["rules"][0]
  229. )
  230. def test_drag_and_drop_rule_error(self):
  231. with self.feature(FEATURE_NAME):
  232. self.project.update_option(
  233. "sentry:dynamic_sampling",
  234. {
  235. "next_id": 3,
  236. "rules": [
  237. {**specific_rule_with_all_current_trace_conditions, "id": 1},
  238. {**uniform_rule_with_recommended_sampling_values, "id": 2},
  239. ],
  240. },
  241. )
  242. self.wait_until_page_loaded()
  243. # Tries to drag specific rules below an uniform rule
  244. dragHandleSource = self.browser.elements(
  245. '[data-test-id="sampling-rule"] [aria-roledescription="sortable"]'
  246. )[0]
  247. dragHandleTarget = self.browser.elements(
  248. '[data-test-id="sampling-rule"] [aria-roledescription="sortable"]'
  249. )[1]
  250. action = ActionChains(self.browser.driver)
  251. action.drag_and_drop(dragHandleSource, dragHandleTarget)
  252. action.perform()
  253. self.browser.wait_until_test_id("toast-error")
  254. def test_drag_and_drop_rule_success(self):
  255. with self.feature(FEATURE_NAME):
  256. self.project.update_option(
  257. "sentry:dynamic_sampling",
  258. {
  259. "next_id": 4,
  260. "rules": [
  261. {
  262. **specific_rule_with_all_current_trace_conditions,
  263. "id": 1,
  264. "condition": {
  265. "op": "and",
  266. "inner": [
  267. {
  268. "op": "glob",
  269. "name": "trace.release",
  270. "value": ["[13].[19]"],
  271. }
  272. ],
  273. },
  274. },
  275. {
  276. **specific_rule_with_all_current_trace_conditions,
  277. "sampleRate": 0.8,
  278. "id": 2,
  279. "type": "trace",
  280. "condition": {
  281. "op": "and",
  282. "inner": [
  283. {
  284. "op": "eq",
  285. "name": "trace.environment",
  286. "value": ["production"],
  287. "options": {"ignoreCase": True},
  288. }
  289. ],
  290. },
  291. },
  292. {
  293. **uniform_rule_with_recommended_sampling_values,
  294. "id": 3,
  295. },
  296. ],
  297. },
  298. )
  299. self.wait_until_page_loaded()
  300. # Before
  301. rules_before = self.browser.elements('[data-test-id="sampling-rule"]')
  302. assert "Release" in rules_before[0].text
  303. assert "Environment" in rules_before[1].text
  304. drag_handle_source = self.browser.elements('[aria-roledescription="sortable"]')[1]
  305. dragHandleTarget = self.browser.elements('[aria-roledescription="sortable"]')[0]
  306. action = ActionChains(self.browser.driver)
  307. action.drag_and_drop(drag_handle_source, dragHandleTarget)
  308. action.perform()
  309. # Wait the success message to show up
  310. self.browser.wait_until('[data-test-id="toast-success"]')
  311. # After
  312. rulesAfter = self.browser.elements('[data-test-id="sampling-rule"]')
  313. assert "Environment" in rulesAfter[0].text
  314. assert "Release" in rulesAfter[1].text