test_organization_events_span_ops.py 3.2 KB

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