test_project_settings_sampling.py 14 KB

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