test_eventstream.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import logging
  2. import time
  3. from datetime import datetime, timedelta
  4. from unittest.mock import Mock, patch
  5. from django.conf import settings
  6. from sentry.event_manager import EventManager
  7. from sentry.eventstream.kafka import KafkaEventStream
  8. from sentry.eventstream.snuba import SnubaEventStream
  9. from sentry.testutils import SnubaTestCase, TestCase
  10. from sentry.utils import json, snuba
  11. from sentry.utils.samples import load_data
  12. class SnubaEventStreamTest(TestCase, SnubaTestCase):
  13. def setUp(self):
  14. super().setUp()
  15. self.kafka_eventstream = KafkaEventStream()
  16. self.kafka_eventstream.producer = Mock()
  17. def __build_event(self, timestamp):
  18. raw_event = {
  19. "event_id": "a" * 32,
  20. "message": "foo",
  21. "timestamp": time.mktime(timestamp.timetuple()),
  22. "level": logging.ERROR,
  23. "logger": "default",
  24. "tags": [],
  25. }
  26. manager = EventManager(raw_event)
  27. manager.normalize()
  28. return manager.save(self.project.id)
  29. def __build_transaction_event(self):
  30. manager = EventManager(load_data("transaction"))
  31. manager.normalize()
  32. return manager.save(self.project.id)
  33. def __produce_event(self, *insert_args, **insert_kwargs):
  34. # pass arguments on to Kafka EventManager
  35. self.kafka_eventstream.insert(*insert_args, **insert_kwargs)
  36. produce_args, produce_kwargs = list(self.kafka_eventstream.producer.produce.call_args)
  37. assert not produce_args
  38. assert produce_kwargs["topic"] == settings.KAFKA_EVENTS
  39. assert produce_kwargs["key"] == str(self.project.id).encode("utf-8")
  40. version, type_, payload1, payload2 = json.loads(produce_kwargs["value"])
  41. assert version == 2
  42. assert type_ == "insert"
  43. # insert what would have been the Kafka payload directly
  44. # into Snuba, expect an HTTP 200 and for the event to now exist
  45. snuba_eventstream = SnubaEventStream()
  46. snuba_eventstream._send(
  47. self.project.id,
  48. "insert",
  49. (payload1, payload2),
  50. is_transaction_event=insert_kwargs["group"] is None,
  51. )
  52. @patch("sentry.eventstream.insert")
  53. def test(self, mock_eventstream_insert):
  54. now = datetime.utcnow()
  55. event = self.__build_event(now)
  56. # verify eventstream was called by EventManager
  57. insert_args, insert_kwargs = list(mock_eventstream_insert.call_args)
  58. assert not insert_args
  59. assert insert_kwargs == {
  60. "event": event,
  61. "group": event.group,
  62. "is_new_group_environment": True,
  63. "is_new": True,
  64. "is_regression": False,
  65. "primary_hash": "acbd18db4cc2f85cedef654fccc4a4d8",
  66. "skip_consume": False,
  67. "received_timestamp": event.data["received"],
  68. }
  69. self.__produce_event(*insert_args, **insert_kwargs)
  70. assert (
  71. snuba.query(
  72. start=now - timedelta(days=1),
  73. end=now + timedelta(days=1),
  74. groupby=["project_id"],
  75. filter_keys={"project_id": [self.project.id]},
  76. ).get(self.project.id, 0)
  77. == 1
  78. )
  79. @patch("sentry.eventstream.insert")
  80. def test_issueless(self, mock_eventstream_insert):
  81. now = datetime.utcnow()
  82. event = self.__build_transaction_event()
  83. event.group_id = None
  84. insert_args = ()
  85. insert_kwargs = {
  86. "event": event,
  87. "group": None,
  88. "is_new_group_environment": True,
  89. "is_new": True,
  90. "is_regression": False,
  91. "primary_hash": "acbd18db4cc2f85cedef654fccc4a4d8",
  92. "skip_consume": False,
  93. "received_timestamp": event.data["received"],
  94. }
  95. self.__produce_event(*insert_args, **insert_kwargs)
  96. result = snuba.raw_query(
  97. dataset=snuba.Dataset.Transactions,
  98. start=now - timedelta(days=1),
  99. end=now + timedelta(days=1),
  100. selected_columns=["event_id"],
  101. groupby=None,
  102. filter_keys={"project_id": [self.project.id], "event_id": [event.event_id]},
  103. )
  104. assert len(result["data"]) == 1