feat: expose recommendation streaming api
This commit is contained in:
parent
e8d20158e3
commit
41aa93c3c7
7 changed files with 473 additions and 12 deletions
30
backend/app/observability/logging.py
Normal file
30
backend/app/observability/logging.py
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
"""Configure structlog for machine-readable live logs and readable development logs."""
|
||||
|
||||
import logging
|
||||
|
||||
import structlog
|
||||
|
||||
from app.config import AppMode
|
||||
|
||||
|
||||
def configure_logging(app_mode: AppMode) -> None:
|
||||
"""Install the process logging pipeline for the selected runtime mode."""
|
||||
logging.basicConfig(level=logging.INFO, format="%(message)s")
|
||||
renderer: structlog.types.Processor
|
||||
if app_mode is AppMode.LIVE:
|
||||
renderer = structlog.processors.JSONRenderer()
|
||||
else:
|
||||
renderer = structlog.dev.ConsoleRenderer(colors=False)
|
||||
structlog.configure(
|
||||
processors=[
|
||||
structlog.contextvars.merge_contextvars,
|
||||
structlog.processors.add_log_level,
|
||||
structlog.processors.TimeStamper(fmt="iso", utc=True),
|
||||
structlog.processors.StackInfoRenderer(),
|
||||
structlog.processors.format_exc_info,
|
||||
renderer,
|
||||
],
|
||||
wrapper_class=structlog.make_filtering_bound_logger(logging.INFO),
|
||||
logger_factory=structlog.PrintLoggerFactory(),
|
||||
cache_logger_on_first_use=True,
|
||||
)
|
||||
|
|
@ -8,6 +8,7 @@ from typing import cast
|
|||
|
||||
import structlog
|
||||
from starlette.types import ASGIApp, Receive, Scope, Send
|
||||
from structlog.contextvars import bind_contextvars, reset_contextvars
|
||||
|
||||
|
||||
@dataclass
|
||||
|
|
@ -71,10 +72,12 @@ class RequestTimingMiddleware:
|
|||
counters = RequestCounters()
|
||||
counter_token = _COUNTERS.set(counters)
|
||||
request_token = _REQUEST_ID.set(request_id)
|
||||
logging_tokens = bind_contextvars(request_id=request_id)
|
||||
try:
|
||||
await self.app(scope, receive, send)
|
||||
finally:
|
||||
self._log_completion(scope, request_id, started_at, counters)
|
||||
reset_contextvars(**logging_tokens)
|
||||
_COUNTERS.reset(counter_token)
|
||||
_REQUEST_ID.reset(request_token)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue