-
Notifications
You must be signed in to change notification settings - Fork 1
Docker orchestration logging #38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
390eb9c
refactor of logger and docker orch logging
Hevagog dc36840
refactor and tmux addition
Hevagog 7b6c6f8
terminal text partial fix
Hevagog a5fcd8f
fix formatting and simplify terminal
Hevagog d4e4c71
get rid of docker dependency
Hevagog b18a437
install xterm in release
Hevagog 784d4d6
fix old empty test.log file
Hevagog 938e08c
Merge branch 'main' into logging-sim-service
Hevagog 3cec27a
ruff refactor & coverage report fix
Hevagog File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| from logger.numeric_logger import get_csv_logger | ||
| from logger.orchestration_logger import log_docker_logs | ||
| from logger.u_logger import configure_logger, get_logger | ||
|
|
||
| __all__ = [ | ||
| "get_csv_logger", | ||
| "configure_logger", | ||
| "get_logger", | ||
| "log_docker_logs", | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| import subprocess | ||
| from logging import Logger | ||
| from pathlib import Path | ||
| from typing import List | ||
|
|
||
| from logger.utils import get_external_console_logging | ||
|
|
||
| TAIL_LINES = 100 | ||
| WORKER_PREFIX = "worker" | ||
| SIM_SERVICE_NAME = "simulation_server" | ||
|
|
||
|
|
||
| def get_services() -> List[str]: | ||
| """Return a list of all docker compose service names.""" | ||
| try: | ||
| container_ids = subprocess.check_output( | ||
| ["docker", "ps", "-q"], text=True | ||
| ).splitlines() | ||
|
|
||
| services = set() | ||
| for cid in container_ids: | ||
| inspect_output = subprocess.check_output( | ||
| [ | ||
| "docker", | ||
| "inspect", | ||
| "--format", | ||
| '{{ index .Config.Labels "com.docker.compose.service" }}', | ||
| cid, | ||
| ], | ||
| text=True, | ||
| ).strip() | ||
| if inspect_output and inspect_output != "<no value>": | ||
| services.add(inspect_output) | ||
| return list(services) | ||
| except Exception as e: | ||
| raise RuntimeError( | ||
| "Failed to get Docker services. Ensure Docker is running and you have access to it." | ||
| ) from e | ||
|
|
||
|
|
||
| def _start_external_xterm_log_terminal(title: str, command: str) -> None: | ||
| """Start an xterm window with the specified log command.""" | ||
| subprocess.Popen(["xterm", "-hold", "-T", title, "-e", "bash", "-c", command]) | ||
|
|
||
|
|
||
| def _start_external_log_terminal(title: str, command: str) -> None: | ||
| """Start a tmux session with the specified log command.""" | ||
|
|
||
| session_name = f"log_{title.replace(' ', '_').lower()}" | ||
|
|
||
| try: | ||
| escaped_command = command.replace('"', '\\"') | ||
| wt_command = ( | ||
| f'wt.exe new-tab --title "{title}" wsl.exe bash -c "{escaped_command}"' | ||
| ) | ||
| subprocess.run(wt_command, shell=True) | ||
| except (OSError, subprocess.SubprocessError): | ||
| _start_external_xterm_log_terminal( | ||
| title, f"tmux attach-session -t {session_name}" | ||
| ) | ||
|
|
||
|
|
||
| def _start_file_logging(command: str) -> None: | ||
| """Start a background process that logs output to a file.""" | ||
| buffered_command = f"stdbuf -oL -eL {command}" | ||
| subprocess.Popen( | ||
| ["bash", "-c", buffered_command], | ||
| stdout=subprocess.DEVNULL, | ||
| stderr=subprocess.DEVNULL, | ||
| ) | ||
|
|
||
|
|
||
| def log_docker_logs(logger: Logger) -> None: | ||
| """Fetch and log docker logs for workers and simulation service.""" | ||
| assert logger is not None, "Logger must be initialized before logging Docker logs." | ||
|
|
||
| project_root = Path(__file__).resolve().parents[2] | ||
| log_dir = project_root / "log" | ||
| log_dir.mkdir(exist_ok=True) | ||
|
|
||
| try: | ||
| services = get_services() | ||
| except RuntimeError as e: | ||
| logger.error(f"Error fetching Docker services: {e}") | ||
| return | ||
|
|
||
| logger.info(f"Found services: {services}") | ||
| if not services: | ||
| logger.warning("No services found. Ensure Docker Compose is running.") | ||
| return | ||
|
|
||
| worker_services = sorted([s for s in services if s.startswith(WORKER_PREFIX)]) | ||
|
|
||
| worker_log_path = (log_dir / "docker_workers.log").resolve() | ||
| sim_log_path = (log_dir / "docker_simulation_server.log").resolve() | ||
|
|
||
| try: | ||
| for session in subprocess.check_output( | ||
| ["tmux", "list-sessions"], text=True, stderr=subprocess.DEVNULL | ||
| ).splitlines(): | ||
| if session.startswith("log_"): | ||
| session_name = session.split(":")[0] | ||
| logger.info(f"Killing existing tmux session: {session_name}") | ||
| subprocess.Popen(["tmux", "kill-session", "-t", session_name]) | ||
| except subprocess.CalledProcessError: | ||
| pass | ||
|
|
||
| if worker_services: | ||
| worker_cmd = f"docker compose logs --tail {TAIL_LINES} --follow {' '.join(worker_services)}" | ||
|
|
||
| if get_external_console_logging(): | ||
| _start_external_log_terminal( | ||
| "Docker Worker Logs", | ||
| f"{worker_cmd} | stdbuf -oL -eL tee '{worker_log_path}'", | ||
| ) | ||
| logger.info( | ||
| f"Opened external terminal for worker logs (also logging to {worker_log_path})." | ||
| ) | ||
|
|
||
| _start_file_logging(f"{worker_cmd} > '{worker_log_path}'") | ||
| logger.info(f"Logging worker logs to file: {worker_log_path}") | ||
|
|
||
| sim_cmd = f"docker logs --tail {TAIL_LINES} --follow {SIM_SERVICE_NAME}" | ||
|
|
||
| if get_external_console_logging(): | ||
| _start_external_log_terminal( | ||
| "Docker Simulation Service Logs", | ||
| f"{sim_cmd} | stdbuf -oL -eL tee '{sim_log_path}'", | ||
| ) | ||
| logger.info( | ||
| f"Opened external terminal for simulation logs (also logging to {sim_log_path})." | ||
| ) | ||
|
|
||
| _start_file_logging(f"{sim_cmd} > '{sim_log_path}'") | ||
| logger.info(f"Logging simulation logs to file: {sim_log_path}") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| import os | ||
| from typing import Any, Dict | ||
|
|
||
| import tomli | ||
|
|
||
|
|
||
| def get_log_to_console_value() -> bool: | ||
| """ | ||
| Get the value of log_to_console from pyproject.toml. | ||
|
|
||
| Returns | ||
| ------- | ||
| bool | ||
| The value of log_to_console. | ||
|
|
||
| """ | ||
| return bool(get_logging_output()["log_to_console"]) | ||
|
|
||
|
|
||
| def log_to_datetime_log_file() -> bool: | ||
| """ | ||
| Get the value of datetime_log_file from pyproject.toml. | ||
|
|
||
| Returns | ||
| ------- | ||
| bool | ||
| The value of datetime_log_file. | ||
|
|
||
| """ | ||
| return bool(get_logging_output()["datetime_log_file"]) | ||
|
|
||
|
|
||
| def get_logging_output() -> Dict[str, Any]: | ||
| """ | ||
| Get the logging output configuration from pyproject.toml. | ||
|
|
||
| Returns | ||
| ------- | ||
| Dict[str, Any] | ||
| The logging output configuration. | ||
|
|
||
| """ | ||
| pyproject_toml_path = os.path.join( | ||
| os.path.abspath(os.path.join(os.path.dirname(__file__), "../../pyproject.toml")) | ||
| ) | ||
| with open(pyproject_toml_path, "rb") as f: | ||
| pyproject_toml = tomli.load(f) | ||
| return dict(pyproject_toml["logging"]["output"]) | ||
|
|
||
|
|
||
| def get_external_console_logging() -> bool: | ||
| """ | ||
| Get the value of external_docker_log_console from pyproject.toml. | ||
|
|
||
| Returns | ||
| ------- | ||
| bool | ||
| The value of external_docker_log_console. | ||
| """ | ||
| return bool(get_logging_output()["external_docker_log_console"]) |
3 changes: 1 addition & 2 deletions
3
src/orchestration/risk_management_service/core/service/risk_management_service.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.