Skip to content

Commit 1808320

Browse files
authored
Merge pull request #22 from MannLabs/improve_webapp_logs
feat: enhance logging for webapp and configure volume for log storage
2 parents 558db85 + b5fc439 commit 1808320

File tree

5 files changed

+29
-14
lines changed

5 files changed

+29
-14
lines changed

airflow_src/plugins/sensors/ssh_utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def ssh_execute(
1414
command: str,
1515
ssh_hook: SSHHook | None = None,
1616
*,
17-
max_tries: int = 10,
17+
max_tries: int = 30,
1818
) -> str:
1919
"""Execute the given `command` via the `ssh_hook`.
2020
@@ -41,7 +41,7 @@ def ssh_execute(
4141
logging.error(f"Execution of SSH command failed too often: {command=}")
4242
raise AirflowFailException("Execution of SSH command failed too often.")
4343

44-
sleep(30 * call_count) # no sleep in the first iteration
44+
sleep(60 * call_count) # no sleep in the first iteration
4545
call_count += 1
4646

4747
try:

airflow_src/tests/plugins/sensors/test_ssh_utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,11 @@ def test_ssh_execute_too_many_tries(mock_sleep: MagicMock) -> None:
6565
# given
6666
ssh_hook = MagicMock()
6767
bad_return = (254, b"command output", b"")
68-
ssh_hook.exec_ssh_client_command.side_effect = [bad_return] * 11
68+
ssh_hook.exec_ssh_client_command.side_effect = [bad_return] * 31
6969

7070
# when
7171
with pytest.raises(AirflowFailException):
7272
ssh_execute("my_command", ssh_hook)
7373

74-
assert mock_sleep.call_count == 10 # noqa: PLR2004
75-
assert ssh_hook.exec_ssh_client_command.call_count == 10 # noqa: PLR2004
74+
assert mock_sleep.call_count == 30 # noqa: PLR2004
75+
assert ssh_hook.exec_ssh_client_command.call_count == 30 # noqa: PLR2004

docker-compose.yaml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -319,8 +319,9 @@ services:
319319
retries: 5
320320
start_period: 30s
321321
restart: always
322-
# # Uncomment for local development so that changes to webapp are immediately active
323-
# volumes:
322+
volumes:
323+
- ./webapp_logs:/app/logs
324+
# Uncomment for local development so that changes to webapp are immediately active
324325
# - ./webapp:/app/webapp
325326
# - ./shared:/app/webapp/shared
326327

webapp/conftest.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
"""Test configuration for webapp tests."""
2+
3+
import os
4+
5+
os.environ["IS_PYTEST_RUN"] = "1" # Indicate that tests are running

webapp/service/utils.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
"""Utilities for the streamlit app."""
22

33
import io
4+
import logging
45
import os
56
import traceback
6-
from datetime import datetime
77
from pathlib import Path
88

99
import plotly.graph_objects as go
10-
import pytz
1110
import streamlit as st
1211
from PIL import Image
1312

@@ -47,20 +46,30 @@ class QueryParams:
4746
DEFAULT_MAX_AGE_OVERVIEW = 30 # days
4847
DEFAULT_MAX_AGE_STATUS = 90 # days
4948

49+
# Configure logging
50+
logging.basicConfig(
51+
level=logging.INFO,
52+
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
53+
handlers=[
54+
logging.FileHandler("/app/logs/webapp.log")
55+
if os.getenv("IS_PYTEST_RUN", "0") != "1"
56+
else None,
57+
logging.StreamHandler(), # Keep console output for debugging
58+
],
59+
)
60+
logger = logging.getLogger(__name__)
61+
5062

5163
def _log(item_to_log: str | Exception, extra_msg: str = "") -> None:
5264
"""Write a log message and show it if it's an exception."""
53-
now = datetime.now(tz=pytz.UTC).strftime("%Y-%m-%d %H:%M:%S.%f")
54-
5565
if isinstance(item_to_log, Exception):
5666
if extra_msg:
5767
st.write(extra_msg)
5868
st.write(item_to_log)
5969
msg = f"{extra_msg}{item_to_log}\n{traceback.format_exc()}"
70+
logger.error(msg, exc_info=True)
6071
else:
61-
msg = f"{now}: {item_to_log}"
62-
63-
os.write(1, f"{msg}\n".encode())
72+
logger.info(item_to_log)
6473

6574

6675
def empty_to_none(value: str) -> str | None:

0 commit comments

Comments
 (0)