Skip to content
Merged
Changes from 3 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
27 changes: 23 additions & 4 deletions src/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import logging
import os
import sys
import time
from collections.abc import Awaitable, Callable
from collections.abc import AsyncGenerator, Awaitable, Callable
from contextlib import asynccontextmanager
from http import HTTPStatus
from typing import Any

Expand All @@ -16,16 +19,29 @@
from routers.probes import router as probes_router
from services.metrics import CustomMetrics
from utils.exceptions import K8sClientError
from utils.logging import get_logger
from utils.logging import get_logger, reconfigure_logging

logger = get_logger(__name__)
access_logger = get_logger("access")


@asynccontextmanager
async def lifespan(app: FastAPI) -> AsyncGenerator[None]:
"""Lifespan event handler to reconfigure logging after uvicorn starts."""
# Only reconfigure logging when NOT running tests
# During tests, logging is already configured by utils.logging on import
if "pytest" not in sys.modules:
# Reconfigure logging after uvicorn has applied its config
reconfigure_logging()
yield


# Probe endpoints for efficient path checking
PROBE_PATHS = frozenset(["/healthz", "/readyz"])

app = FastAPI(
title="Joule",
lifespan=lifespan,
)


Expand Down Expand Up @@ -189,5 +205,8 @@ async def metrics() -> Response:

if __name__ == "__main__":
# Logging is already configured in utils.logging
# Just run uvicorn without custom log config
uvicorn.run(app, host="0.0.0.0", port=8000)
# Disable uvicorn's log config to use our custom configuration
# Host and port are configurable via environment variables
host = os.getenv("HOST", "0.0.0.0")
port = int(os.getenv("PORT", "8000"))
uvicorn.run(app, host=host, port=port, log_config=None)
Loading