test_organization_events_span_indexed.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. from datetime import datetime, timedelta
  2. from uuid import uuid4
  3. from tests.snuba.api.endpoints.test_organization_events import OrganizationEventsEndpointTestBase
  4. class OrganizationEventsSpanIndexedEndpointTest(OrganizationEventsEndpointTestBase):
  5. """Test the indexed spans dataset.
  6. To run this locally you may need to set the ENABLE_SPANS_CONSUMER flag to True in Snuba.
  7. A way to do this is
  8. 1. run: `sentry devservices down snuba`
  9. 2. clone snuba locally
  10. 3. run: `export ENABLE_SPANS_CONSUMER=True`
  11. 4. run snuba
  12. At this point tests should work locally
  13. Once span ingestion is on by default this will no longer need to be done
  14. """
  15. # Some base data for create_span
  16. base_span = {
  17. "is_segment": False,
  18. "retention_days": 90,
  19. "tags": {},
  20. "sentry_tags": {},
  21. "measurements": {},
  22. }
  23. def setUp(self):
  24. super().setUp()
  25. self.features = {
  26. "organizations:starfish-view": True,
  27. }
  28. def create_span(
  29. self, extra_data=None, organization=None, project=None, start_ts=None, duration=1000
  30. ):
  31. """Create span json, not required for store_span, but with no params passed should just work out of the box"""
  32. if organization is None:
  33. organization = self.organization
  34. if project is None:
  35. project = self.project
  36. if start_ts is None:
  37. start_ts = datetime.now() - timedelta(minutes=1)
  38. if extra_data is None:
  39. extra_data = {}
  40. span = self.base_span.copy()
  41. # Load some defaults
  42. span.update(
  43. {
  44. "event_id": uuid4().hex,
  45. "organization_id": organization.id,
  46. "project_id": project.id,
  47. "trace_id": uuid4().hex,
  48. "span_id": uuid4().hex[:16],
  49. "parent_span_id": uuid4().hex[:16],
  50. "segment_id": uuid4().hex[:16],
  51. "profile_id": uuid4().hex,
  52. # Multiply by 1000 cause it needs to be ms
  53. "start_timestamp_ms": int(start_ts.timestamp() * 1000),
  54. "received": start_ts.timestamp(),
  55. "duration_ms": duration,
  56. "exclusive_time_ms": duration,
  57. }
  58. )
  59. # Load any specific custom data
  60. span.update(extra_data)
  61. return span
  62. def test_simple(self):
  63. self.store_spans(
  64. [
  65. self.create_span({"description": "foo"}, start_ts=self.ten_mins_ago),
  66. self.create_span({"description": "bar"}, start_ts=self.ten_mins_ago),
  67. ]
  68. )
  69. response = self.do_request(
  70. {
  71. "field": ["description", "count()"],
  72. "query": "",
  73. "orderby": "description",
  74. "project": self.project.id,
  75. "dataset": "spansIndexed",
  76. }
  77. )
  78. assert response.status_code == 200, response.content
  79. data = response.data["data"]
  80. meta = response.data["meta"]
  81. assert len(data) == 2
  82. assert data[0]["description"] == "bar"
  83. assert data[1]["description"] == "foo"
  84. assert meta["dataset"] == "spansIndexed"