@@ -277,7 +277,7 @@ def update_current_gain_ref(
277277 db .commit ()
278278
279279
280- # Methods for turning alerts on and off
280+ # Endpoints for turning alerts on and off
281281
282282
283283@router .get ("/silences/{instrument_name}" )
@@ -288,11 +288,18 @@ def get_silences(instrument_name: MurfeyInstrumentName):
288288 return None
289289 machine_config = all_configs [instrument_name ]
290290 alertmanager_url = machine_config .alertmanager_url
291- silences = requests .get (
291+ if alertmanager_url == "" :
292+ logger .warning (f"alertmanager_url not set for { sanitise (instrument_name )} " )
293+ return None
294+ response = requests .get (
292295 f"{ alertmanager_url } /api/v2/silences?filter=microscope={ sanitise (instrument_name )} "
293296 )
297+ if response .status_code != 200 :
298+ logger .warning (
299+ f"Tried to get silences for { sanitise (instrument_name )} , but received status { response .status_code } from alertmanager API"
300+ )
294301 active_silences = []
295- for silence in silences .json ():
302+ for silence in response .json ():
296303 if silence ["status" ]["state" ] == "active" :
297304 active_silences .append (silence )
298305 return active_silences
@@ -302,20 +309,27 @@ def get_silences(instrument_name: MurfeyInstrumentName):
302309def create_silence (instrument_name : MurfeyInstrumentName , end_time : datetime ):
303310 machine_config = machine_info_by_instrument (instrument_name )
304311 alertmanager_url = machine_config .alertmanager_url
312+ if alertmanager_url == "" :
313+ logger .warning (f"alertmanager_url not set for { sanitise (instrument_name )} " )
314+ return None
305315 start_time = datetime .now ().astimezone ().isoformat ()
306316 end_time_str = end_time .astimezone ().isoformat ()
307317 silence_json = {
308318 "matchers" : [
309319 {"name" : "microscope" , "value" : instrument_name , "isRegex" : False }
310320 ],
311321 "createdBy" : "murfey" ,
312- "annotations" : {"description" : "Test " },
322+ "annotations" : {"description" : f"Silence for alerts on { instrument_name } " },
313323 "comment" : "silence created from murfey" ,
314324 "status" : {"state" : "active" },
315325 "startsAt" : str (start_time ),
316326 "endsAt" : str (end_time_str ),
317327 }
318328 response = requests .post (f"{ alertmanager_url } /api/v2/silences" , json = silence_json )
329+ if response .status_code != 200 :
330+ logger .warning (
331+ f"Tried to create silence for { sanitise (instrument_name )} , but received status { response .status_code } from alertmanager API"
332+ )
319333 return JSONResponse (
320334 status_code = response .status_code , content = response .json ()
321335 ) # return a response with same data and code as from alertmanager
@@ -327,12 +341,19 @@ def create_silence(instrument_name: MurfeyInstrumentName, end_time: datetime):
327341def delete_silences (instrument_name : MurfeyInstrumentName ):
328342 machine_config = machine_info_by_instrument (instrument_name )
329343 alertmanager_url = machine_config .alertmanager_url
344+ if alertmanager_url == "" :
345+ logger .warning (f"alertmanager_url not set for { sanitise (instrument_name )} " )
346+ return None
330347 silences = get_silences (instrument_name )
331348 if len (silences ) == 0 :
332349 return None
333350 for silence in silences :
334351 id = silence ["id" ]
335352 response = requests .delete (f"{ alertmanager_url } /api/v2/silence/{ id } " )
353+ if response .status_code != 200 :
354+ logger .warning (
355+ f"Tried to delete silence { id } for { sanitise (instrument_name )} , but received status { response .status_code } from alertmanager API"
356+ )
336357 return response # returns final response in loop
337358
338359
0 commit comments