Skip to content

Commit bd6925b

Browse files
committed
imp: error messages
1 parent fa64c66 commit bd6925b

File tree

5 files changed

+54
-13
lines changed

5 files changed

+54
-13
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ Available tags for each message (tags doesn't work only for `no_unhealthy_messag
7777
{url} # requested url
7878
{param} # requested param (look to `to_checks` section)
7979
{status_code} # status_code after request
80+
{error_message} # if request has some error message you can find it over this tag
8081
{is_healthy} # true/fals
8182
{how_long_was_unhealthy} # in min, if url was healthy this tag return 0
8283
{summary} # only for monthly summary string of: f"{url_with_monthly_dead_time.url}: {url_with_monthly_dead_time.unhealthy_this_month} min, efficiency: {(self.AVERAGE_MINUTES_IN_MONTH-url_with_monthly_dead_time.unhealthy_this_month)/self.AVERAGE_MINUTES_IN_MONTH}%\n"

configuration_example.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717
"send_if_there_no_unhealthy": false,
1818
"hello_message": ":wave: Hi, we are connected! Let's go! :tada:",
1919
"healthy_message": "URL {url} is fine :heart:",
20-
"unhealthy_message": "URL {url} is dead :firecracker::skull_and_crossbones::firecracker:",
20+
"unhealthy_message": "URL {url} is dead, status code: {status_code}, error message: {error_message} :firecracker::skull_and_crossbones::firecracker:",
2121
"no_unhealthy_message": "Everything is fine :green_heart:",
2222
"back_to_healthy_message": "URL {url}, back to live! :tada: Total dead time {how_long_was_unhealthy} min",
23-
"still_unhealthy_message": "URL {url}, is still dead :firecracker::skull_and_crossbones::firecracker: Total dead time {how_long_was_unhealthy} min"
23+
"still_unhealthy_message": "URL {url}, is still dead, status code: {status_code}, error message: {error_message} :firecracker::skull_and_crossbones::firecracker: Total dead time {how_long_was_unhealthy} min"
2424
},
2525
"to_checks": [
2626
{

dtos.py

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class HealthResultDTO:
4040
url: str
4141
param: str
4242
is_sent_to_slack: bool = False
43+
error_message: str = ""
4344

4445

4546
@define(kw_only=True)

health_check.py

+48-10
Original file line numberDiff line numberDiff line change
@@ -77,25 +77,63 @@ async def _health_check(
7777
try:
7878
async with aiohttp.ClientSession() as session:
7979
async with session.get(url, timeout=self.config.timeout) as response:
80+
status_code = response.status
8081
health_result = HealthResultDTO(
81-
is_healthy=response.status_code == self.HEALTHY_STATUS_CODE,
82-
status_code=response.status_code,
82+
is_healthy=status_code == self.HEALTHY_STATUS_CODE,
83+
status_code=status_code,
8384
param=param,
8485
url=url,
8586
)
86-
except (
87-
asyncio.TimeoutError,
88-
aiohttp.ClientError,
89-
ConnectTimeout,
90-
ReadTimeout,
91-
):
87+
except asyncio.TimeoutError:
9288
health_result = HealthResultDTO(
9389
is_healthy=False,
94-
status_code=408,
90+
status_code=-1,
9591
param=param,
9692
url=url,
93+
error_message="Client-side timeout",
9794
)
95+
except aiohttp.ClientConnectionError:
96+
health_result = HealthResultDTO(
97+
is_healthy=False,
98+
status_code=-1,
99+
param=param,
100+
url=url,
101+
error_message="Connection error occurred",
102+
)
103+
except aiohttp.ClientResponseError as e:
104+
health_result = HealthResultDTO(
105+
is_healthy=False,
106+
status_code=e.status,
107+
param=param,
108+
url=url,
109+
error_message=str(e.message),
110+
)
111+
except aiohttp.InvalidURL:
112+
health_result = HealthResultDTO(
113+
is_healthy=False,
114+
status_code=-1,
115+
param=param,
116+
url=url,
117+
error_message="Invalid URL",
118+
)
119+
except aiohttp.ClientPayloadError:
120+
health_result = HealthResultDTO(
121+
is_healthy=False,
122+
status_code=-1,
123+
param=param,
124+
url=url,
125+
error_message="Payload error",
126+
)
127+
except aiohttp.ClientError as e:
128+
health_result = HealthResultDTO(
129+
is_healthy=False,
130+
status_code=-1,
131+
param=param,
132+
url=url,
133+
error_message=str(e),
134+
)
135+
98136
logging.info(
99-
f"{datetime.now()} - url: {health_result.url} - param: {health_result.param} - status_code: {health_result.status_code} - is_healthy: {health_result.is_healthy}"
137+
f"{datetime.now()} - url: {health_result.url} - param: {health_result.param} - status_code: {health_result.status_code} - is_healthy: {health_result.is_healthy} - error_message: {health_result.error_message}"
100138
)
101139
return health_result

slack_connector.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def send_health_check_report(self, *, health_check_dto: HealthCheckDTO):
5555
)
5656
if self.config.send_unhealthy:
5757
self._send_results(
58-
health_results=health_check_dto.unhealthy,
58+
health_results=health_check_dto.new_unhealthy,
5959
message=self.config.unhealthy_message or self.DEFAULT_UNHEALTHY_MESSAGE,
6060
)
6161

@@ -106,6 +106,7 @@ def _send_results(
106106
how_long_was_unhealthy=self.repository.get_how_long_was_unhealthy(
107107
url=health_result.url
108108
),
109+
error_message=health_result.error_message,
109110
)
110111
)
111112
health_result.is_sent_to_slack = True

0 commit comments

Comments
 (0)