Skip to content

Commit c31b165

Browse files
committed
Upd with check error write
1 parent 9b8f12a commit c31b165

File tree

4 files changed

+52
-8
lines changed

4 files changed

+52
-8
lines changed

pylibagent/agent.py

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from setproctitle import setproctitle
1313
from .logger import setup_logger
1414
from .check import CheckBase
15+
from .exceptions import CheckException
1516

1617

1718
class 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:

pylibagent/exceptions.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from .severity import Severity
2+
3+
4+
class CheckException(Exception):
5+
"""CheckException is the basic check exception."""
6+
def __init__(self, msg: str, severity: Severity = Severity.MEDIUM):
7+
assert msg, 'CheckException message must not be empty'
8+
self.severity = severity
9+
super().__init__(msg)
10+
11+
def to_dict(self):
12+
return {
13+
"message": self.__str__(),
14+
"severity": self.severity.value
15+
}

pylibagent/severity.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from enum import Enum
2+
3+
4+
class Severity(Enum):
5+
HIGH = 0
6+
MEDIUM = 1
7+
LOW = 2

pylibagent/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '1.0.0'
1+
__version__ = '1.0.1'

0 commit comments

Comments
 (0)