test_organization_events_span_ops.py 3.3 KB

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