Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions maestro_worker_python/serve.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ async def lifespan(_app: FastAPI) -> AsyncIterator[None]:

if settings.enable_json_logging:
register_client_safe_request_extractor()
# json_logging pins its own logger to DEBUG at import, and init logs at DEBUG before
# the root handler is JSON, leaking a plain-text line that Cloud Logging files as ERROR.
logging.getLogger("json_logging").setLevel(logging.INFO)
json_logging.init_fastapi(enable_json=True)
json_logging.init_request_instrument(app)
json_logging.config_root_logger()
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "maestro-worker-python"
version = "5.2.0"
version = "5.2.1"
description = "Utility to run workers on Moises/Maestro"
readme = "README.md"
requires-python = ">=3.10,<3.14"
Expand Down
35 changes: 35 additions & 0 deletions tests/test_serve.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import asyncio
import importlib
import json
import subprocess
import sys
import threading
from pathlib import Path
Expand Down Expand Up @@ -341,3 +343,36 @@ def test_a_recoverable_failure_keeps_the_process_serving(tmp_path, monkeypatch,
assert client.post("/inference", json={}).status_code == status_code
assert serve_module.model.calls == 2
assert terminations == []


def _start_with_json_logging(tmp_path, then: str = "") -> subprocess.CompletedProcess:
"""Import serve.py in a fresh interpreter: json_logging configures process-wide state once."""
worker_path = tmp_path / "worker.py"
worker_path.write_text("class MoisesWorker:\n pass\n")
return subprocess.run(
[sys.executable, "-c", f"from maestro_worker_python import serve\n{then}"],
capture_output=True,
text=True,
env={"ENABLE_JSON_LOGGING": "true", "MODEL_PATH": str(worker_path)},
)


def _log_lines(result: subprocess.CompletedProcess) -> list[str]:
return [line for line in (result.stdout + result.stderr).splitlines() if line.strip()]


def test_json_logging_startup_writes_only_json(tmp_path):
# Cloud Logging files any non-JSON stderr line as ERROR.
result = _start_with_json_logging(tmp_path)

assert result.returncode == 0, result.stderr
for line in _log_lines(result):
json.loads(line)


def test_json_logging_keeps_reporting_its_own_warnings(tmp_path):
result = _start_with_json_logging(tmp_path, 'import logging; logging.getLogger("json_logging").warning("probe")')

assert result.returncode == 0, result.stderr
records = [json.loads(line) for line in _log_lines(result) if line.startswith("{")]
assert any(r["msg"] == "probe" and r["level"] == "WARNING" for r in records), records
2 changes: 1 addition & 1 deletion uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading