test_project_settings_sampling.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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"
  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": 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. "op": "eq",
  43. "name": "trace.user.segment",
  44. "value": ["paid", "common"],
  45. "options": {"ignoreCase": True},
  46. },
  47. ],
  48. },
  49. "sampleRate": 0.3,
  50. }
  51. class ProjectSettingsSamplingTest(AcceptanceTestCase):
  52. def setUp(self):
  53. super().setUp()
  54. self.user = self.create_user("foo@example.com")
  55. self.org = self.create_organization(name="Rowdy Tiger", owner=None)
  56. self.team = self.create_team(organization=self.org, name="Mariachi Band")
  57. self.project = self.create_project(organization=self.org, teams=[self.team], name="Bengal")
  58. self.project.update_option(
  59. "sentry:dynamic_sampling",
  60. {
  61. "next_id": 1,
  62. "rules": [],
  63. },
  64. )
  65. self.create_member(user=self.user, organization=self.org, role="owner", teams=[self.team])
  66. self.login_as(self.user)
  67. self.path = f"/settings/{self.org.slug}/projects/{self.project.slug}/server-side-sampling/"
  68. def wait_until_page_loaded(self):
  69. self.browser.get(self.path)
  70. self.browser.wait_until_not('[data-test-id="loading-indicator"]')
  71. def test_add_uniform_rule_with_recommended_sampling_values(self):
  72. with self.feature(FEATURE_NAME):
  73. self.wait_until_page_loaded()
  74. # Open uniform rate modal
  75. self.browser.element('[aria-label="Start Setup"]').click()
  76. # Click on the recommended sampling values option
  77. self.browser.element('[id="sampling-recommended"]').click()
  78. # Click on done button
  79. self.browser.element('[aria-label="Done"]').click()
  80. # Wait the success message to show up
  81. self.browser.wait_until('[data-test-id="toast-success"]')
  82. # Validate the payload
  83. project_option = ProjectOption.objects.get(
  84. key="sentry:dynamic_sampling", project=self.project
  85. )
  86. saved_sampling_setting = project_option.value
  87. serializer = DynamicSamplingSerializer(data=saved_sampling_setting)
  88. assert serializer.is_valid()
  89. assert len(serializer.validated_data["rules"]) == 1
  90. assert saved_sampling_setting == serializer.validated_data
  91. assert (
  92. uniform_rule_with_recommended_sampling_values
  93. == serializer.validated_data["rules"][0]
  94. )
  95. def test_add_uniform_rule_with_custom_sampling_values(self):
  96. with self.feature(FEATURE_NAME):
  97. self.wait_until_page_loaded()
  98. # Open uniform rate modal
  99. self.browser.element('[aria-label="Start Setup"]').click()
  100. # Enter a custom value for client side sampling
  101. self.browser.element('[id="recommended-client-sampling"]').clear()
  102. self.browser.element('[id="recommended-client-sampling"]').send_keys(80, Keys.ENTER)
  103. # Enter a custom value for server side sampling
  104. self.browser.element('[id="recommended-server-sampling"]').clear()
  105. self.browser.element('[id="recommended-server-sampling"]').send_keys(50, Keys.ENTER)
  106. # Click on next button
  107. self.browser.element('[aria-label="Next"]').click()
  108. # Click on done button
  109. self.browser.element('[aria-label="Done"]').click()
  110. # Wait the success message to show up
  111. self.browser.wait_until('[data-test-id="toast-success"]')
  112. # Validate the payload
  113. project_option = ProjectOption.objects.get(
  114. key="sentry:dynamic_sampling", project=self.project
  115. )
  116. saved_sampling_setting = project_option.value
  117. serializer = DynamicSamplingSerializer(data=saved_sampling_setting)
  118. assert serializer.is_valid()
  119. assert len(serializer.validated_data["rules"]) == 1
  120. assert saved_sampling_setting == serializer.validated_data
  121. assert uniform_rule_with_custom_sampling_values == serializer.validated_data["rules"][0]
  122. def test_activate_uniform_rule(self):
  123. with self.feature(FEATURE_NAME):
  124. self.project.update_option(
  125. "sentry:dynamic_sampling",
  126. {
  127. "next_id": 2,
  128. "rules": [uniform_rule_with_recommended_sampling_values],
  129. },
  130. )
  131. self.wait_until_page_loaded()
  132. # Click on activate rule button
  133. self.browser.element('[aria-label="Activate Rule"]').click()
  134. # Wait the success message to show up
  135. self.browser.wait_until('[data-test-id="toast-success"]')
  136. # Validate the payload
  137. project_option = ProjectOption.objects.get(
  138. key="sentry:dynamic_sampling", project=self.project
  139. )
  140. saved_sampling_setting = project_option.value
  141. serializer = DynamicSamplingSerializer(data=saved_sampling_setting)
  142. assert serializer.is_valid()
  143. assert len(serializer.validated_data["rules"]) == 1
  144. assert saved_sampling_setting == serializer.validated_data
  145. assert {
  146. **uniform_rule_with_recommended_sampling_values,
  147. "active": True,
  148. "id": 2,
  149. } == serializer.validated_data["rules"][0]
  150. def test_deactivate_uniform_rule(self):
  151. with self.feature(FEATURE_NAME):
  152. self.project.update_option(
  153. "sentry:dynamic_sampling",
  154. {
  155. "next_id": 2,
  156. "rules": [{**uniform_rule_with_recommended_sampling_values, "active": True}],
  157. },
  158. )
  159. self.wait_until_page_loaded()
  160. # Click on deactivate rule button
  161. self.browser.element('[aria-label="Deactivate Rule"]').click()
  162. # Wait the success message to show up
  163. self.browser.wait_until('[data-test-id="toast-success"]')
  164. # Validate the payload
  165. project_option = ProjectOption.objects.get(
  166. key="sentry:dynamic_sampling", project=self.project
  167. )
  168. saved_sampling_setting = project_option.value
  169. serializer = DynamicSamplingSerializer(data=saved_sampling_setting)
  170. assert serializer.is_valid()
  171. assert len(serializer.validated_data["rules"]) == 1
  172. assert saved_sampling_setting == serializer.validated_data
  173. assert {
  174. **uniform_rule_with_recommended_sampling_values,
  175. "active": False,
  176. "id": 2,
  177. } == serializer.validated_data["rules"][0]
  178. def test_add_specific_rule(self):
  179. with self.feature(FEATURE_NAME):
  180. self.project.update_option(
  181. "sentry:dynamic_sampling",
  182. {
  183. "next_id": 2,
  184. "rules": [uniform_rule_with_recommended_sampling_values],
  185. },
  186. )
  187. self.wait_until_page_loaded()
  188. # Open specific rule modal
  189. self.browser.element('[aria-label="Add Rule"]').click()
  190. # Open conditions dropdown
  191. self.browser.element('[aria-label="Add Condition"]').click()
  192. # Add Environment
  193. self.browser.element('[data-test-id="trace.environment"]').click()
  194. # Add Release
  195. self.browser.element('[data-test-id="trace.release"]').click()
  196. # Add User Segment
  197. self.browser.element('[data-test-id="trace.user.segment"]').click()
  198. # Fill in Environment
  199. self.browser.element('[aria-label="Search or add an environment"]').send_keys(
  200. "prod", Keys.ENTER, "production", Keys.ENTER
  201. )
  202. # Fill in Release
  203. self.browser.element('[aria-label="Search or add a release"]').send_keys(
  204. "frontend@22*", Keys.ENTER
  205. )
  206. # Fill in User Segment
  207. self.browser.element('[placeholder="ex. paid, common (Multiline)"]').send_keys(
  208. "paid\ncommon"
  209. )
  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