12
12
from setproctitle import setproctitle
13
13
from .logger import setup_logger
14
14
from .check import CheckBase
15
+ from .exceptions import CheckException
15
16
16
17
17
18
class SendDataException (Exception ):
@@ -140,7 +141,10 @@ async def announce(self, asset_name: Optional[str] = None,
140
141
logging .error (f'announce failed: { msg } ' )
141
142
exit (1 )
142
143
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 ,
144
148
timestamp : Optional [int ] = None ,
145
149
runtime : Optional [float ] = None ):
146
150
# 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,
157
161
timestamp = timestamp or int (time .time ())
158
162
data = {
159
163
"version" : self .version ,
160
- "data" : check_data ,
161
164
"timestamp" : timestamp ,
162
165
}
163
166
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
+
164
173
if runtime is not None :
165
174
data ["runtime" ] = runtime
166
175
@@ -256,11 +265,24 @@ async def _check_loop(self, check: Type[CheckBase]):
256
265
if await self ._disabled_checks .is_disabled (self , check .key ):
257
266
logging .debug (f'check { check .key } is disabled' )
258
267
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
+
264
286
except SendDataException as e :
265
287
logging .error (str (e ))
266
288
except Exception as e :
0 commit comments