test_performance_trace_detail.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. from datetime import timedelta
  2. from unittest.mock import patch
  3. from uuid import uuid4
  4. import pytz
  5. from sentry.testutils import AcceptanceTestCase, SnubaTestCase
  6. from sentry.testutils.helpers.datetime import before_now, iso_format
  7. from sentry.utils.samples import load_data
  8. FEATURE_NAMES = ["organizations:performance-view"]
  9. def make_span_id() -> str:
  10. return uuid4().hex[:16]
  11. class PerformanceTraceDetailTest(AcceptanceTestCase, SnubaTestCase):
  12. def create_error(self, platform, trace_id, span_id, project_id, timestamp):
  13. data = load_data(platform, timestamp=timestamp)
  14. if "contexts" not in data:
  15. data["contexts"] = {}
  16. data["contexts"]["trace"] = {"type": "trace", "trace_id": trace_id, "span_id": span_id}
  17. return self.store_event(data, project_id=project_id)
  18. def create_transaction(
  19. self,
  20. transaction,
  21. trace_id,
  22. span_id,
  23. parent_span_id,
  24. spans,
  25. project_id,
  26. start_timestamp,
  27. duration,
  28. transaction_id=None,
  29. ):
  30. timestamp = start_timestamp + timedelta(milliseconds=duration)
  31. data = load_data(
  32. "transaction",
  33. trace=trace_id,
  34. span_id=span_id,
  35. spans=spans,
  36. start_timestamp=start_timestamp,
  37. timestamp=timestamp,
  38. )
  39. if transaction_id is not None:
  40. data["event_id"] = transaction_id
  41. data["transaction"] = transaction
  42. data["contexts"]["trace"]["parent_span_id"] = parent_span_id
  43. return self.store_event(data, project_id=project_id)
  44. def setUp(self):
  45. super().setUp()
  46. self.org = self.create_organization(owner=self.user, name="Rowdy Tiger")
  47. self.team = self.create_team(
  48. organization=self.org, name="Mariachi Band", members=[self.user]
  49. )
  50. self.frontend_project = self.create_project(
  51. organization=self.org, teams=[self.team], name="Frontend", platform="javascript"
  52. )
  53. self.backend_project = self.create_project(
  54. organization=self.org, teams=[self.team], name="Backend", platform="python"
  55. )
  56. self.service_project = self.create_project(
  57. organization=self.org, teams=[self.team], name="Service", platform="go"
  58. )
  59. self.task_project = self.create_project(
  60. organization=self.org, teams=[self.team], name="Task", platform="rust"
  61. )
  62. self.login_as(self.user)
  63. self.day_ago = before_now(days=1).replace(hour=10, minute=0, second=0, microsecond=0)
  64. self.trace_id = "a" * 32
  65. self.frontend_transaction_id = "b" * 16
  66. self.frontend_span_ids = [make_span_id() for _ in range(3)]
  67. self.backend_transaction_ids = [make_span_id() for _ in range(3)]
  68. # a chain of transactions that are orphans
  69. self.task_transactions = []
  70. last_transaction_id = make_span_id()
  71. for i in range(5):
  72. transaction_id = make_span_id()
  73. timestamp = self.day_ago + timedelta(seconds=i, microseconds=30000)
  74. self.create_error(
  75. platform="python",
  76. trace_id=self.trace_id,
  77. span_id=transaction_id,
  78. project_id=self.task_project.id,
  79. timestamp=timestamp,
  80. )
  81. self.task_transactions.append(
  82. self.create_transaction(
  83. transaction=f"task_transaction_{i}",
  84. trace_id=self.trace_id,
  85. span_id=transaction_id,
  86. parent_span_id=last_transaction_id,
  87. spans=None,
  88. project_id=self.task_project.id,
  89. start_timestamp=timestamp,
  90. duration=700 + 100 * (i + 1),
  91. )
  92. )
  93. last_transaction_id = transaction_id
  94. # two transactions attached to the same span
  95. self.service_transaction_s = [
  96. self.create_transaction(
  97. transaction=f"service_transaction_{i}",
  98. trace_id=self.trace_id,
  99. span_id=make_span_id(),
  100. parent_span_id=self.backend_transaction_ids[1],
  101. spans=None,
  102. project_id=self.service_project.id,
  103. start_timestamp=self.day_ago
  104. + timedelta(seconds=1, microseconds=100000 + i * 50000),
  105. duration=750 * (i + 1),
  106. )
  107. for i in range(2)
  108. ]
  109. # single transaction attached to the root span
  110. self.service_transaction_2 = self.create_transaction(
  111. transaction="service_transaction_2",
  112. trace_id=self.trace_id,
  113. span_id=make_span_id(),
  114. parent_span_id=self.backend_transaction_ids[2],
  115. spans=None,
  116. project_id=self.service_project.id,
  117. start_timestamp=self.day_ago + timedelta(microseconds=400000),
  118. duration=1000,
  119. )
  120. # 3 transactions attached to 3 different spans on the same transaction
  121. self.backend_transactions = [
  122. self.create_transaction(
  123. transaction=f"backend_transaction_{i}",
  124. trace_id=self.trace_id,
  125. span_id=backend_transaction_id,
  126. parent_span_id=frontend_span_id,
  127. spans=None,
  128. project_id=self.backend_project.id,
  129. start_timestamp=self.day_ago + timedelta(microseconds=100000 + i * 50000),
  130. duration=2500 + i * 500,
  131. )
  132. for i, (frontend_span_id, backend_transaction_id) in enumerate(
  133. zip(self.frontend_span_ids, self.backend_transaction_ids)
  134. )
  135. ]
  136. self.frontend_error = self.create_error(
  137. platform="javascript",
  138. trace_id=self.trace_id,
  139. span_id=self.frontend_span_ids[1],
  140. project_id=self.frontend_project.id,
  141. timestamp=self.day_ago,
  142. )
  143. self.frontend_transaction = self.create_transaction(
  144. transaction="frontend_transaction",
  145. trace_id=self.trace_id,
  146. span_id=self.frontend_transaction_id,
  147. parent_span_id=None,
  148. spans=[
  149. {
  150. "same_process_as_parent": True,
  151. "op": "http",
  152. "description": f"GET gen1-{i}",
  153. "span_id": frontend_span_id,
  154. "trace_id": self.trace_id,
  155. }
  156. for i, frontend_span_id in enumerate(self.frontend_span_ids)
  157. ],
  158. project_id=self.frontend_project.id,
  159. start_timestamp=self.day_ago,
  160. duration=4000,
  161. transaction_id="c" * 32,
  162. )
  163. @property
  164. def path(self):
  165. return "/organizations/{}/performance/trace/{}/?pageStart={}&pageEnd={}".format(
  166. self.org.slug,
  167. self.trace_id,
  168. iso_format(before_now(days=1).replace(hour=9, minute=0, second=0, microsecond=0)),
  169. iso_format(before_now(days=1).replace(hour=11, minute=0, second=0, microsecond=0)),
  170. )
  171. @patch("django.utils.timezone.now")
  172. def test_with_data(self, mock_now):
  173. mock_now.return_value = before_now().replace(tzinfo=pytz.utc)
  174. with self.feature(FEATURE_NAMES):
  175. self.browser.get(self.path)
  176. self.browser.wait_until_not('[data-test-id="loading-indicator"]')
  177. self.browser.elements('[data-test-id="transaction-row-title"]')[1].click()
  178. self.browser.snapshot("performance trace view - with data")