@@ -27,10 +27,10 @@ use base64::Engine;
27
27
use bytes:: Bytes ;
28
28
use chrono:: Utc ;
29
29
use http:: { header:: AUTHORIZATION , HeaderMap , HeaderValue } ;
30
- use humantime_serde:: re:: humantime;
31
30
use itertools:: Itertools ;
32
31
use once_cell:: sync:: Lazy ;
33
32
use reqwest:: ClientBuilder ;
33
+ use serde_json:: { json, Value } ;
34
34
use tokio:: sync:: RwLock ;
35
35
use tracing:: { error, trace, warn} ;
36
36
use ulid:: Ulid ;
@@ -66,14 +66,6 @@ impl TargetConfigs {
66
66
67
67
pub async fn update ( & self , target : Target ) -> Result < ( ) , AlertError > {
68
68
let mut map = self . target_configs . write ( ) . await ;
69
- if map. values ( ) . any ( |t| {
70
- t. target == target. target
71
- && t. timeout . interval == target. timeout . interval
72
- && t. timeout . times == target. timeout . times
73
- && t. id != target. id
74
- } ) {
75
- return Err ( AlertError :: DuplicateTargetConfig ) ;
76
- }
77
69
map. insert ( target. id , target. clone ( ) ) ;
78
70
79
71
let path = target_json_path ( & target. id ) ;
@@ -148,16 +140,72 @@ pub struct Target {
148
140
pub name : String ,
149
141
#[ serde( flatten) ]
150
142
pub target : TargetType ,
151
- #[ serde( default , rename = "repeat" ) ]
152
- pub timeout : Timeout ,
143
+ pub notification_config : Timeout ,
153
144
#[ serde( default = "Ulid::new" ) ]
154
145
pub id : Ulid ,
155
146
}
156
147
157
148
impl Target {
149
+ pub fn mask ( self ) -> Value {
150
+ match self . target {
151
+ TargetType :: Slack ( slack_web_hook) => {
152
+ let endpoint = slack_web_hook. endpoint . to_string ( ) ;
153
+ let masked_endpoint = if endpoint. len ( ) > 20 {
154
+ format ! ( "{}********" , & endpoint[ ..20 ] )
155
+ } else {
156
+ "********" . to_string ( )
157
+ } ;
158
+ json ! ( {
159
+ "name" : self . name,
160
+ "type" : "slack" ,
161
+ "endpoint" : masked_endpoint,
162
+ "notificationConfig" : self . notification_config,
163
+ "id" : self . id
164
+ } )
165
+ }
166
+ TargetType :: Other ( other_web_hook) => {
167
+ json ! ( {
168
+ "name" : self . name,
169
+ "type" : "webhook" ,
170
+ "endpoint" : other_web_hook. endpoint,
171
+ "headers" : other_web_hook. headers,
172
+ "skipTlsCheck" : other_web_hook. skip_tls_check,
173
+ "notificationConfig" : self . notification_config,
174
+ "id" : self . id
175
+ } )
176
+ }
177
+ TargetType :: AlertManager ( alert_manager) => {
178
+ if let Some ( auth) = alert_manager. auth {
179
+ let password = "********" ;
180
+ json ! ( {
181
+ "name" : self . name,
182
+ "type" : "webhook" ,
183
+ "endpoint" : alert_manager. endpoint,
184
+ "username" : auth. username,
185
+ "password" : password,
186
+ "skipTlsCheck" : alert_manager. skip_tls_check,
187
+ "notificationConfig" : self . notification_config,
188
+ "id" : self . id
189
+ } )
190
+ } else {
191
+ json ! ( {
192
+ "name" : self . name,
193
+ "type" : "webhook" ,
194
+ "endpoint" : alert_manager. endpoint,
195
+ "username" : Value :: Null ,
196
+ "password" : Value :: Null ,
197
+ "skipTlsCheck" : alert_manager. skip_tls_check,
198
+ "notificationConfig" : self . notification_config,
199
+ "id" : self . id
200
+ } )
201
+ }
202
+ }
203
+ }
204
+ }
205
+
158
206
pub fn call ( & self , context : Context ) {
159
207
trace ! ( "target.call context- {context:?}" ) ;
160
- let timeout = & self . timeout ;
208
+ let timeout = & self . notification_config ;
161
209
let resolves = context. alert_info . alert_state ;
162
210
let mut state = timeout. state . lock ( ) . unwrap ( ) ;
163
211
trace ! ( "target.call state- {state:?}" ) ;
@@ -205,7 +253,7 @@ impl Target {
205
253
let sleep_and_check_if_call =
206
254
move |timeout_state : Arc < Mutex < TimeoutState > > , current_state : AlertState | {
207
255
async move {
208
- tokio:: time:: sleep ( timeout) . await ;
256
+ tokio:: time:: sleep ( Duration :: from_secs ( timeout * 60 ) ) . await ;
209
257
210
258
let mut state = timeout_state. lock ( ) . unwrap ( ) ;
211
259
@@ -276,8 +324,8 @@ fn call_target(target: TargetType, context: Context) {
276
324
}
277
325
278
326
#[ derive( Debug , serde:: Deserialize ) ]
279
- pub struct RepeatVerifier {
280
- interval : Option < String > ,
327
+ pub struct NotificationConfigVerifier {
328
+ interval : Option < u64 > ,
281
329
times : Option < usize > ,
282
330
}
283
331
@@ -288,7 +336,7 @@ pub struct TargetVerifier {
288
336
#[ serde( flatten) ]
289
337
pub target : TargetType ,
290
338
#[ serde( default ) ]
291
- pub repeat : Option < RepeatVerifier > ,
339
+ pub notification_config : Option < NotificationConfigVerifier > ,
292
340
#[ serde( default = "Ulid::new" ) ]
293
341
pub id : Ulid ,
294
342
}
@@ -304,26 +352,22 @@ impl TryFrom<TargetVerifier> for Target {
304
352
timeout. times = Retry :: Infinite
305
353
}
306
354
307
- if let Some ( repeat_config) = value. repeat {
308
- let interval = repeat_config
309
- . interval
310
- . map ( |ref interval| humantime:: parse_duration ( interval) )
311
- . transpose ( )
312
- . map_err ( |err| err. to_string ( ) ) ?;
355
+ if let Some ( notification_config) = value. notification_config {
356
+ let interval = notification_config. interval . map ( |ref interval| * interval) ;
313
357
314
358
if let Some ( interval) = interval {
315
359
timeout. interval = interval
316
360
}
317
361
318
- if let Some ( times) = repeat_config . times {
362
+ if let Some ( times) = notification_config . times {
319
363
timeout. times = Retry :: Finite ( times)
320
364
}
321
365
}
322
366
323
367
Ok ( Target {
324
368
name : value. name ,
325
369
target : value. target ,
326
- timeout,
370
+ notification_config : timeout,
327
371
id : value. id ,
328
372
} )
329
373
}
@@ -518,8 +562,7 @@ impl CallableTarget for AlertManager {
518
562
519
563
#[ derive( Debug , serde:: Serialize , serde:: Deserialize , Clone ) ]
520
564
pub struct Timeout {
521
- #[ serde( with = "humantime_serde" ) ]
522
- pub interval : Duration ,
565
+ pub interval : u64 ,
523
566
#[ serde( default = "Retry::default" ) ]
524
567
pub times : Retry ,
525
568
#[ serde( skip) ]
@@ -529,7 +572,7 @@ pub struct Timeout {
529
572
impl Default for Timeout {
530
573
fn default ( ) -> Self {
531
574
Self {
532
- interval : Duration :: from_secs ( 60 ) ,
575
+ interval : 1 ,
533
576
times : Retry :: default ( ) ,
534
577
state : Arc :: < Mutex < TimeoutState > > :: default ( ) ,
535
578
}
0 commit comments