@@ -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,84 @@ 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
+ let endpoint = other_web_hook. endpoint . to_string ( ) ;
168
+ let masked_endpoint = if endpoint. len ( ) > 20 {
169
+ format ! ( "{}********" , & endpoint[ ..20 ] )
170
+ } else {
171
+ "********" . to_string ( )
172
+ } ;
173
+ json ! ( {
174
+ "name" : self . name,
175
+ "type" : "webhook" ,
176
+ "endpoint" : masked_endpoint,
177
+ "headers" : other_web_hook. headers,
178
+ "skipTlsCheck" : other_web_hook. skip_tls_check,
179
+ "notificationConfig" : self . notification_config,
180
+ "id" : self . id
181
+ } )
182
+ }
183
+ TargetType :: AlertManager ( alert_manager) => {
184
+ let endpoint = alert_manager. endpoint . to_string ( ) ;
185
+ let masked_endpoint = if endpoint. len ( ) > 20 {
186
+ format ! ( "{}********" , & endpoint[ ..20 ] )
187
+ } else {
188
+ "********" . to_string ( )
189
+ } ;
190
+ if let Some ( auth) = alert_manager. auth {
191
+ let password = "********" ;
192
+ json ! ( {
193
+ "name" : self . name,
194
+ "type" : "webhook" ,
195
+ "endpoint" : masked_endpoint,
196
+ "username" : auth. username,
197
+ "password" : password,
198
+ "skipTlsCheck" : alert_manager. skip_tls_check,
199
+ "notificationConfig" : self . notification_config,
200
+ "id" : self . id
201
+ } )
202
+ } else {
203
+ json ! ( {
204
+ "name" : self . name,
205
+ "type" : "webhook" ,
206
+ "endpoint" : masked_endpoint,
207
+ "username" : Value :: Null ,
208
+ "password" : Value :: Null ,
209
+ "skipTlsCheck" : alert_manager. skip_tls_check,
210
+ "notificationConfig" : self . notification_config,
211
+ "id" : self . id
212
+ } )
213
+ }
214
+ }
215
+ }
216
+ }
217
+
158
218
pub fn call ( & self , context : Context ) {
159
219
trace ! ( "target.call context- {context:?}" ) ;
160
- let timeout = & self . timeout ;
220
+ let timeout = & self . notification_config ;
161
221
let resolves = context. alert_info . alert_state ;
162
222
let mut state = timeout. state . lock ( ) . unwrap ( ) ;
163
223
trace ! ( "target.call state- {state:?}" ) ;
@@ -205,7 +265,7 @@ impl Target {
205
265
let sleep_and_check_if_call =
206
266
move |timeout_state : Arc < Mutex < TimeoutState > > , current_state : AlertState | {
207
267
async move {
208
- tokio:: time:: sleep ( timeout) . await ;
268
+ tokio:: time:: sleep ( Duration :: from_secs ( timeout * 60 ) ) . await ;
209
269
210
270
let mut state = timeout_state. lock ( ) . unwrap ( ) ;
211
271
@@ -276,8 +336,8 @@ fn call_target(target: TargetType, context: Context) {
276
336
}
277
337
278
338
#[ derive( Debug , serde:: Deserialize ) ]
279
- pub struct RepeatVerifier {
280
- interval : Option < String > ,
339
+ pub struct NotificationConfigVerifier {
340
+ interval : Option < u64 > ,
281
341
times : Option < usize > ,
282
342
}
283
343
@@ -288,7 +348,7 @@ pub struct TargetVerifier {
288
348
#[ serde( flatten) ]
289
349
pub target : TargetType ,
290
350
#[ serde( default ) ]
291
- pub repeat : Option < RepeatVerifier > ,
351
+ pub notification_config : Option < NotificationConfigVerifier > ,
292
352
#[ serde( default = "Ulid::new" ) ]
293
353
pub id : Ulid ,
294
354
}
@@ -304,26 +364,22 @@ impl TryFrom<TargetVerifier> for Target {
304
364
timeout. times = Retry :: Infinite
305
365
}
306
366
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 ( ) ) ?;
367
+ if let Some ( notification_config) = value. notification_config {
368
+ let interval = notification_config. interval . map ( |ref interval| * interval) ;
313
369
314
370
if let Some ( interval) = interval {
315
371
timeout. interval = interval
316
372
}
317
373
318
- if let Some ( times) = repeat_config . times {
374
+ if let Some ( times) = notification_config . times {
319
375
timeout. times = Retry :: Finite ( times)
320
376
}
321
377
}
322
378
323
379
Ok ( Target {
324
380
name : value. name ,
325
381
target : value. target ,
326
- timeout,
382
+ notification_config : timeout,
327
383
id : value. id ,
328
384
} )
329
385
}
@@ -518,8 +574,7 @@ impl CallableTarget for AlertManager {
518
574
519
575
#[ derive( Debug , serde:: Serialize , serde:: Deserialize , Clone ) ]
520
576
pub struct Timeout {
521
- #[ serde( with = "humantime_serde" ) ]
522
- pub interval : Duration ,
577
+ pub interval : u64 ,
523
578
#[ serde( default = "Retry::default" ) ]
524
579
pub times : Retry ,
525
580
#[ serde( skip) ]
@@ -529,7 +584,7 @@ pub struct Timeout {
529
584
impl Default for Timeout {
530
585
fn default ( ) -> Self {
531
586
Self {
532
- interval : Duration :: from_secs ( 60 ) ,
587
+ interval : 1 ,
533
588
times : Retry :: default ( ) ,
534
589
state : Arc :: < Mutex < TimeoutState > > :: default ( ) ,
535
590
}
0 commit comments