test_organization_spans_aggregation.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import hashlib
  2. from unittest import mock
  3. from django.urls import reverse
  4. from sentry.testutils.cases import APITestCase, SnubaTestCase
  5. class OrganizationSpansAggregationTest(APITestCase, SnubaTestCase):
  6. url_name = "sentry-api-0-organization-spans-aggregation"
  7. FEATURES = [
  8. "organizations:starfish-view",
  9. "organizations:performance-view",
  10. ]
  11. def setUp(self):
  12. super().setUp()
  13. self.login_as(user=self.user)
  14. self.url = reverse(
  15. self.url_name,
  16. kwargs={"organization_slug": self.project.organization.slug},
  17. )
  18. @mock.patch("sentry.api.endpoints.organization_spans_aggregation.raw_snql_query")
  19. def test_simple(self, mock_query):
  20. mock_query.side_effect = [
  21. {
  22. "data": [
  23. {
  24. "transaction_id": "80fe542aea4945ffbe612646987ee449",
  25. "count": 71,
  26. "spans": [
  27. [
  28. "root_1",
  29. 1,
  30. "parent_1",
  31. "A",
  32. "A",
  33. "bind_organization_context",
  34. 422,
  35. 0,
  36. 0.0,
  37. ],
  38. ["B1", 0, "root_1", "B", "B", "connect", 158, 50, 50.0],
  39. ["C1", 0, "root_1", "C", "C", "resolve_conditions", 448, 0, 10.0],
  40. ["D1", 0, "C1", "D", "D", "resolve_orderby", 429, 0, 20.0],
  41. ],
  42. },
  43. {
  44. "transaction_id": "86b21833d1854d9b811000b91e7fccfa",
  45. "count": 71,
  46. "spans": [
  47. [
  48. "root_2",
  49. 1,
  50. "parent_2",
  51. "A",
  52. "A",
  53. "bind_organization_context",
  54. 422,
  55. 0,
  56. 0.0,
  57. ],
  58. ["B2", 0, "root_2", "B", "B", "connect", 158, 10, 30.0],
  59. ["C2", 0, "root_2", "C", "C", "resolve_conditions", 448, 0, 40.0],
  60. ["D2", 0, "C2", "D", "D", "resolve_orderby", 429, 0, 10.0],
  61. ["D2-duplicate", 0, "C2", "D", "D", "resolve_orderby", 430, 0, 20.0],
  62. ],
  63. },
  64. ]
  65. }
  66. ]
  67. with self.feature(self.FEATURES):
  68. response = self.client.get(
  69. self.url,
  70. data={"transaction": "foo"},
  71. format="json",
  72. )
  73. assert response.data
  74. data = response.data
  75. root_fingerprint = hashlib.md5(b"A").hexdigest()[:16]
  76. assert root_fingerprint in data
  77. assert data[root_fingerprint]["description"] == "bind_organization_context"
  78. assert data[root_fingerprint]["count()"] == 2
  79. fingerprint = hashlib.md5(b"A-B").hexdigest()[:16]
  80. assert data[fingerprint]["description"] == "connect"
  81. assert data[fingerprint]["avg(duration)"] == 30.0
  82. fingerprint = hashlib.md5(b"A-C-D").hexdigest()[:16]
  83. assert data[fingerprint]["description"] == "resolve_orderby"
  84. assert data[fingerprint]["avg(exclusive_time)"] == 15.0
  85. assert data[fingerprint]["count()"] == 2
  86. fingerprint = hashlib.md5(b"A-C-D2").hexdigest()[:16]
  87. assert data[fingerprint]["description"] == "resolve_orderby"
  88. assert data[fingerprint]["avg(exclusive_time)"] == 20.0
  89. assert data[fingerprint]["count()"] == 1