test_project_settings_sampling.py 15 KB

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