test_organization_metrics_meta.py 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. import pytest
  2. from django.urls import reverse
  3. from sentry.testutils import MetricsEnhancedPerformanceTestCase
  4. from sentry.testutils.helpers.datetime import before_now, iso_format
  5. from sentry.testutils.silo import region_silo_test
  6. pytestmark = pytest.mark.sentry_metrics
  7. @region_silo_test
  8. class OrganizationMetricsCompatiblity(MetricsEnhancedPerformanceTestCase):
  9. def setUp(self):
  10. super().setUp()
  11. self.min_ago = before_now(minutes=1)
  12. self.two_min_ago = before_now(minutes=2)
  13. self.features = {
  14. "organizations:performance-use-metrics": True,
  15. }
  16. self.login_as(user=self.user)
  17. # Don't create any txn on this, don't set its DS rules, it shouldn't show up anywhere
  18. self.bad_project = self.create_project()
  19. def test_unparameterized_transactions(self):
  20. # Make current project incompatible
  21. self.store_transaction_metric(
  22. 1, tags={"transaction": "<< unparameterized >>"}, timestamp=self.min_ago
  23. )
  24. url = reverse(
  25. "sentry-api-0-organization-metrics-compatibility",
  26. kwargs={"organization_slug": self.project.organization.slug},
  27. )
  28. response = self.client.get(url, format="json")
  29. assert response.status_code == 200, response.content
  30. self.assertCountEqual(
  31. response.data["incompatible_projects"], [self.project.id, self.bad_project.id]
  32. )
  33. assert response.data["compatible_projects"] == []
  34. def test_null_transaction(self):
  35. # Make current project incompatible
  36. self.store_transaction_metric(1, tags={}, timestamp=self.min_ago)
  37. url = reverse(
  38. "sentry-api-0-organization-metrics-compatibility",
  39. kwargs={"organization_slug": self.project.organization.slug},
  40. )
  41. response = self.client.get(url, format="json")
  42. assert response.status_code == 200, response.content
  43. self.assertCountEqual(
  44. response.data["incompatible_projects"], [self.project.id, self.bad_project.id]
  45. )
  46. assert response.data["compatible_projects"] == []
  47. def test_no_transaction(self):
  48. # Make current project incompatible by having nothing
  49. url = reverse(
  50. "sentry-api-0-organization-metrics-compatibility",
  51. kwargs={"organization_slug": self.project.organization.slug},
  52. )
  53. response = self.client.get(url, format="json")
  54. assert response.status_code == 200, response.content
  55. self.assertCountEqual(
  56. response.data["incompatible_projects"], [self.project.id, self.bad_project.id]
  57. )
  58. assert response.data["compatible_projects"] == []
  59. def test_has_transaction(self):
  60. self.store_transaction_metric(
  61. 1, tags={"transaction": "foo_transaction"}, timestamp=self.min_ago
  62. )
  63. url = reverse(
  64. "sentry-api-0-organization-metrics-compatibility",
  65. kwargs={"organization_slug": self.project.organization.slug},
  66. )
  67. response = self.client.get(url, format="json")
  68. assert response.status_code == 200, response.content
  69. assert response.data["incompatible_projects"] == [self.bad_project.id]
  70. assert response.data["compatible_projects"] == [self.project.id]
  71. def test_multiple_projects(self):
  72. project2 = self.create_project()
  73. project3 = self.create_project()
  74. project4 = self.create_project()
  75. self.store_transaction_metric(
  76. 1, tags={"transaction": "foo_transaction"}, timestamp=self.min_ago
  77. )
  78. self.store_transaction_metric(
  79. 1, tags={"transaction": "foo_transaction"}, timestamp=self.min_ago, project=project4.id
  80. )
  81. self.store_transaction_metric(
  82. 1,
  83. tags={"transaction": "<< unparameterized >>"},
  84. timestamp=self.min_ago,
  85. project=project2.id,
  86. )
  87. self.store_transaction_metric(
  88. 1,
  89. tags={},
  90. timestamp=self.min_ago,
  91. project=project3.id,
  92. )
  93. self.store_event(
  94. data={"timestamp": iso_format(self.min_ago), "transaction": "foo_transaction"},
  95. project_id=self.project.id,
  96. )
  97. url = reverse(
  98. "sentry-api-0-organization-metrics-compatibility",
  99. kwargs={"organization_slug": self.project.organization.slug},
  100. )
  101. response = self.client.get(url, format="json")
  102. assert response.status_code == 200, response.content
  103. self.assertCountEqual(
  104. response.data["incompatible_projects"], [project2.id, project3.id, self.bad_project.id]
  105. )
  106. self.assertCountEqual(response.data["compatible_projects"], [self.project.id, project4.id])
  107. @region_silo_test
  108. class OrganizationEventsMetricsSums(MetricsEnhancedPerformanceTestCase):
  109. def setUp(self):
  110. super().setUp()
  111. self.min_ago = before_now(minutes=1)
  112. self.two_min_ago = before_now(minutes=2)
  113. self.features = {
  114. "organizations:performance-use-metrics": True,
  115. }
  116. self.login_as(user=self.user)
  117. # Don't create any txn on this, don't set its DS rules, it shouldn't show up anywhere
  118. self.create_project()
  119. def test_unparameterized_transactions(self):
  120. # Make current project incompatible
  121. self.store_transaction_metric(
  122. 1, tags={"transaction": "<< unparameterized >>"}, timestamp=self.min_ago
  123. )
  124. url = reverse(
  125. "sentry-api-0-organization-metrics-compatibility-sums",
  126. kwargs={"organization_slug": self.project.organization.slug},
  127. )
  128. response = self.client.get(url, format="json")
  129. assert response.status_code == 200, response.content
  130. assert response.data["sum"]["metrics"] == 1
  131. assert response.data["sum"]["metrics_unparam"] == 1
  132. assert response.data["sum"]["metrics_null"] == 0
  133. def test_null_transaction(self):
  134. # Make current project incompatible
  135. self.store_transaction_metric(1, tags={}, timestamp=self.min_ago)
  136. url = reverse(
  137. "sentry-api-0-organization-metrics-compatibility-sums",
  138. kwargs={"organization_slug": self.project.organization.slug},
  139. )
  140. response = self.client.get(url, format="json")
  141. assert response.status_code == 200, response.content
  142. assert response.data["sum"]["metrics"] == 1
  143. assert response.data["sum"]["metrics_unparam"] == 0
  144. assert response.data["sum"]["metrics_null"] == 1
  145. def test_no_transaction(self):
  146. # Make current project incompatible by having nothing
  147. url = reverse(
  148. "sentry-api-0-organization-metrics-compatibility-sums",
  149. kwargs={"organization_slug": self.project.organization.slug},
  150. )
  151. response = self.client.get(url, format="json")
  152. assert response.status_code == 200, response.content
  153. assert response.data["sum"]["metrics"] == 0
  154. assert response.data["sum"]["metrics_unparam"] == 0
  155. assert response.data["sum"]["metrics_null"] == 0
  156. def test_has_transaction(self):
  157. self.store_transaction_metric(
  158. 1, tags={"transaction": "foo_transaction"}, timestamp=self.min_ago
  159. )
  160. url = reverse(
  161. "sentry-api-0-organization-metrics-compatibility-sums",
  162. kwargs={"organization_slug": self.project.organization.slug},
  163. )
  164. response = self.client.get(url, format="json")
  165. assert response.status_code == 200, response.content
  166. assert response.data["sum"]["metrics"] == 1
  167. assert response.data["sum"]["metrics_unparam"] == 0
  168. assert response.data["sum"]["metrics_null"] == 0
  169. def test_multiple_projects(self):
  170. project2 = self.create_project()
  171. project3 = self.create_project()
  172. # Not setting DS, it shouldn't show up
  173. project4 = self.create_project()
  174. self.store_transaction_metric(
  175. 1, tags={"transaction": "foo_transaction"}, timestamp=self.min_ago
  176. )
  177. self.store_transaction_metric(
  178. 1, tags={"transaction": "foo_transaction"}, timestamp=self.min_ago, project=project4.id
  179. )
  180. self.store_transaction_metric(
  181. 1,
  182. tags={"transaction": "<< unparameterized >>"},
  183. timestamp=self.min_ago,
  184. project=project2.id,
  185. )
  186. self.store_transaction_metric(
  187. 1,
  188. tags={},
  189. timestamp=self.min_ago,
  190. project=project3.id,
  191. )
  192. self.store_event(
  193. data={"timestamp": iso_format(self.min_ago), "transaction": "foo_transaction"},
  194. project_id=self.project.id,
  195. )
  196. url = reverse(
  197. "sentry-api-0-organization-metrics-compatibility-sums",
  198. kwargs={"organization_slug": self.project.organization.slug},
  199. )
  200. response = self.client.get(url, format="json")
  201. assert response.status_code == 200, response.content
  202. assert response.data["sum"]["metrics"] == 4
  203. assert response.data["sum"]["metrics_unparam"] == 1
  204. assert response.data["sum"]["metrics_null"] == 1
  205. def test_counts_add_up_correctly(self):
  206. # Make current project incompatible
  207. for _ in range(2):
  208. self.store_transaction_metric(
  209. 1, tags={"transaction": "<< unparameterized >>"}, timestamp=self.min_ago
  210. )
  211. for _ in range(3):
  212. self.store_transaction_metric(1, tags={}, timestamp=self.min_ago)
  213. for _ in range(1):
  214. self.store_transaction_metric(1, tags={"transaction": "/foo"}, timestamp=self.min_ago)
  215. url = reverse(
  216. "sentry-api-0-organization-metrics-compatibility-sums",
  217. kwargs={"organization_slug": self.project.organization.slug},
  218. )
  219. response = self.client.get(url, format="json")
  220. assert response.status_code == 200, response.content
  221. assert response.data["sum"]["metrics"] == 6
  222. assert response.data["sum"]["metrics_unparam"] == 2
  223. assert response.data["sum"]["metrics_null"] == 3