Skip to content

Commit

Permalink
Rename logger functions to better reflect what they do (especially no…
Browse files Browse the repository at this point in the history
…w that SANDBOX log level does not exist).
  • Loading branch information
craigwalton-dsit committed Jan 3, 2025
1 parent 3aaa67d commit 1ce0891
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/k8s_sandbox/_helm.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
get_current_context_namespace,
k8s_client,
)
from k8s_sandbox._logger import format_log_message, inspect_trace_action, sandbox_log
from k8s_sandbox._logger import format_log_message, inspect_trace_action, log_trace
from k8s_sandbox._pod import Pod

DEFAULT_CHART = Path(__file__).parent / "resources" / "helm" / "agent-env"
Expand Down Expand Up @@ -137,7 +137,7 @@ def _raise_install_error(self, result: ExecResult[str]) -> NoReturn:
r"again",
result.stderr,
):
sandbox_log(
log_trace(
"resourcequota modified error whilst installing helm chart.",
release=self.release_name,
error=result.stderr,
Expand Down
6 changes: 3 additions & 3 deletions src/k8s_sandbox/_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
DEFAULT_ARG_TRUNCATION_THRESHOLD = 1000


def sandbox_log(message: str, **kwargs: Any) -> None:
def log_trace(message: str, **kwargs: Any) -> None:
"""Format and log a message at TRACE level with K8s category.
Args:
Expand All @@ -25,10 +25,10 @@ def sandbox_log(message: str, **kwargs: Any) -> None:
var INSPECT_K8S_LOG_TRUNCATION_THRESHOLD).
"""
formatted = format_log_message(message, **kwargs)
trace_message(logger, "K8s", formatted)
trace_message(logger, category="K8s", message=formatted)


def sandbox_log_error(message: str, **kwargs: Any) -> None:
def log_error(message: str, **kwargs: Any) -> None:
"""Format and log a message at ERROR level with K8s prefix.
Args:
Expand Down
4 changes: 2 additions & 2 deletions src/k8s_sandbox/_pod/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from inspect_ai.util import concurrency

from k8s_sandbox._logger import sandbox_log
from k8s_sandbox._logger import log_trace

T = TypeVar("T")

Expand All @@ -32,7 +32,7 @@ def __init__(self) -> None:
cpu_count = os.cpu_count() or 1
# Pod operations are typically I/O-bound (from the client's perspective).
self._max_workers = cpu_count * 4
sandbox_log("Creating PodOpExecutor.", max_workers=self._max_workers)
log_trace("Creating PodOpExecutor.", max_workers=self._max_workers)
self._executor = ThreadPoolExecutor(
max_workers=self._max_workers, thread_name_prefix="pod-op-executor"
)
Expand Down
10 changes: 5 additions & 5 deletions src/k8s_sandbox/_sandbox_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
from k8s_sandbox._logger import (
format_log_message,
inspect_trace_action,
sandbox_log,
sandbox_log_error,
log_error,
log_trace,
)
from k8s_sandbox._manager import (
HelmReleaseManager,
Expand Down Expand Up @@ -78,7 +78,7 @@ async def get_sandboxes(release: Release) -> dict[str, SandboxEnvironment]:
sandbox_envs: dict[str, SandboxEnvironment] = {}
for key, pod in pods.items():
sandbox_envs[key] = cls(release, pod)
sandbox_log(f"Available sandboxes: {list(sandbox_envs.keys())}")
log_trace(f"Available sandboxes: {list(sandbox_envs.keys())}")
return sandbox_envs

def reorder_default_first(
Expand Down Expand Up @@ -135,7 +135,7 @@ async def exec(
op = "K8s execute command in Pod"
with self._log_op(op, expected_exceptions, **log_kwargs):
result = await self._pod.exec(cmd, input, cwd, env, timeout)
sandbox_log(f"Completed: {op}.", **(log_kwargs | {"result": result}))
log_trace(f"Completed: {op}.", **(log_kwargs | {"result": result}))
return result

async def write_file(self, file: str, contents: str | bytes) -> None:
Expand Down Expand Up @@ -204,7 +204,7 @@ def _log_op(
except Exception as e:
# Whilst Inspect's trace_action will have logged the exception, log it
# at ERROR level here for user visibility.
sandbox_log_error(f"Error during: {op}.", cause=e, **log_kwargs)
log_error(f"Error during: {op}.", cause=e, **log_kwargs)
# Enrich the unexpected exception with additional context.
raise K8sError(f"Error during: {op}.", **log_kwargs) from e

Expand Down

0 comments on commit 1ce0891

Please sign in to comment.