1212from setproctitle import setproctitle
1313from .logger import setup_logger
1414from .check import CheckBase
15+ from .exceptions import CheckException
1516
1617
1718class SendDataException (Exception ):
@@ -140,7 +141,10 @@ async def announce(self, asset_name: Optional[str] = None,
140141 logging .error (f'announce failed: { msg } ' )
141142 exit (1 )
142143
143- async def send_data (self , check_key : str , check_data : dict ,
144+ async def send_data (self ,
145+ check_key : str ,
146+ check_data : Optional [dict ] = None ,
147+ check_error : Optional [CheckException ] = None ,
144148 timestamp : Optional [int ] = None ,
145149 runtime : Optional [float ] = None ):
146150 # The latter strings shouldn't start with a slash. If they start with a
@@ -157,10 +161,15 @@ async def send_data(self, check_key: str, check_data: dict,
157161 timestamp = timestamp or int (time .time ())
158162 data = {
159163 "version" : self .version ,
160- "data" : check_data ,
161164 "timestamp" : timestamp ,
162165 }
163166
167+ if check_data is not None :
168+ data ['data' ] = check_data
169+
170+ if check_error is not None :
171+ data ['error' ] = check_error .to_dict ()
172+
164173 if runtime is not None :
165174 data ["runtime" ] = runtime
166175
@@ -256,11 +265,24 @@ async def _check_loop(self, check: Type[CheckBase]):
256265 if await self ._disabled_checks .is_disabled (self , check .key ):
257266 logging .debug (f'check { check .key } is disabled' )
258267 else :
259- check_data = \
260- await asyncio .wait_for (check .run (), timeout = timeout )
261- await self .send_data (check .key , check_data )
262- except asyncio .TimeoutError :
263- logging .error (f'check error ({ check .key } ): timed out' )
268+ check_data = None
269+ check_error = None
270+ try :
271+ check_data = await asyncio .wait_for (
272+ check .run (),
273+ timeout = timeout )
274+ except asyncio .TimeoutError :
275+ check_error = CheckException ('timed out' )
276+ except CheckException as e :
277+ check_error = e
278+ except Exception as e :
279+ msg = str (e ) or type (e ).__name__
280+ check_error = CheckException (msg )
281+
282+ await self .send_data (check .key , check_data , check_error )
283+ if check_error is not None :
284+ raise check_error
285+
264286 except SendDataException as e :
265287 logging .error (str (e ))
266288 except Exception as e :
0 commit comments