test_organization_event_details.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. from datetime import timedelta
  2. import pytest
  3. from django.urls import NoReverseMatch, reverse
  4. from sentry.models.group import Group
  5. from sentry.search.events import constants
  6. from sentry.testutils.cases import APITestCase, MetricsEnhancedPerformanceTestCase, SnubaTestCase
  7. from sentry.testutils.helpers.datetime import before_now
  8. from sentry.utils.samples import load_data
  9. from tests.sentry.issues.test_utils import OccurrenceTestMixin
  10. pytestmark = pytest.mark.sentry_metrics
  11. class OrganizationEventDetailsEndpointTest(APITestCase, SnubaTestCase, OccurrenceTestMixin):
  12. def setUp(self):
  13. super().setUp()
  14. min_ago = before_now(minutes=1).isoformat()
  15. two_min_ago = before_now(minutes=2).isoformat()
  16. three_min_ago = before_now(minutes=3).isoformat()
  17. self.login_as(user=self.user)
  18. self.project = self.create_project()
  19. self.project_2 = self.create_project()
  20. self.store_event(
  21. data={
  22. "event_id": "a" * 32,
  23. "message": "oh no",
  24. "timestamp": three_min_ago,
  25. "fingerprint": ["group-1"],
  26. },
  27. project_id=self.project.id,
  28. )
  29. self.store_event(
  30. data={
  31. "event_id": "b" * 32,
  32. "message": "very bad",
  33. "timestamp": two_min_ago,
  34. "fingerprint": ["group-1"],
  35. },
  36. project_id=self.project.id,
  37. )
  38. self.store_event(
  39. data={
  40. "event_id": "c" * 32,
  41. "message": "very bad",
  42. "timestamp": min_ago,
  43. "fingerprint": ["group-2"],
  44. },
  45. project_id=self.project.id,
  46. )
  47. self.groups = list(Group.objects.all().order_by("id"))
  48. def test_performance_flag(self):
  49. url = reverse(
  50. "sentry-api-0-organization-event-details",
  51. kwargs={
  52. "organization_id_or_slug": self.project.organization.slug,
  53. "project_id_or_slug": self.project.slug,
  54. "event_id": "a" * 32,
  55. },
  56. )
  57. with self.feature(
  58. {"organizations:discover-basic": False, "organizations:performance-view": True}
  59. ):
  60. response = self.client.get(url, format="json")
  61. assert response.status_code == 200, response.content
  62. assert response.data["id"] == "a" * 32
  63. assert response.data["projectSlug"] == self.project.slug
  64. def test_simple(self):
  65. url = reverse(
  66. "sentry-api-0-organization-event-details",
  67. kwargs={
  68. "organization_id_or_slug": self.project.organization.slug,
  69. "project_id_or_slug": self.project.slug,
  70. "event_id": "a" * 32,
  71. },
  72. )
  73. with self.feature("organizations:discover-basic"):
  74. response = self.client.get(url, format="json")
  75. assert response.status_code == 200, response.content
  76. assert response.data["id"] == "a" * 32
  77. assert response.data["projectSlug"] == self.project.slug
  78. def test_simple_with_id(self):
  79. url = reverse(
  80. "sentry-api-0-organization-event-details",
  81. kwargs={
  82. "organization_id_or_slug": self.project.organization.slug,
  83. "project_id_or_slug": self.project.id,
  84. "event_id": "a" * 32,
  85. },
  86. )
  87. with self.feature("organizations:discover-basic"):
  88. response = self.client.get(url, format="json")
  89. assert response.status_code == 200, response.content
  90. assert response.data["id"] == "a" * 32
  91. assert response.data["projectSlug"] == self.project.slug
  92. def test_simple_transaction(self):
  93. min_ago = before_now(minutes=1).isoformat()
  94. event = self.store_event(
  95. data={
  96. "event_id": "d" * 32,
  97. "type": "transaction",
  98. "transaction": "api.issue.delete",
  99. "spans": [],
  100. "contexts": {"trace": {"op": "foobar", "trace_id": "a" * 32, "span_id": "a" * 16}},
  101. "start_timestamp": before_now(minutes=1, seconds=5).isoformat(),
  102. "timestamp": min_ago,
  103. },
  104. project_id=self.project.id,
  105. )
  106. url = reverse(
  107. "sentry-api-0-organization-event-details",
  108. kwargs={
  109. "organization_id_or_slug": self.project.organization.slug,
  110. "project_id_or_slug": self.project.slug,
  111. "event_id": event.event_id,
  112. },
  113. )
  114. with self.feature("organizations:discover-basic"):
  115. response = self.client.get(url, format="json")
  116. assert response.status_code == 200
  117. assert response.data["id"] == "d" * 32
  118. assert response.data["type"] == "transaction"
  119. def test_no_access_missing_feature(self):
  120. with self.feature({"organizations:discover-basic": False}):
  121. url = reverse(
  122. "sentry-api-0-organization-event-details",
  123. kwargs={
  124. "organization_id_or_slug": self.project.organization.slug,
  125. "project_id_or_slug": self.project.slug,
  126. "event_id": "a" * 32,
  127. },
  128. )
  129. response = self.client.get(url, format="json")
  130. assert response.status_code == 404, response.content
  131. def test_access_non_member_project(self):
  132. # Add a new user to a project and then access events on project they are not part of.
  133. member_user = self.create_user()
  134. team = self.create_team(members=[member_user])
  135. self.create_project(organization=self.organization, teams=[team])
  136. # Enable open membership
  137. self.organization.flags.allow_joinleave = True
  138. self.organization.save()
  139. self.login_as(member_user)
  140. url = reverse(
  141. "sentry-api-0-organization-event-details",
  142. kwargs={
  143. "organization_id_or_slug": self.organization.slug,
  144. "project_id_or_slug": self.project.slug,
  145. "event_id": "a" * 32,
  146. },
  147. )
  148. with self.feature("organizations:discover-basic"):
  149. response = self.client.get(url, format="json")
  150. assert response.status_code == 200, response.content
  151. # When open membership is off, access should be denied to non owner users
  152. self.organization.flags.allow_joinleave = False
  153. self.organization.save()
  154. with self.feature("organizations:discover-basic"):
  155. response = self.client.get(url, format="json")
  156. assert response.status_code == 404, response.content
  157. def test_no_event(self):
  158. url = reverse(
  159. "sentry-api-0-organization-event-details",
  160. kwargs={
  161. "organization_id_or_slug": self.project.organization.slug,
  162. "project_id_or_slug": self.project.slug,
  163. "event_id": "d" * 32,
  164. },
  165. )
  166. with self.feature("organizations:discover-basic"):
  167. response = self.client.get(url, format="json")
  168. assert response.status_code == 404, response.content
  169. def test_invalid_event_id(self):
  170. with pytest.raises(NoReverseMatch):
  171. reverse(
  172. "sentry-api-0-organization-event-details",
  173. kwargs={
  174. "organization_id_or_slug": self.project.organization.slug,
  175. "project_id_or_slug": self.project.slug,
  176. "event_id": "not-an-event",
  177. },
  178. )
  179. def test_long_trace_description(self):
  180. data = load_data("transaction")
  181. data["event_id"] = "d" * 32
  182. data["timestamp"] = before_now(minutes=1).isoformat()
  183. data["start_timestamp"] = (before_now(minutes=1) - timedelta(seconds=5)).isoformat()
  184. data["contexts"]["trace"]["description"] = "b" * 512
  185. self.store_event(data=data, project_id=self.project.id)
  186. url = reverse(
  187. "sentry-api-0-organization-event-details",
  188. kwargs={
  189. "organization_id_or_slug": self.project.organization.slug,
  190. "project_id_or_slug": self.project.slug,
  191. "event_id": "d" * 32,
  192. },
  193. )
  194. with self.feature("organizations:discover-basic"):
  195. response = self.client.get(url, format="json")
  196. assert response.status_code == 200, response.content
  197. trace = response.data["contexts"]["trace"]
  198. original_trace = data["contexts"]["trace"]
  199. assert trace["trace_id"] == original_trace["trace_id"]
  200. assert trace["span_id"] == original_trace["span_id"]
  201. assert trace["parent_span_id"] == original_trace["parent_span_id"]
  202. assert trace["description"][:-3] in original_trace["description"]
  203. def test_blank_fields(self):
  204. url = reverse(
  205. "sentry-api-0-organization-event-details",
  206. kwargs={
  207. "organization_id_or_slug": self.project.organization.slug,
  208. "project_id_or_slug": self.project.slug,
  209. "event_id": "a" * 32,
  210. },
  211. )
  212. with self.feature("organizations:discover-basic"):
  213. response = self.client.get(
  214. url,
  215. data={"field": ["", " "], "statsPeriod": "24h"},
  216. format="json",
  217. )
  218. assert response.status_code == 200, response.content
  219. assert response.data["id"] == "a" * 32
  220. assert response.data["projectSlug"] == self.project.slug
  221. def test_out_of_retention(self):
  222. self.store_event(
  223. data={
  224. "event_id": "d" * 32,
  225. "message": "oh no",
  226. "timestamp": before_now(days=2).isoformat(),
  227. "fingerprint": ["group-1"],
  228. },
  229. project_id=self.project.id,
  230. )
  231. url = reverse(
  232. "sentry-api-0-organization-event-details",
  233. kwargs={
  234. "organization_id_or_slug": self.project.organization.slug,
  235. "project_id_or_slug": self.project.slug,
  236. "event_id": "d" * 32,
  237. },
  238. )
  239. with self.options({"system.event-retention-days": 1}):
  240. response = self.client.get(
  241. url,
  242. format="json",
  243. )
  244. assert response.status_code == 404, response.content
  245. def test_generic_event(self):
  246. occurrence, _ = self.process_occurrence(
  247. project_id=self.project.id,
  248. event_data={
  249. "level": "info",
  250. },
  251. )
  252. url = reverse(
  253. "sentry-api-0-organization-event-details",
  254. kwargs={
  255. "organization_id_or_slug": self.project.organization.slug,
  256. "project_id_or_slug": self.project.slug,
  257. "event_id": occurrence.event_id,
  258. },
  259. )
  260. with self.feature("organizations:discover-basic"):
  261. response = self.client.get(url, format="json")
  262. assert response.status_code == 200, response.content
  263. assert response.data["id"] == occurrence.event_id
  264. assert response.data["projectSlug"] == self.project.slug
  265. assert response.data["occurrence"] is not None
  266. assert response.data["occurrence"]["id"] == occurrence.id
  267. class EventComparisonTest(MetricsEnhancedPerformanceTestCase):
  268. endpoint = "sentry-api-0-organization-event-details"
  269. def setUp(self):
  270. self.init_snuba()
  271. self.ten_mins_ago = before_now(minutes=10)
  272. self.transaction_data = load_data("transaction", timestamp=self.ten_mins_ago)
  273. self.RESULT_COLUMN = "span.averageResults"
  274. event = self.store_event(self.transaction_data, self.project)
  275. self.url = reverse(
  276. self.endpoint,
  277. kwargs={
  278. "organization_id_or_slug": self.project.organization.slug,
  279. "project_id_or_slug": self.project.slug,
  280. "event_id": event.event_id,
  281. },
  282. )
  283. self.login_as(user=self.user)
  284. self.store_span_metric(
  285. 1,
  286. internal_metric=constants.SELF_TIME_LIGHT,
  287. timestamp=self.ten_mins_ago,
  288. tags={"span.group": "26b881987e4bad99"},
  289. )
  290. def test_get_without_feature(self):
  291. response = self.client.get(self.url, {"averageColumn": "span.self_time"})
  292. assert response.status_code == 200, response.content
  293. entries = response.data["entries"] # type: ignore[attr-defined]
  294. for entry in entries:
  295. if entry["type"] == "spans":
  296. for span in entry["data"]:
  297. assert span.get(self.RESULT_COLUMN) is None
  298. def test_get(self):
  299. with self.feature("organizations:insights-initial-modules"):
  300. response = self.client.get(self.url, {"averageColumn": "span.self_time"})
  301. assert response.status_code == 200, response.content
  302. entries = response.data["entries"] # type: ignore[attr-defined]
  303. for entry in entries:
  304. if entry["type"] == "spans":
  305. for span in entry["data"]:
  306. if span["op"] == "db":
  307. assert span[self.RESULT_COLUMN] == {"avg(span.self_time)": 1.0}
  308. if span["op"] == "django.middleware":
  309. assert self.RESULT_COLUMN not in span
  310. def test_get_multiple_columns(self):
  311. self.store_span_metric(
  312. 2,
  313. internal_metric=constants.SPAN_METRICS_MAP["span.duration"],
  314. timestamp=self.ten_mins_ago,
  315. tags={"span.group": "26b881987e4bad99"},
  316. )
  317. with self.feature("organizations:insights-initial-modules"):
  318. response = self.client.get(
  319. self.url, {"averageColumn": ["span.self_time", "span.duration"]}
  320. )
  321. assert response.status_code == 200, response.content
  322. entries = response.data["entries"] # type: ignore[attr-defined]
  323. for entry in entries:
  324. if entry["type"] == "spans":
  325. for span in entry["data"]:
  326. if span["op"] == "db":
  327. assert span[self.RESULT_COLUMN] == {
  328. "avg(span.self_time)": 1.0,
  329. "avg(span.duration)": 2.0,
  330. }
  331. if span["op"] == "django.middleware":
  332. assert self.RESULT_COLUMN not in span
  333. def test_nan_column(self):
  334. # If there's nothing stored for a metric, span.duration in this case the query returns nan
  335. with self.feature("organizations:insights-initial-modules"):
  336. response = self.client.get(
  337. self.url, {"averageColumn": ["span.self_time", "span.duration"]}
  338. )
  339. assert response.status_code == 200, response.content
  340. entries = response.data["entries"] # type: ignore[attr-defined]
  341. for entry in entries:
  342. if entry["type"] == "spans":
  343. for span in entry["data"]:
  344. if span["op"] == "db":
  345. assert span[self.RESULT_COLUMN] == {"avg(span.self_time)": 1.0}
  346. if span["op"] == "django.middleware":
  347. assert self.RESULT_COLUMN not in span
  348. def test_invalid_column(self):
  349. # If any columns are invalid, ignore average field in results completely
  350. response = self.client.get(
  351. self.url, {"averageColumn": ["span.self_time", "span.everything"]}
  352. )
  353. assert response.status_code == 200, response.content
  354. entries = response.data["entries"] # type: ignore[attr-defined]
  355. for entry in entries:
  356. if entry["type"] == "spans":
  357. for span in entry["data"]:
  358. assert self.RESULT_COLUMN not in span