test_organization_events_facets_performance_histogram.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. from datetime import timedelta
  2. from django.urls import reverse
  3. from sentry.testutils.helpers.datetime import iso_format
  4. from sentry.testutils.silo import region_silo_test
  5. from sentry.utils.cursors import Cursor
  6. from sentry.utils.samples import load_data
  7. from tests.snuba.api.endpoints.test_organization_events_facets_performance import (
  8. BaseOrganizationEventsFacetsPerformanceEndpointTest,
  9. )
  10. @region_silo_test
  11. class OrganizationEventsFacetsPerformanceHistogramEndpointTest(
  12. BaseOrganizationEventsFacetsPerformanceEndpointTest
  13. ):
  14. feature_list = (
  15. "organizations:discover-basic",
  16. "organizations:global-views",
  17. "organizations:performance-view",
  18. )
  19. def setUp(self):
  20. super().setUp()
  21. self._transaction_count = 0
  22. self.url = reverse(
  23. "sentry-api-0-organization-events-facets-performance-histogram",
  24. kwargs={"organization_slug": self.project.organization.slug},
  25. )
  26. # Function to set up some transactions for most tests
  27. def setup_transactions(self):
  28. for i in range(5):
  29. self.store_transaction(
  30. tags=[["color", "blue"], ["many", "yes"]], duration=4000, lcp=4000
  31. )
  32. # LCP-less transaction
  33. self.store_transaction(tags=[["color", "orange"], ["many", "maybe"]], lcp=None)
  34. for i in range(14):
  35. self.store_transaction(
  36. tags=[["color", "red"], ["many", "yes"]], duration=1000, lcp=1000
  37. )
  38. for i in range(1):
  39. self.store_transaction(
  40. tags=[["color", "green"], ["many", "no"]], duration=5000, lcp=5000
  41. )
  42. def store_transaction(
  43. self,
  44. name="exampleTransaction",
  45. duration=100,
  46. tags=None,
  47. project_id=None,
  48. lcp=None,
  49. user_id=None,
  50. ):
  51. if tags is None:
  52. tags = []
  53. if project_id is None:
  54. project_id = self.project.id
  55. event = load_data("transaction").copy()
  56. event.data["tags"].extend(tags)
  57. event.update(
  58. {
  59. "transaction": name,
  60. "event_id": f"{self._transaction_count:02x}".rjust(32, "0"),
  61. "start_timestamp": iso_format(self.two_mins_ago - timedelta(seconds=duration)),
  62. "timestamp": iso_format(self.two_mins_ago),
  63. }
  64. )
  65. if user_id:
  66. event["user"] = {
  67. "email": "foo@example.com",
  68. "id": user_id,
  69. "ip_address": "127.0.0.1",
  70. "username": "foo",
  71. }
  72. if lcp:
  73. event["measurements"]["lcp"]["value"] = lcp
  74. else:
  75. del event["measurements"]["lcp"]
  76. self._transaction_count += 1
  77. self.store_event(data=event, project_id=project_id)
  78. def test_multiple_projects_not_allowed(self):
  79. response = self.do_request(
  80. {
  81. "aggregateColumn": "transaction.duration",
  82. "project": [self.project.id, self.project2.id],
  83. }
  84. )
  85. assert response.status_code == 400, response.content
  86. assert response.data == {
  87. "detail": "You cannot view facet performance for multiple projects."
  88. }
  89. def test_missing_tags_column(self):
  90. response = self.do_request({})
  91. assert response.status_code == 400, response.content
  92. assert response.data == {"detail": "'aggregateColumn' must be provided."}
  93. def test_no_access(self):
  94. request = {
  95. "aggregateColumn": "transaction.duration",
  96. "sort": "-frequency",
  97. "per_page": 5,
  98. "statsPeriod": "14d",
  99. "query": "(color:red or color:blue)",
  100. }
  101. error_response = self.do_request(
  102. request,
  103. {
  104. "organizations:performance-view": False,
  105. },
  106. )
  107. assert error_response.status_code == 404
  108. def test_num_buckets_error(self):
  109. self.setup_transactions()
  110. request = {
  111. "aggregateColumn": "transaction.duration",
  112. "sort": "-frequency",
  113. "statsPeriod": "14d",
  114. "query": "(color:red or color:blue)",
  115. "per_page": 5,
  116. }
  117. # With feature access, no tag key
  118. error_response = self.do_request(request)
  119. assert error_response.status_code == 400, error_response.content
  120. assert error_response.data == {
  121. "detail": "'numBucketsPerKey' must be provided for the performance histogram."
  122. }
  123. def test_tag_key_histograms(self):
  124. self.setup_transactions()
  125. request = {
  126. "aggregateColumn": "transaction.duration",
  127. "sort": "-frequency",
  128. "statsPeriod": "14d",
  129. "per_page": 10,
  130. "numBucketsPerKey": 10,
  131. "query": "(color:red or color:blue)",
  132. }
  133. # With feature access, no tag key
  134. error_response = self.do_request(request)
  135. assert error_response.status_code == 400, error_response.content
  136. assert error_response.data == {"detail": "'tagKey' must be provided when using histograms."}
  137. # With feature access and tag key
  138. request["tagKey"] = "color"
  139. data_response = self.do_request(request)
  140. histogram_data = data_response.data["histogram"]["data"]
  141. assert len(histogram_data) == 2
  142. assert histogram_data[0]["count"] == 14
  143. assert histogram_data[0]["histogram_transaction_duration_500000_1000000_1"] == 1000000.0
  144. assert histogram_data[0]["tags_value"] == "red"
  145. assert histogram_data[0]["tags_key"] == "color"
  146. assert histogram_data[1]["count"] == 5
  147. assert histogram_data[1]["histogram_transaction_duration_500000_1000000_1"] == 4000000.0
  148. assert histogram_data[1]["tags_value"] == "blue"
  149. assert histogram_data[1]["tags_key"] == "color"
  150. tag_data = data_response.data["tags"]["data"]
  151. assert len(tag_data) == 2
  152. assert tag_data[0]["tags_value"] == "red"
  153. assert tag_data[1]["tags_value"] == "blue"
  154. def test_no_top_tags(self):
  155. self.setup_transactions()
  156. request = {
  157. "aggregateColumn": "transaction.duration",
  158. "sort": "-frequency",
  159. "statsPeriod": "14d",
  160. "per_page": 10,
  161. "numBucketsPerKey": 10,
  162. "tagKey": "color",
  163. "query": "(color:teal or color:oak)",
  164. }
  165. data_response = self.do_request(request)
  166. histogram_data = data_response.data["histogram"]["data"]
  167. assert histogram_data == []
  168. tag_data = data_response.data["tags"]["data"]
  169. assert tag_data == []
  170. def test_tag_key_histogram_buckets(self):
  171. self.setup_transactions()
  172. request = {
  173. "aggregateColumn": "transaction.duration",
  174. "sort": "-frequency",
  175. "statsPeriod": "14d",
  176. "per_page": 1,
  177. "numBucketsPerKey": 2,
  178. "tagKey": "color",
  179. "query": "(color:red or color:blue or color:green)",
  180. }
  181. data_response = self.do_request(request)
  182. histogram_data = data_response.data["histogram"]["data"]
  183. assert len(histogram_data) == 1
  184. assert histogram_data[0]["count"] == 14
  185. assert histogram_data[0]["histogram_transaction_duration_2500000_0_1"] == 0.0
  186. assert histogram_data[0]["tags_value"] == "red"
  187. assert histogram_data[0]["tags_key"] == "color"
  188. request["per_page"] = 3
  189. data_response = self.do_request(request)
  190. histogram_data = data_response.data["histogram"]["data"]
  191. assert len(histogram_data) == 3
  192. assert histogram_data[0]["count"] == 14
  193. assert histogram_data[0]["histogram_transaction_duration_2500000_0_1"] == 0.0
  194. assert histogram_data[0]["tags_value"] == "red"
  195. assert histogram_data[0]["tags_key"] == "color"
  196. assert histogram_data[1]["count"] == 5
  197. assert histogram_data[1]["histogram_transaction_duration_2500000_0_1"] == 2500000.0
  198. assert histogram_data[1]["tags_value"] == "blue"
  199. assert histogram_data[1]["tags_key"] == "color"
  200. assert histogram_data[2]["count"] == 1
  201. assert histogram_data[2]["histogram_transaction_duration_2500000_0_1"] == 5000000.0
  202. assert histogram_data[2]["tags_value"] == "green"
  203. assert histogram_data[2]["tags_key"] == "color"
  204. def test_histograms_omit_empty_measurements(self):
  205. self.setup_transactions()
  206. request = {
  207. "aggregateColumn": "transaction.duration",
  208. "sort": "-frequency",
  209. "statsPeriod": "14d",
  210. "per_page": 3,
  211. "numBucketsPerKey": 2,
  212. "tagKey": "color",
  213. "query": "(color:red or color:blue or color:green or color:orange)",
  214. }
  215. data_response = self.do_request(request)
  216. assert data_response.data["tags"]["data"][2]["tags_value"] == "green"
  217. request["aggregateColumn"] = "measurements.lcp"
  218. data_response = self.do_request(request)
  219. tags_data = data_response.data["tags"]["data"]
  220. assert len(tags_data) == 3
  221. assert tags_data[2]["tags_value"] == "green"
  222. histogram_data = data_response.data["histogram"]["data"]
  223. assert len(histogram_data) == 3
  224. assert histogram_data[0]["count"] == 14
  225. assert histogram_data[0]["histogram_measurements_lcp_2500_0_1"] == 0.0
  226. assert histogram_data[0]["tags_value"] == "red"
  227. assert histogram_data[0]["tags_key"] == "color"
  228. assert histogram_data[1]["count"] == 5
  229. assert histogram_data[1]["histogram_measurements_lcp_2500_0_1"] == 2500.0
  230. assert histogram_data[1]["tags_value"] == "blue"
  231. assert histogram_data[1]["tags_key"] == "color"
  232. assert histogram_data[2]["count"] == 1
  233. assert histogram_data[2]["histogram_measurements_lcp_2500_0_1"] == 5000.0
  234. assert histogram_data[2]["tags_value"] == "green"
  235. assert histogram_data[2]["tags_key"] == "color"
  236. def test_histogram_user_field(self):
  237. self.setup_transactions()
  238. self.store_transaction(
  239. tags=[["color", "blue"], ["many", "yes"]], duration=4000, user_id=555
  240. )
  241. request = {
  242. "aggregateColumn": "transaction.duration",
  243. "per_page": 1,
  244. "numBucketsPerKey": 2,
  245. "tagKey": "user",
  246. "query": "(user.id:555)",
  247. }
  248. data_response = self.do_request(request)
  249. histogram_data = data_response.data["histogram"]["data"]
  250. assert histogram_data[0]["count"] == 1
  251. assert histogram_data[0]["tags_value"] == "id:555"
  252. assert histogram_data[0]["tags_key"] == "user"
  253. tag_data = data_response.data["tags"]["data"]
  254. assert tag_data[0]["count"] == 1
  255. assert tag_data[0]["tags_value"] == "id:555"
  256. def test_histogram_pagination(self):
  257. self.setup_transactions()
  258. request = {
  259. "aggregateColumn": "transaction.duration",
  260. "per_page": 3,
  261. "numBucketsPerKey": 2,
  262. "tagKey": "color",
  263. }
  264. data_response = self.do_request(request)
  265. tag_data = data_response.data["tags"]["data"]
  266. assert len(tag_data) == 3
  267. request["cursor"] = Cursor(0, 3)
  268. data_response = self.do_request(request)
  269. tag_data = data_response.data["tags"]["data"]
  270. assert len(tag_data) == 1
  271. def test_histogram_sorting(self):
  272. self.setup_transactions()
  273. request = {
  274. "aggregateColumn": "transaction.duration",
  275. "per_page": 1,
  276. "sort": "-frequency",
  277. "numBucketsPerKey": 2,
  278. "tagKey": "color",
  279. }
  280. data_response = self.do_request(request)
  281. tag_data = data_response.data["tags"]["data"]
  282. assert len(tag_data) == 1
  283. assert tag_data[0]["tags_value"] == "red"
  284. assert tag_data[0]["count"] == 14
  285. request["sort"] = "-aggregate"
  286. data_response = self.do_request(request)
  287. tag_data = data_response.data["tags"]["data"]
  288. assert len(tag_data) == 1
  289. assert tag_data[0]["tags_value"] == "green"
  290. assert tag_data[0]["count"] == 1
  291. def test_histogram_high_buckets(self):
  292. for i in range(10):
  293. self.store_transaction(tags=[["fruit", "apple"]], duration=i * 100 + 50)
  294. self.store_transaction(tags=[["fruit", "orange"]], duration=i * 100 + 1000 + 50)
  295. request = {
  296. "aggregateColumn": "transaction.duration",
  297. "per_page": 2,
  298. "sort": "-frequency",
  299. "numBucketsPerKey": 20,
  300. "tagKey": "fruit",
  301. }
  302. data_response = self.do_request(request)
  303. histogram_data = data_response.data["histogram"]["data"]
  304. assert len(histogram_data) == 20
  305. for i, d in enumerate(histogram_data):
  306. assert d["count"] == 1
  307. assert d["histogram_transaction_duration_100000_0_1"] == i * 100000.0
  308. if i < 10:
  309. assert d["tags_value"] == "apple"
  310. else:
  311. assert d["tags_value"] == "orange"
  312. tag_data = data_response.data["tags"]["data"]
  313. assert len(tag_data) == 2
  314. assert tag_data[0]["tags_value"] == "apple"
  315. assert tag_data[0]["count"] == 10
  316. assert tag_data[0]["aggregate"] == 500000.0
  317. assert tag_data[1]["tags_value"] == "orange"
  318. assert tag_data[1]["count"] == 10
  319. assert tag_data[1]["aggregate"] == 1500000.0