Update observability docs and task utilities
- Add Observability.md documentation - Standardize task logging with correlation_id support - Add log_sanitizer utility for PII masking - Update Tasks.md tracking - Update geo_cache tasks and other task modules with correlation_id Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -8,15 +8,20 @@ consuming excessive memory.
|
||||
The cleanup is conservative: it only removes IPs with no recent attempts
|
||||
(all timestamps outside the rate-limit window), so active or recently-active
|
||||
IPs are preserved.
|
||||
|
||||
Correlation IDs are propagated through the task using :mod:`app.utils.correlation`
|
||||
so that task logs can be correlated across runs.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import uuid
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
import structlog
|
||||
|
||||
from app.tasks.timeout_utils import run_with_timeout
|
||||
from app.utils.correlation import get_correlation_id, reset_correlation_id, set_correlation_id
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from fastapi import FastAPI
|
||||
@@ -35,7 +40,10 @@ JOB_ID: str = "rate_limiter_cleanup"
|
||||
TASK_TIMEOUT_SECONDS: int = 5
|
||||
|
||||
|
||||
async def _run_cleanup(app: FastAPI) -> None:
|
||||
async def _run_cleanup(
|
||||
app: FastAPI,
|
||||
correlation_id: str | None = None,
|
||||
) -> None:
|
||||
"""Trigger cleanup of expired rate-limiter entries.
|
||||
|
||||
Cleans up both the login-specific rate limiter (exponential backoff)
|
||||
@@ -43,13 +51,27 @@ async def _run_cleanup(app: FastAPI) -> None:
|
||||
|
||||
Args:
|
||||
app: The FastAPI application instance (holds the rate limiters).
|
||||
correlation_id: Optional correlation ID for log correlation.
|
||||
"""
|
||||
if correlation_id is None:
|
||||
correlation_id = str(uuid.uuid4())
|
||||
|
||||
token = set_correlation_id(correlation_id)
|
||||
try:
|
||||
await _do_cleanup_with_app(app)
|
||||
finally:
|
||||
reset_correlation_id(token)
|
||||
|
||||
|
||||
async def _do_cleanup_with_app(app: FastAPI) -> None:
|
||||
"""Inner cleanup logic that runs with correlation context set."""
|
||||
|
||||
async def _do_cleanup() -> None:
|
||||
login_limiter = getattr(app.state, "login_rate_limiter", None)
|
||||
if login_limiter is None:
|
||||
log.warning(
|
||||
"rate_limiter_cleanup_skipped",
|
||||
correlation_id=get_correlation_id(),
|
||||
reason="login_rate_limiter not found on app.state",
|
||||
)
|
||||
else:
|
||||
@@ -59,6 +81,7 @@ async def _run_cleanup(app: FastAPI) -> None:
|
||||
if global_limiter is None:
|
||||
log.warning(
|
||||
"rate_limiter_cleanup_skipped",
|
||||
correlation_id=get_correlation_id(),
|
||||
reason="global_rate_limiter not found on app.state",
|
||||
)
|
||||
else:
|
||||
|
||||
Reference in New Issue
Block a user