test_organization_events_span_ops.py 3.2 KB

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