4
4
from datetime import datetime , timezone
5
5
6
6
import sentry_sdk
7
+ from arroyo import Topic as ArroyoTopic
8
+ from arroyo .backends .kafka import KafkaPayload , KafkaProducer , build_kafka_configuration
7
9
from django .conf import settings
10
+ from sentry_kafka_schemas .schema_types .monitors_clock_tick_v1 import ClockTick
8
11
9
12
from sentry import options
13
+ from sentry .conf .types .kafka_definition import Topic
10
14
from sentry .monitors .tasks .check_missed import check_missing
11
15
from sentry .monitors .tasks .check_timeout import check_timeout
12
- from sentry .utils import metrics , redis
16
+ from sentry .utils import json , metrics , redis
17
+ from sentry .utils .arroyo_producer import SingletonProducer
18
+ from sentry .utils .kafka_config import get_kafka_producer_cluster_options , get_topic_definition
13
19
14
20
logger = logging .getLogger ("sentry" )
15
21
# This key is used to store the last timestamp that the tasks were triggered.
@@ -26,6 +32,17 @@ def _int_or_none(s: str | None) -> int | None:
26
32
return int (s )
27
33
28
34
35
+ def _get_producer () -> KafkaProducer :
36
+ cluster_name = get_topic_definition (Topic .MONITORS_CLOCK_TICK )["cluster" ]
37
+ producer_config = get_kafka_producer_cluster_options (cluster_name )
38
+ producer_config .pop ("compression.type" , None )
39
+ producer_config .pop ("message.max.bytes" , None )
40
+ return KafkaProducer (build_kafka_configuration (default_config = producer_config ))
41
+
42
+
43
+ _clock_tick_producer = SingletonProducer (_get_producer )
44
+
45
+
29
46
def _dispatch_tick (ts : datetime ):
30
47
"""
31
48
Dispatch a clock tick which will trigger monitor tasks.
@@ -43,9 +60,15 @@ def _dispatch_tick(ts: datetime):
43
60
skip any tasks it missed)
44
61
"""
45
62
if options .get ("crons.use_clock_pulse_consumer" ):
46
- # TODO(epurkhiser): This should dispatch the pulse as a message on the
47
- # monitors-clock-pulse topic
48
- pass
63
+ if settings .SENTRY_EVENTSTREAM != "sentry.eventstream.kafka.KafkaEventStream" :
64
+ # XXX(epurkhiser): Unclear what we want to do if we're not using kafka
65
+ return
66
+
67
+ message : ClockTick = {"ts" : ts .timestamp ()}
68
+ payload = KafkaPayload (None , json .dumps (message ).encode ("utf-8" ), [])
69
+
70
+ topic = get_topic_definition (Topic .MONITORS_CLOCK_TICK )["real_topic_name" ]
71
+ _clock_tick_producer .produce (ArroyoTopic (topic ), payload )
49
72
else :
50
73
check_missing .delay (current_datetime = ts )
51
74
check_timeout .delay (current_datetime = ts )
0 commit comments