test_project_settings_sampling.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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(
  203. "prod", Keys.ENTER, "production", Keys.ENTER
  204. )
  205. # Fill in Release
  206. self.browser.element('[aria-label="Search or add a release"]').send_keys(
  207. "frontend@22*", Keys.ENTER
  208. )
  209. # Fill in User Segment
  210. self.browser.element('[placeholder="ex. paid, common (Multiline)"]').send_keys(
  211. "paid\ncommon"
  212. )
  213. # Fill in sample rate
  214. self.browser.element('[placeholder="%"]').send_keys("30")
  215. # Save rule
  216. self.browser.element('[aria-label="Save Rule"]').click()
  217. # Wait the success message to show up
  218. self.browser.wait_until('[data-test-id="toast-success"]')
  219. # Take a screenshot
  220. self.browser.snapshot("sampling settings rule with current trace conditions")
  221. # Validate the payload
  222. project_option = ProjectOption.objects.get(
  223. key="sentry:dynamic_sampling", project=self.project
  224. )
  225. saved_sampling_setting = project_option.value
  226. serializer = DynamicSamplingSerializer(data=saved_sampling_setting)
  227. assert serializer.is_valid()
  228. assert len(serializer.validated_data["rules"]) == 2
  229. assert saved_sampling_setting == serializer.validated_data
  230. assert (
  231. specific_rule_with_all_current_trace_conditions
  232. == serializer.validated_data["rules"][0]
  233. )
  234. def test_drag_and_drop_rule_error(self):
  235. with self.feature(FEATURE_NAME):
  236. self.project.update_option(
  237. "sentry:dynamic_sampling",
  238. {
  239. "next_id": 3,
  240. "rules": [
  241. {**specific_rule_with_all_current_trace_conditions, "id": 1},
  242. {**uniform_rule_with_recommended_sampling_values, "id": 2},
  243. ],
  244. },
  245. )
  246. self.wait_until_page_loaded()
  247. # Tries to drag specific rules below an uniform rule
  248. dragHandleSource = self.browser.elements(
  249. '[data-test-id="sampling-rule"] [aria-roledescription="sortable"]'
  250. )[0]
  251. dragHandleTarget = self.browser.elements(
  252. '[data-test-id="sampling-rule"] [aria-roledescription="sortable"]'
  253. )[1]
  254. action = ActionChains(self.browser.driver)
  255. action.drag_and_drop(dragHandleSource, dragHandleTarget)
  256. action.perform()
  257. self.browser.wait_until_test_id("toast-error")
  258. def test_drag_and_drop_rule_success(self):
  259. with self.feature(FEATURE_NAME):
  260. self.project.update_option(
  261. "sentry:dynamic_sampling",
  262. {
  263. "next_id": 4,
  264. "rules": [
  265. {
  266. **specific_rule_with_all_current_trace_conditions,
  267. "id": 1,
  268. "condition": {
  269. "op": "and",
  270. "inner": [
  271. {
  272. "op": "glob",
  273. "name": "trace.release",
  274. "value": ["[13].[19]"],
  275. }
  276. ],
  277. },
  278. },
  279. {
  280. **specific_rule_with_all_current_trace_conditions,
  281. "sampleRate": 0.8,
  282. "id": 2,
  283. "type": "trace",
  284. "condition": {
  285. "op": "and",
  286. "inner": [
  287. {
  288. "op": "eq",
  289. "name": "trace.environment",
  290. "value": ["production"],
  291. "options": {"ignoreCase": True},
  292. }
  293. ],
  294. },
  295. },
  296. {
  297. **uniform_rule_with_recommended_sampling_values,
  298. "id": 3,
  299. },
  300. ],
  301. },
  302. )
  303. self.wait_until_page_loaded()
  304. # Before
  305. rules_before = self.browser.elements('[data-test-id="sampling-rule"]')
  306. assert "Release" in rules_before[0].text
  307. assert "Environment" in rules_before[1].text
  308. drag_handle_source = self.browser.elements('[aria-roledescription="sortable"]')[1]
  309. dragHandleTarget = self.browser.elements('[aria-roledescription="sortable"]')[0]
  310. action = ActionChains(self.browser.driver)
  311. action.drag_and_drop(drag_handle_source, dragHandleTarget)
  312. action.perform()
  313. # Wait the success message to show up
  314. self.browser.wait_until('[data-test-id="toast-success"]')
  315. # After
  316. rulesAfter = self.browser.elements('[data-test-id="sampling-rule"]')
  317. assert "Environment" in rulesAfter[0].text
  318. assert "Release" in rulesAfter[1].text