test_organization_events_span_indexed.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. "group_raw": uuid4().hex[:16],
  52. "profile_id": uuid4().hex,
  53. # Multiply by 1000 cause it needs to be ms
  54. "start_timestamp_ms": int(start_ts.timestamp() * 1000),
  55. "received": start_ts.timestamp(),
  56. "duration_ms": duration,
  57. "exclusive_time_ms": duration,
  58. }
  59. )
  60. # Load any specific custom data
  61. span.update(extra_data)
  62. return span
  63. def test_simple(self):
  64. self.store_spans(
  65. [
  66. self.create_span({"description": "foo"}, start_ts=self.ten_mins_ago),
  67. self.create_span({"description": "bar"}, start_ts=self.ten_mins_ago),
  68. ]
  69. )
  70. response = self.do_request(
  71. {
  72. "field": ["description", "count()"],
  73. "query": "",
  74. "orderby": "description",
  75. "project": self.project.id,
  76. "dataset": "spansIndexed",
  77. }
  78. )
  79. assert response.status_code == 200, response.content
  80. data = response.data["data"]
  81. meta = response.data["meta"]
  82. assert len(data) == 2
  83. assert data[0]["description"] == "bar"
  84. assert data[1]["description"] == "foo"
  85. assert meta["dataset"] == "spansIndexed"