@@ -27,10 +27,10 @@ use base64::Engine;
2727use bytes:: Bytes ;
2828use chrono:: Utc ;
2929use http:: { header:: AUTHORIZATION , HeaderMap , HeaderValue } ;
30- use humantime_serde:: re:: humantime;
3130use itertools:: Itertools ;
3231use once_cell:: sync:: Lazy ;
3332use reqwest:: ClientBuilder ;
33+ use serde_json:: { json, Value } ;
3434use tokio:: sync:: RwLock ;
3535use tracing:: { error, trace, warn} ;
3636use ulid:: Ulid ;
@@ -66,14 +66,6 @@ impl TargetConfigs {
6666
6767 pub async fn update ( & self , target : Target ) -> Result < ( ) , AlertError > {
6868 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- }
7769 map. insert ( target. id , target. clone ( ) ) ;
7870
7971 let path = target_json_path ( & target. id ) ;
@@ -148,16 +140,84 @@ pub struct Target {
148140 pub name : String ,
149141 #[ serde( flatten) ]
150142 pub target : TargetType ,
151- #[ serde( default , rename = "repeat" ) ]
152- pub timeout : Timeout ,
143+ pub notification_config : Timeout ,
153144 #[ serde( default = "Ulid::new" ) ]
154145 pub id : Ulid ,
155146}
156147
157148impl 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+
158218 pub fn call ( & self , context : Context ) {
159219 trace ! ( "target.call context- {context:?}" ) ;
160- let timeout = & self . timeout ;
220+ let timeout = & self . notification_config ;
161221 let resolves = context. alert_info . alert_state ;
162222 let mut state = timeout. state . lock ( ) . unwrap ( ) ;
163223 trace ! ( "target.call state- {state:?}" ) ;
@@ -205,7 +265,7 @@ impl Target {
205265 let sleep_and_check_if_call =
206266 move |timeout_state : Arc < Mutex < TimeoutState > > , current_state : AlertState | {
207267 async move {
208- tokio:: time:: sleep ( timeout) . await ;
268+ tokio:: time:: sleep ( Duration :: from_secs ( timeout * 60 ) ) . await ;
209269
210270 let mut state = timeout_state. lock ( ) . unwrap ( ) ;
211271
@@ -276,8 +336,8 @@ fn call_target(target: TargetType, context: Context) {
276336}
277337
278338#[ derive( Debug , serde:: Deserialize ) ]
279- pub struct RepeatVerifier {
280- interval : Option < String > ,
339+ pub struct NotificationConfigVerifier {
340+ interval : Option < u64 > ,
281341 times : Option < usize > ,
282342}
283343
@@ -288,7 +348,7 @@ pub struct TargetVerifier {
288348 #[ serde( flatten) ]
289349 pub target : TargetType ,
290350 #[ serde( default ) ]
291- pub repeat : Option < RepeatVerifier > ,
351+ pub notification_config : Option < NotificationConfigVerifier > ,
292352 #[ serde( default = "Ulid::new" ) ]
293353 pub id : Ulid ,
294354}
@@ -304,26 +364,22 @@ impl TryFrom<TargetVerifier> for Target {
304364 timeout. times = Retry :: Infinite
305365 }
306366
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) ;
313369
314370 if let Some ( interval) = interval {
315371 timeout. interval = interval
316372 }
317373
318- if let Some ( times) = repeat_config . times {
374+ if let Some ( times) = notification_config . times {
319375 timeout. times = Retry :: Finite ( times)
320376 }
321377 }
322378
323379 Ok ( Target {
324380 name : value. name ,
325381 target : value. target ,
326- timeout,
382+ notification_config : timeout,
327383 id : value. id ,
328384 } )
329385 }
@@ -518,8 +574,7 @@ impl CallableTarget for AlertManager {
518574
519575#[ derive( Debug , serde:: Serialize , serde:: Deserialize , Clone ) ]
520576pub struct Timeout {
521- #[ serde( with = "humantime_serde" ) ]
522- pub interval : Duration ,
577+ pub interval : u64 ,
523578 #[ serde( default = "Retry::default" ) ]
524579 pub times : Retry ,
525580 #[ serde( skip) ]
@@ -529,7 +584,7 @@ pub struct Timeout {
529584impl Default for Timeout {
530585 fn default ( ) -> Self {
531586 Self {
532- interval : Duration :: from_secs ( 60 ) ,
587+ interval : 1 ,
533588 times : Retry :: default ( ) ,
534589 state : Arc :: < Mutex < TimeoutState > > :: default ( ) ,
535590 }
0 commit comments