test_project_settings_sampling.py 14 KB

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