@@ -211,6 +211,193 @@ func TestMetricAlertService_Get(t *testing.T) {
211
211
require .Equal (t , expected , alert )
212
212
}
213
213
214
+ func TestMetricAlertsService_CreateWithAsyncTask (t * testing.T ) {
215
+ client , mux , _ , teardown := setup ()
216
+ defer teardown ()
217
+
218
+ mux .HandleFunc ("/api/0/projects/the-interstellar-jurisdiction/pump-station/alert-rule-task/fakeuuid/" , func (w http.ResponseWriter , r * http.Request ) {
219
+ w .Header ().Set ("Content-Type" , "application/json" )
220
+ fmt .Fprint (w , `
221
+ {
222
+ "status": "success",
223
+ "error": null,
224
+ "alertRule": {
225
+ "id": "12345",
226
+ "name": "pump-station-alert",
227
+ "environment": "production",
228
+ "dataset": "transactions",
229
+ "eventTypes": ["transaction"],
230
+ "query": "http.url:http://service/unreadmessages",
231
+ "aggregate": "p50(transaction.duration)",
232
+ "timeWindow": 10,
233
+ "thresholdType": 0,
234
+ "resolveThreshold": 0,
235
+ "triggers": [
236
+ {
237
+ "actions": [
238
+ {
239
+ "alertRuleTriggerId": "56789",
240
+ "dateCreated": "2022-04-15T15:06:01.087054Z",
241
+ "desc": "Send a Slack notification to #alert-rule-alerts",
242
+ "id": "12389",
243
+ "inputChannelId": "C0XXXFKLXXX",
244
+ "integrationId": 111,
245
+ "sentryAppId": null,
246
+ "targetIdentifier": "#alert-rule-alerts",
247
+ "targetType": "specific",
248
+ "type": "slack"
249
+ }
250
+ ],
251
+ "alertRuleId": "12345",
252
+ "alertThreshold": 10000,
253
+ "dateCreated": "2022-04-15T15:06:01.079598Z",
254
+ "id": "56789",
255
+ "label": "critical",
256
+ "resolveThreshold": 0,
257
+ "thresholdType": 0
258
+ }
259
+ ],
260
+ "projects": [
261
+ "pump-station"
262
+ ],
263
+ "owner": "pump-station:12345",
264
+ "dateCreated": "2022-04-15T15:06:01.05618Z"
265
+ }
266
+ }
267
+ ` )
268
+ })
269
+
270
+ mux .HandleFunc ("/api/0/projects/the-interstellar-jurisdiction/pump-station/alert-rules/" , func (w http.ResponseWriter , r * http.Request ) {
271
+ assertMethod (t , "POST" , r )
272
+ assertPostJSONValue (t , map [string ]interface {}{
273
+ "id" : "12345" ,
274
+ "name" : "pump-station-alert" ,
275
+ "environment" : "production" ,
276
+ "dataset" : "transactions" ,
277
+ "eventTypes" : []string {"transaction" },
278
+ "query" : "http.url:http://service/unreadmessages" ,
279
+ "aggregate" : "p50(transaction.duration)" ,
280
+ "timeWindow" : 10 ,
281
+ "thresholdType" : 0 ,
282
+ "resolveThreshold" : 0 ,
283
+ "triggers" : []map [string ]interface {}{
284
+ {
285
+ "actions" : []map [string ]interface {}{
286
+ {
287
+ "alertRuleTriggerId" : "56789" ,
288
+ "dateCreated" : "2022-04-15T15:06:01.087054Z" ,
289
+ "desc" : "Send a Slack notification to #alert-rule-alerts" ,
290
+ "id" : "12389" ,
291
+ "inputChannelId" : "C0XXXFKLXXX" ,
292
+ "integrationId" : 111 ,
293
+ "sentryAppId" : nil ,
294
+ "targetIdentifier" : "#alert-rule-alerts" ,
295
+ "targetType" : "specific" ,
296
+ "type" : "slack" ,
297
+ },
298
+ },
299
+ "alertRuleId" : "12345" ,
300
+ "alertThreshold" : 10000 ,
301
+ "dateCreated" : "2022-04-15T15:06:01.079598Z" ,
302
+ "id" : "56789" ,
303
+ "label" : "critical" ,
304
+ "resolveThreshold" : 0 ,
305
+ "thresholdType" : 0 ,
306
+ },
307
+ },
308
+ "projects" : []string {"pump-station" },
309
+ "owner" : "pump-station:12345" ,
310
+ "dateCreated" : "2022-04-15T15:06:01.05618Z" ,
311
+ }, r )
312
+
313
+ w .WriteHeader (http .StatusAccepted )
314
+ w .Header ().Set ("Content-Type" , "application/json" )
315
+ fmt .Fprint (w , `{"uuid": "fakeuuid"}` )
316
+ })
317
+
318
+ params := & MetricAlert {
319
+ Name : String ("pump-station-alert" ),
320
+ Environment : String ("production" ),
321
+ DataSet : String ("transactions" ),
322
+ Query : String ("http.url:http://service/unreadmessages" ),
323
+ Aggregate : String ("p50(transaction.duration)" ),
324
+ TimeWindow : Float64 (10.0 ),
325
+ ThresholdType : Int (0 ),
326
+ ResolveThreshold : Float64 (0 ),
327
+ Triggers : []* MetricAlertTrigger {
328
+ {
329
+ ID : String ("56789" ),
330
+ AlertRuleID : String ("12345" ),
331
+ Label : String ("critical" ),
332
+ ThresholdType : Int (0 ),
333
+ AlertThreshold : Float64 (55501.0 ),
334
+ ResolveThreshold : Float64 (100.0 ),
335
+ DateCreated : Time (mustParseTime ("2022-04-15T15:06:01.079598Z" )),
336
+ Actions : []* MetricAlertTriggerAction {
337
+ {
338
+ ID : String ("12389" ),
339
+ AlertRuleTriggerID : String ("56789" ),
340
+ Type : String ("slack" ),
341
+ TargetType : String ("specific" ),
342
+ TargetIdentifier : String ("#alert-rule-alerts" ),
343
+ InputChannelID : String ("C0XXXFKLXXX" ),
344
+ IntegrationID : Int (123 ),
345
+ DateCreated : Time (mustParseTime ("2022-04-15T15:06:01.087054Z" )),
346
+ Description : String ("Send a Slack notification to #alert-rule-alerts" ),
347
+ },
348
+ },
349
+ },
350
+ },
351
+ Projects : []string {"pump-station" },
352
+ Owner : String ("pump-station:12345" ),
353
+ }
354
+ ctx := context .Background ()
355
+ alertRule , _ , err := client .MetricAlerts .Create (ctx , "the-interstellar-jurisdiction" , "pump-station" , params )
356
+ require .NoError (t , err )
357
+
358
+ expected := & MetricAlert {
359
+ ID : String ("12345" ),
360
+ Name : String ("pump-station-alert" ),
361
+ Environment : String ("production" ),
362
+ DataSet : String ("transactions" ),
363
+ EventTypes : []string {"transaction" },
364
+ Query : String ("http.url:http://service/unreadmessages" ),
365
+ Aggregate : String ("p50(transaction.duration)" ),
366
+ ThresholdType : Int (0 ),
367
+ ResolveThreshold : Float64 (0 ),
368
+ TimeWindow : Float64 (10.0 ),
369
+ Triggers : []* MetricAlertTrigger {
370
+ {
371
+ ID : String ("56789" ),
372
+ AlertRuleID : String ("12345" ),
373
+ Label : String ("critical" ),
374
+ ThresholdType : Int (0 ),
375
+ AlertThreshold : Float64 (10000.0 ),
376
+ ResolveThreshold : Float64 (0.0 ),
377
+ DateCreated : Time (mustParseTime ("2022-04-15T15:06:01.079598Z" )),
378
+ Actions : []* MetricAlertTriggerAction {
379
+ {
380
+ ID : String ("12389" ),
381
+ AlertRuleTriggerID : String ("56789" ),
382
+ Type : String ("slack" ),
383
+ TargetType : String ("specific" ),
384
+ TargetIdentifier : String ("#alert-rule-alerts" ),
385
+ InputChannelID : String ("C0XXXFKLXXX" ),
386
+ IntegrationID : Int (111 ),
387
+ DateCreated : Time (mustParseTime ("2022-04-15T15:06:01.087054Z" )),
388
+ Description : String ("Send a Slack notification to #alert-rule-alerts" ),
389
+ },
390
+ },
391
+ },
392
+ },
393
+ Projects : []string {"pump-station" },
394
+ Owner : String ("pump-station:12345" ),
395
+ DateCreated : Time (mustParseTime ("2022-04-15T15:06:01.05618Z" )),
396
+ }
397
+
398
+ require .Equal (t , expected , alertRule )
399
+ }
400
+
214
401
func TestMetricAlertService_Create (t * testing.T ) {
215
402
client , mux , _ , teardown := setup ()
216
403
defer teardown ()
0 commit comments