25
25
MonitorStatus ,
26
26
ScheduleType ,
27
27
)
28
+ from sentry .monitors .utils import ensure_cron_detector , get_detector_for_monitor
28
29
from sentry .testutils .cases import TestCase
29
30
30
31
31
32
class IncidentOccurrenceTestCase (TestCase ):
32
- @mock .patch ("sentry.monitors.logic.incident_occurrence.produce_occurrence_to_kafka" )
33
- def test_send_incident_occurrence (
34
- self , mock_produce_occurrence_to_kafka : mock .MagicMock
35
- ) -> None :
36
- monitor = Monitor .objects .create (
33
+ def build_occurrence_test_data (self ):
34
+ self .monitor = Monitor .objects .create (
37
35
name = "test monitor" ,
38
36
organization_id = self .organization .id ,
39
37
project_id = self .project .id ,
@@ -44,51 +42,56 @@ def test_send_incident_occurrence(
44
42
"checkin_margin" : None ,
45
43
},
46
44
)
47
- monitor_environment = MonitorEnvironment .objects .create (
48
- monitor = monitor ,
45
+ self . monitor_environment = MonitorEnvironment .objects .create (
46
+ monitor = self . monitor ,
49
47
environment_id = self .environment .id ,
50
48
status = MonitorStatus .ERROR ,
51
49
)
52
50
53
- successful_checkin = MonitorCheckIn .objects .create (
54
- monitor = monitor ,
55
- monitor_environment = monitor_environment ,
51
+ self . successful_checkin = MonitorCheckIn .objects .create (
52
+ monitor = self . monitor ,
53
+ monitor_environment = self . monitor_environment ,
56
54
project_id = self .project .id ,
57
55
status = CheckInStatus .OK ,
58
56
)
59
57
60
- last_checkin = timezone .now ()
61
- trace_id = uuid .uuid4 ()
58
+ self . last_checkin = timezone .now ()
59
+ self . trace_id = uuid .uuid4 ()
62
60
63
- timeout_checkin = MonitorCheckIn .objects .create (
64
- monitor = monitor ,
65
- monitor_environment = monitor_environment ,
61
+ self . timeout_checkin = MonitorCheckIn .objects .create (
62
+ monitor = self . monitor ,
63
+ monitor_environment = self . monitor_environment ,
66
64
project_id = self .project .id ,
67
65
status = CheckInStatus .TIMEOUT ,
68
66
trace_id = uuid .uuid4 (),
69
- date_added = last_checkin - timedelta (minutes = 1 ),
67
+ date_added = self . last_checkin - timedelta (minutes = 1 ),
70
68
)
71
- failed_checkin = MonitorCheckIn .objects .create (
72
- monitor = monitor ,
73
- monitor_environment = monitor_environment ,
69
+ self . failed_checkin = MonitorCheckIn .objects .create (
70
+ monitor = self . monitor ,
71
+ monitor_environment = self . monitor_environment ,
74
72
project_id = self .project .id ,
75
73
status = CheckInStatus .ERROR ,
76
- trace_id = trace_id ,
77
- date_added = last_checkin ,
74
+ trace_id = self . trace_id ,
75
+ date_added = self . last_checkin ,
78
76
)
79
- incident = MonitorIncident .objects .create (
80
- monitor = monitor ,
81
- monitor_environment = monitor_environment ,
82
- starting_checkin = failed_checkin ,
83
- starting_timestamp = last_checkin ,
77
+ self . incident = MonitorIncident .objects .create (
78
+ monitor = self . monitor ,
79
+ monitor_environment = self . monitor_environment ,
80
+ starting_checkin = self . failed_checkin ,
81
+ starting_timestamp = self . last_checkin ,
84
82
grouphash = "abcd" ,
85
83
)
86
84
85
+ @mock .patch ("sentry.monitors.logic.incident_occurrence.produce_occurrence_to_kafka" )
86
+ def test_send_incident_occurrence (
87
+ self , mock_produce_occurrence_to_kafka : mock .MagicMock
88
+ ) -> None :
89
+ self .build_occurrence_test_data ()
87
90
send_incident_occurrence (
88
- failed_checkin ,
89
- [timeout_checkin , failed_checkin ],
90
- incident ,
91
- last_checkin ,
91
+ self . failed_checkin ,
92
+ [self . timeout_checkin , self . failed_checkin ],
93
+ self . incident ,
94
+ self . last_checkin ,
92
95
)
93
96
94
97
assert mock_produce_occurrence_to_kafka .call_count == 1
@@ -102,8 +105,8 @@ def test_send_incident_occurrence(
102
105
occurrence ,
103
106
** {
104
107
"project_id" : self .project .id ,
105
- "fingerprint" : [incident .grouphash ],
106
- "issue_title" : f"Monitor failure: { monitor .name } " ,
108
+ "fingerprint" : [self . incident .grouphash ],
109
+ "issue_title" : f"Monitor failure: { self . monitor .name } " ,
107
110
"subtitle" : "Your monitor has reached its failure threshold." ,
108
111
"resource_id" : None ,
109
112
"evidence_data" : {},
@@ -115,12 +118,96 @@ def test_send_incident_occurrence(
115
118
},
116
119
{
117
120
"name" : "Environment" ,
118
- "value" : monitor_environment .get_environment ().name ,
121
+ "value" : self .monitor_environment .get_environment ().name ,
122
+ "important" : False ,
123
+ },
124
+ {
125
+ "name" : "Last successful check-in" ,
126
+ "value" : self .successful_checkin .date_added .isoformat (),
127
+ "important" : False ,
128
+ },
129
+ ],
130
+ "type" : MonitorIncidentType .type_id ,
131
+ "level" : "error" ,
132
+ "culprit" : "" ,
133
+ },
134
+ ) == dict (occurrence )
135
+
136
+ assert dict (
137
+ event ,
138
+ ** {
139
+ "contexts" : {
140
+ "monitor" : {
141
+ "status" : "error" ,
142
+ "config" : self .monitor .config ,
143
+ "id" : str (self .monitor .guid ),
144
+ "name" : self .monitor .name ,
145
+ "slug" : self .monitor .slug ,
146
+ },
147
+ "trace" : {
148
+ "trace_id" : self .trace_id .hex ,
149
+ "span_id" : None ,
150
+ },
151
+ },
152
+ "environment" : self .monitor_environment .get_environment ().name ,
153
+ "event_id" : occurrence ["event_id" ],
154
+ "fingerprint" : [self .incident .grouphash ],
155
+ "platform" : "other" ,
156
+ "project_id" : self .monitor .project_id ,
157
+ "sdk" : None ,
158
+ "tags" : {
159
+ "monitor.id" : str (self .monitor .guid ),
160
+ "monitor.slug" : str (self .monitor .slug ),
161
+ "monitor.incident" : str (self .incident .id ),
162
+ },
163
+ },
164
+ ) == dict (event )
165
+
166
+ @mock .patch ("sentry.monitors.logic.incident_occurrence.produce_occurrence_to_kafka" )
167
+ def test_send_incident_occurrence_detector (
168
+ self , mock_produce_occurrence_to_kafka : mock .MagicMock
169
+ ) -> None :
170
+ self .build_occurrence_test_data ()
171
+ ensure_cron_detector (self .monitor )
172
+ send_incident_occurrence (
173
+ self .failed_checkin ,
174
+ [self .timeout_checkin , self .failed_checkin ],
175
+ self .incident ,
176
+ self .last_checkin ,
177
+ )
178
+
179
+ assert mock_produce_occurrence_to_kafka .call_count == 1
180
+ kwargs = mock_produce_occurrence_to_kafka .call_args .kwargs
181
+
182
+ occurrence = kwargs ["occurrence" ]
183
+ event = kwargs ["event_data" ]
184
+ occurrence = occurrence .to_dict ()
185
+
186
+ detector = get_detector_for_monitor (self .monitor )
187
+ assert detector
188
+ assert dict (
189
+ occurrence ,
190
+ ** {
191
+ "project_id" : self .project .id ,
192
+ "fingerprint" : [self .incident .grouphash ],
193
+ "issue_title" : f"Monitor failure: { self .monitor .name } " ,
194
+ "subtitle" : "Your monitor has reached its failure threshold." ,
195
+ "resource_id" : None ,
196
+ "evidence_data" : {"detector_id" : detector .id },
197
+ "evidence_display" : [
198
+ {
199
+ "name" : "Failure reason" ,
200
+ "value" : "1 timeout and 1 error check-ins detected" ,
201
+ "important" : True ,
202
+ },
203
+ {
204
+ "name" : "Environment" ,
205
+ "value" : self .monitor_environment .get_environment ().name ,
119
206
"important" : False ,
120
207
},
121
208
{
122
209
"name" : "Last successful check-in" ,
123
- "value" : successful_checkin .date_added .isoformat (),
210
+ "value" : self . successful_checkin .date_added .isoformat (),
124
211
"important" : False ,
125
212
},
126
213
],
@@ -136,26 +223,26 @@ def test_send_incident_occurrence(
136
223
"contexts" : {
137
224
"monitor" : {
138
225
"status" : "error" ,
139
- "config" : monitor .config ,
140
- "id" : str (monitor .guid ),
141
- "name" : monitor .name ,
142
- "slug" : monitor .slug ,
226
+ "config" : self . monitor .config ,
227
+ "id" : str (self . monitor .guid ),
228
+ "name" : self . monitor .name ,
229
+ "slug" : self . monitor .slug ,
143
230
},
144
231
"trace" : {
145
- "trace_id" : trace_id .hex ,
232
+ "trace_id" : self . trace_id .hex ,
146
233
"span_id" : None ,
147
234
},
148
235
},
149
- "environment" : monitor_environment .get_environment ().name ,
236
+ "environment" : self . monitor_environment .get_environment ().name ,
150
237
"event_id" : occurrence ["event_id" ],
151
- "fingerprint" : [incident .grouphash ],
238
+ "fingerprint" : [self . incident .grouphash ],
152
239
"platform" : "other" ,
153
- "project_id" : monitor .project_id ,
240
+ "project_id" : self . monitor .project_id ,
154
241
"sdk" : None ,
155
242
"tags" : {
156
- "monitor.id" : str (monitor .guid ),
157
- "monitor.slug" : str (monitor .slug ),
158
- "monitor.incident" : str (incident .id ),
243
+ "monitor.id" : str (self . monitor .guid ),
244
+ "monitor.slug" : str (self . monitor .slug ),
245
+ "monitor.incident" : str (self . incident .id ),
159
246
},
160
247
},
161
248
) == dict (event )
0 commit comments