test_organization_metrics_meta.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. import pytest
  2. from django.urls import reverse
  3. from sentry.testutils.cases 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.json()["incompatible_projects"], [self.project.id, self.bad_project.id]
  32. )
  33. assert response.json()["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.json()["incompatible_projects"], [self.project.id, self.bad_project.id]
  45. )
  46. assert response.json()["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.json()["incompatible_projects"], [self.project.id, self.bad_project.id]
  57. )
  58. assert response.json()["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.json()["incompatible_projects"] == [self.bad_project.id]
  70. assert response.json()["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.json()["incompatible_projects"],
  105. [project2.id, project3.id, self.bad_project.id],
  106. )
  107. self.assertCountEqual(
  108. response.json()["compatible_projects"], [self.project.id, project4.id]
  109. )
  110. @region_silo_test
  111. class OrganizationEventsMetricsSums(MetricsEnhancedPerformanceTestCase):
  112. def setUp(self):
  113. super().setUp()
  114. self.min_ago = before_now(minutes=1)
  115. self.two_min_ago = before_now(minutes=2)
  116. self.features = {
  117. "organizations:performance-use-metrics": True,
  118. }
  119. self.login_as(user=self.user)
  120. # Don't create any txn on this, don't set its DS rules, it shouldn't show up anywhere
  121. self.create_project()
  122. def test_unparameterized_transactions(self):
  123. # Make current project incompatible
  124. self.store_transaction_metric(
  125. 1, tags={"transaction": "<< unparameterized >>"}, timestamp=self.min_ago
  126. )
  127. url = reverse(
  128. "sentry-api-0-organization-metrics-compatibility-sums",
  129. kwargs={"organization_slug": self.project.organization.slug},
  130. )
  131. response = self.client.get(url, format="json")
  132. assert response.status_code == 200, response.content
  133. assert response.json()["sum"]["metrics"] == 1
  134. assert response.json()["sum"]["metrics_unparam"] == 1
  135. assert response.json()["sum"]["metrics_null"] == 0
  136. def test_null_transaction(self):
  137. # Make current project incompatible
  138. self.store_transaction_metric(1, tags={}, timestamp=self.min_ago)
  139. url = reverse(
  140. "sentry-api-0-organization-metrics-compatibility-sums",
  141. kwargs={"organization_slug": self.project.organization.slug},
  142. )
  143. response = self.client.get(url, format="json")
  144. assert response.status_code == 200, response.content
  145. assert response.json()["sum"]["metrics"] == 1
  146. assert response.json()["sum"]["metrics_unparam"] == 0
  147. assert response.json()["sum"]["metrics_null"] == 1
  148. def test_no_transaction(self):
  149. # Make current project incompatible by having nothing
  150. url = reverse(
  151. "sentry-api-0-organization-metrics-compatibility-sums",
  152. kwargs={"organization_slug": self.project.organization.slug},
  153. )
  154. response = self.client.get(url, format="json")
  155. assert response.status_code == 200, response.content
  156. assert response.json()["sum"]["metrics"] == 0
  157. assert response.json()["sum"]["metrics_unparam"] == 0
  158. assert response.json()["sum"]["metrics_null"] == 0
  159. def test_has_transaction(self):
  160. self.store_transaction_metric(
  161. 1, tags={"transaction": "foo_transaction"}, timestamp=self.min_ago
  162. )
  163. url = reverse(
  164. "sentry-api-0-organization-metrics-compatibility-sums",
  165. kwargs={"organization_slug": self.project.organization.slug},
  166. )
  167. response = self.client.get(url, format="json")
  168. assert response.status_code == 200, response.content
  169. assert response.json()["sum"]["metrics"] == 1
  170. assert response.json()["sum"]["metrics_unparam"] == 0
  171. assert response.json()["sum"]["metrics_null"] == 0
  172. def test_multiple_projects(self):
  173. project2 = self.create_project()
  174. project3 = self.create_project()
  175. # Not setting DS, it shouldn't show up
  176. project4 = self.create_project()
  177. self.store_transaction_metric(
  178. 1, tags={"transaction": "foo_transaction"}, timestamp=self.min_ago
  179. )
  180. self.store_transaction_metric(
  181. 1, tags={"transaction": "foo_transaction"}, timestamp=self.min_ago, project=project4.id
  182. )
  183. self.store_transaction_metric(
  184. 1,
  185. tags={"transaction": "<< unparameterized >>"},
  186. timestamp=self.min_ago,
  187. project=project2.id,
  188. )
  189. self.store_transaction_metric(
  190. 1,
  191. tags={},
  192. timestamp=self.min_ago,
  193. project=project3.id,
  194. )
  195. self.store_event(
  196. data={"timestamp": iso_format(self.min_ago), "transaction": "foo_transaction"},
  197. project_id=self.project.id,
  198. )
  199. url = reverse(
  200. "sentry-api-0-organization-metrics-compatibility-sums",
  201. kwargs={"organization_slug": self.project.organization.slug},
  202. )
  203. response = self.client.get(url, format="json")
  204. assert response.status_code == 200, response.content
  205. assert response.json()["sum"]["metrics"] == 4
  206. assert response.json()["sum"]["metrics_unparam"] == 1
  207. assert response.json()["sum"]["metrics_null"] == 1
  208. def test_counts_add_up_correctly(self):
  209. # Make current project incompatible
  210. for _ in range(2):
  211. self.store_transaction_metric(
  212. 1, tags={"transaction": "<< unparameterized >>"}, timestamp=self.min_ago
  213. )
  214. for _ in range(3):
  215. self.store_transaction_metric(1, tags={}, timestamp=self.min_ago)
  216. for _ in range(1):
  217. self.store_transaction_metric(1, tags={"transaction": "/foo"}, timestamp=self.min_ago)
  218. url = reverse(
  219. "sentry-api-0-organization-metrics-compatibility-sums",
  220. kwargs={"organization_slug": self.project.organization.slug},
  221. )
  222. response = self.client.get(url, format="json")
  223. assert response.status_code == 200, response.content
  224. assert response.json()["sum"]["metrics"] == 6
  225. assert response.json()["sum"]["metrics_unparam"] == 2
  226. assert response.json()["sum"]["metrics_null"] == 3