test_organization_events_span_ops.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. from datetime import timedelta
  2. import pytest
  3. from django.urls import reverse
  4. from sentry.testutils import APITestCase, SnubaTestCase
  5. from sentry.testutils.helpers.datetime import before_now, iso_format
  6. from sentry.testutils.silo import region_silo_test
  7. from sentry.utils.samples import load_data
  8. @region_silo_test
  9. class OrganizationEventsSpanOpsEndpointBase(APITestCase, SnubaTestCase):
  10. FEATURES = ["organizations:performance-suspect-spans-view"]
  11. def setUp(self):
  12. super().setUp()
  13. self.login_as(user=self.user)
  14. self.url = reverse(
  15. "sentry-api-0-organization-events-span-ops",
  16. kwargs={"organization_slug": self.organization.slug},
  17. )
  18. self.min_ago = before_now(minutes=1).replace(microsecond=0)
  19. def create_event(self, **kwargs):
  20. if "span_id" not in kwargs:
  21. kwargs["span_id"] = "a" * 16
  22. if "start_timestamp" not in kwargs:
  23. kwargs["start_timestamp"] = self.min_ago
  24. if "timestamp" not in kwargs:
  25. kwargs["timestamp"] = self.min_ago + timedelta(seconds=8)
  26. if "trace_context" not in kwargs:
  27. # should appear for all of the pXX metrics
  28. kwargs["trace_context"] = {
  29. "op": "http.server",
  30. "hash": "ab" * 8,
  31. "exclusive_time": 4.0,
  32. }
  33. if "spans" not in kwargs:
  34. kwargs["spans"] = [
  35. # should appear for the sum metric
  36. {
  37. "same_process_as_parent": True,
  38. "parent_span_id": "a" * 16,
  39. "span_id": x * 16,
  40. "start_timestamp": iso_format(self.min_ago + timedelta(seconds=1)),
  41. "timestamp": iso_format(self.min_ago + timedelta(seconds=4)),
  42. "op": "django.middleware",
  43. "description": "middleware span",
  44. "hash": "cd" * 8,
  45. "exclusive_time": 3.0,
  46. }
  47. for x in ["b", "c"]
  48. ] + [
  49. # should appear for the count metric
  50. {
  51. "same_process_as_parent": True,
  52. "parent_span_id": "a" * 16,
  53. "span_id": x * 16,
  54. "start_timestamp": iso_format(self.min_ago + timedelta(seconds=4)),
  55. "timestamp": iso_format(self.min_ago + timedelta(seconds=5)),
  56. "op": "django.view",
  57. "description": "view span",
  58. "hash": "ef" * 8,
  59. "exclusive_time": 1.0,
  60. }
  61. for x in ["d", "e", "f"]
  62. ]
  63. data = load_data("transaction", **kwargs)
  64. data["transaction"] = "root transaction"
  65. return self.store_event(data, project_id=self.project.id)
  66. @pytest.mark.skip("setting snuba config is too slow")
  67. def test_basic(self):
  68. self.create_event()
  69. with self.feature(self.FEATURES):
  70. response = self.client.get(
  71. self.url,
  72. data={
  73. "project": self.project.id,
  74. },
  75. format="json",
  76. )
  77. assert response.status_code == 200, response.content
  78. assert response.data == [
  79. {"op": "django.view", "count": 3},
  80. {"op": "django.middleware", "count": 2},
  81. {"op": "http.server", "count": 1},
  82. ]