5
5
from sentry .incidents .action_handlers import SentryAppActionHandler
6
6
from sentry .incidents .models .alert_rule import AlertRuleTriggerAction
7
7
from sentry .incidents .models .incident import IncidentStatus
8
+ from sentry .integrations .types import EventLifecycleOutcome
9
+ from sentry .sentry_apps .metrics import SentryAppWebhookHaltReason
10
+ from sentry .testutils .asserts import (
11
+ assert_count_of_metric ,
12
+ assert_halt_metric ,
13
+ assert_success_metric ,
14
+ )
8
15
from sentry .testutils .helpers .datetime import freeze_time
9
16
from sentry .utils import json
10
17
@@ -63,7 +70,8 @@ def run_test(self, incident, method):
63
70
)
64
71
65
72
@responses .activate
66
- def test_rule_snoozed (self ):
73
+ @patch ("sentry.integrations.utils.metrics.EventLifecycle.record_event" )
74
+ def test_rule_snoozed (self , mock_record ):
67
75
alert_rule = self .create_alert_rule ()
68
76
incident = self .create_incident (alert_rule = alert_rule , status = IncidentStatus .CLOSED .value )
69
77
self .snooze_rule (alert_rule = alert_rule )
@@ -88,11 +96,82 @@ def test_rule_snoozed(self):
88
96
89
97
assert len (responses .calls ) == 0
90
98
91
- def test_fire_metric_alert (self ):
99
+ # SLO asserts
100
+ # PREPARE_WEBHOOK (never got to this point)
101
+ assert_count_of_metric (
102
+ mock_record = mock_record , outcome = EventLifecycleOutcome .STARTED , outcome_count = 0
103
+ )
104
+ assert_count_of_metric (
105
+ mock_record = mock_record , outcome = EventLifecycleOutcome .SUCCESS , outcome_count = 0
106
+ )
107
+
108
+ @responses .activate
109
+ @patch ("sentry.integrations.utils.metrics.EventLifecycle.record_event" )
110
+ def test_rule_bad_response (self , mock_record ):
111
+ alert_rule = self .create_alert_rule ()
112
+ incident = self .create_incident (alert_rule = alert_rule , status = IncidentStatus .CLOSED .value )
113
+
114
+ responses .add (
115
+ method = responses .POST ,
116
+ url = "https://example.com/webhook" ,
117
+ status = 400 ,
118
+ content_type = "application/json" ,
119
+ body = json .dumps ({"ok" : "true" }),
120
+ )
121
+
122
+ metric_value = 1000
123
+ with self .tasks ():
124
+ self .handler .fire (
125
+ self .action , incident , self .project , metric_value , IncidentStatus (incident .status )
126
+ )
127
+
128
+ assert len (responses .calls ) == 1
129
+
130
+ # SLO asserts
131
+ assert_halt_metric (
132
+ mock_record = mock_record ,
133
+ error_msg = f"send_and_save_webhook_request.{ SentryAppWebhookHaltReason .GOT_CLIENT_ERROR } _{ 400 } " ,
134
+ )
135
+
136
+ # PREPARE_WEBHOOK (success) -> SEND_WEBHOOK (halt)
137
+ assert_count_of_metric (
138
+ mock_record = mock_record , outcome = EventLifecycleOutcome .STARTED , outcome_count = 2
139
+ )
140
+ assert_count_of_metric (
141
+ mock_record = mock_record , outcome = EventLifecycleOutcome .SUCCESS , outcome_count = 1
142
+ )
143
+ assert_count_of_metric (
144
+ mock_record = mock_record , outcome = EventLifecycleOutcome .HALTED , outcome_count = 1
145
+ )
146
+
147
+ @patch ("sentry.integrations.utils.metrics.EventLifecycle.record_event" )
148
+ def test_fire_metric_alert (self , mock_record ):
92
149
self .run_fire_test ()
93
150
94
- def test_resolve_metric_alert (self ):
151
+ # SLO asserts
152
+ assert_success_metric (mock_record )
153
+
154
+ # PREPARE_WEBHOOK (success) -> SEND_WEBHOOK (success)
155
+ assert_count_of_metric (
156
+ mock_record = mock_record , outcome = EventLifecycleOutcome .STARTED , outcome_count = 2
157
+ )
158
+ assert_count_of_metric (
159
+ mock_record = mock_record , outcome = EventLifecycleOutcome .SUCCESS , outcome_count = 2
160
+ )
161
+
162
+ @patch ("sentry.integrations.utils.metrics.EventLifecycle.record_event" )
163
+ def test_resolve_metric_alert (self , mock_record ):
95
164
self .run_fire_test ("resolve" )
165
+ # SLO asserts
166
+ assert_success_metric (mock_record )
167
+
168
+ # PREPARE_WEBHOOK (success) -> SEND_WEBHOOK (success)
169
+ assert_count_of_metric (
170
+ mock_record = mock_record , outcome = EventLifecycleOutcome .STARTED , outcome_count = 2
171
+ )
172
+ assert_count_of_metric (
173
+ mock_record = mock_record , outcome = EventLifecycleOutcome .SUCCESS , outcome_count = 2
174
+ )
96
175
97
176
98
177
@freeze_time ()
@@ -169,14 +248,39 @@ def run_test(self, incident, method):
169
248
{"name" : "teamId" , "value" : 1 },
170
249
]
171
250
172
- def test_fire_metric_alert (self ):
251
+ @patch ("sentry.integrations.utils.metrics.EventLifecycle.record_event" )
252
+ def test_fire_metric_alert (self , mock_record ):
173
253
self .run_fire_test ()
174
254
175
- def test_resolve_metric_alert (self ):
255
+ # SLO asserts
256
+ assert_success_metric (mock_record )
257
+
258
+ # PREPARE_WEBHOOK (success) -> SEND_WEBHOOK (success)
259
+ assert_count_of_metric (
260
+ mock_record = mock_record , outcome = EventLifecycleOutcome .STARTED , outcome_count = 2
261
+ )
262
+ assert_count_of_metric (
263
+ mock_record = mock_record , outcome = EventLifecycleOutcome .SUCCESS , outcome_count = 2
264
+ )
265
+
266
+ @patch ("sentry.integrations.utils.metrics.EventLifecycle.record_event" )
267
+ def test_resolve_metric_alert (self , mock_record ):
176
268
self .run_fire_test ("resolve" )
177
269
270
+ # SLO asserts
271
+ assert_success_metric (mock_record )
272
+
273
+ # PREPARE_WEBHOOK (success) -> SEND_WEBHOOK (success)
274
+ assert_count_of_metric (
275
+ mock_record = mock_record , outcome = EventLifecycleOutcome .STARTED , outcome_count = 2
276
+ )
277
+ assert_count_of_metric (
278
+ mock_record = mock_record , outcome = EventLifecycleOutcome .SUCCESS , outcome_count = 2
279
+ )
280
+
281
+ @patch ("sentry.integrations.utils.metrics.EventLifecycle.record_event" )
178
282
@patch ("sentry.analytics.record" )
179
- def test_alert_sent_recorded (self , mock_record ):
283
+ def test_alert_sent_recorded (self , mock_record , mock_record_event ):
180
284
self .run_fire_test ()
181
285
mock_record .assert_called_with (
182
286
"alert.sent" ,
@@ -188,3 +292,14 @@ def test_alert_sent_recorded(self, mock_record):
188
292
external_id = str (self .action .sentry_app_id ),
189
293
notification_uuid = "" ,
190
294
)
295
+
296
+ # SLO asserts
297
+ assert_success_metric (mock_record_event )
298
+
299
+ # PREPARE_WEBHOOK (success) -> SEND_WEBHOOK (success)
300
+ assert_count_of_metric (
301
+ mock_record = mock_record_event , outcome = EventLifecycleOutcome .STARTED , outcome_count = 2
302
+ )
303
+ assert_count_of_metric (
304
+ mock_record = mock_record_event , outcome = EventLifecycleOutcome .SUCCESS , outcome_count = 2
305
+ )
0 commit comments