feat(observability): backfill logging for upload, auth, and domain services
Why: - resolve_auth_context() runs on every authenticated request and logged nothing; four distinct rejection reasons (malformed/unknown/inactive/ expired key, inactive tenant) were all invisible. - The domain-allowlist rejection in upload_source_file() happens before any ingestion_jobs row exists, so it wasn't covered by the job-level ingestion.job.failed event either -- a rejected upload left zero trace. - Four of upload_source_file()'s five failure branches (parse_failed, chunk_limit_exceeded, embedding_failed, index_failed) called _mark_job_failed(), which wrote to Postgres but never logged; only storage_failed and timeout had an ad-hoc logger.warning duplicated at their own call sites. Changes: - auth/service.py: auth.succeeded / auth.failed (with a reason field per rejection type), matching ADR-0011's own event catalog. - domains/service.py: domain.rejected on the allowlist check; domain.created / domain.updated / domain.status_changed on the three mutations. - files/upload.py: centralized failure logging inside _mark_job_failed (every failure branch already calls it, so logging there once closes all five branches instead of duplicating a log call at each site) as ingestion.job.failed; added ingestion.job.started; renamed the ad-hoc files.upload.succeeded to ingestion.job.completed for catalog consistency. Impact: - None to request/response behavior -- log events only.
This commit is contained in:
@@ -68,6 +68,16 @@ async def _mark_job_failed(
|
||||
error_code: str,
|
||||
error_message: str,
|
||||
) -> None:
|
||||
"""Write the terminal `failed` job row and emit its log event together.
|
||||
|
||||
Every failure branch below calls this, so logging here once closes every
|
||||
branch at once rather than duplicating a `logger.warning` at each call
|
||||
site (CLAUDE.md, "prefer deep modules") -- previously only
|
||||
`storage_upload_failed` and `timeout` did that ad hoc, and
|
||||
`parse_failed`/`chunk_limit_exceeded`/`embedding_failed`/`index_failed`
|
||||
logged nothing at all: visible in `ingestion_job_events` but invisible to
|
||||
log-based alerting (ADR-0011).
|
||||
"""
|
||||
async with sessionmaker() as session:
|
||||
job = await jobs_repo.mark_terminal(
|
||||
session,
|
||||
@@ -88,6 +98,14 @@ async def _mark_job_failed(
|
||||
)
|
||||
await session.commit()
|
||||
|
||||
logger.warning(
|
||||
"ingestion.job.failed",
|
||||
tenant_id=str(tenant_id),
|
||||
ingestion_job_id=str(ingestion_job_id),
|
||||
error_code=error_code,
|
||||
error_message=error_message,
|
||||
)
|
||||
|
||||
|
||||
async def upload_source_file(
|
||||
*,
|
||||
@@ -191,6 +209,15 @@ async def upload_source_file(
|
||||
await session.commit()
|
||||
ingestion_job_id = job.id
|
||||
|
||||
logger.info(
|
||||
"ingestion.job.started",
|
||||
tenant_id=str(auth.tenant_id),
|
||||
ingestion_job_id=str(ingestion_job_id),
|
||||
file_id=str(source_file_id),
|
||||
domain=domain,
|
||||
source_type=validated.source_type,
|
||||
)
|
||||
|
||||
# Phase 2: no Postgres session open across this work (ADR-0017),
|
||||
# bounded end-to-end by INGESTION_TIMEOUT_SECONDS.
|
||||
try:
|
||||
@@ -200,12 +227,6 @@ async def upload_source_file(
|
||||
key=object_key, data=data, content_type=validated.content_type
|
||||
)
|
||||
except Exception as exc:
|
||||
logger.warning(
|
||||
"files.upload.storage_failed",
|
||||
tenant_id=str(auth.tenant_id),
|
||||
file_id=str(source_file_id),
|
||||
ingestion_job_id=str(ingestion_job_id),
|
||||
)
|
||||
await _mark_job_failed(
|
||||
sessionmaker,
|
||||
tenant_id=auth.tenant_id,
|
||||
@@ -288,12 +309,6 @@ async def upload_source_file(
|
||||
)
|
||||
raise
|
||||
except TimeoutError:
|
||||
logger.warning(
|
||||
"files.upload.timeout",
|
||||
tenant_id=str(auth.tenant_id),
|
||||
file_id=str(source_file_id),
|
||||
ingestion_job_id=str(ingestion_job_id),
|
||||
)
|
||||
await _mark_job_failed(
|
||||
sessionmaker,
|
||||
tenant_id=auth.tenant_id,
|
||||
@@ -334,11 +349,13 @@ async def upload_source_file(
|
||||
await session.commit()
|
||||
|
||||
logger.info(
|
||||
"files.upload.succeeded",
|
||||
"ingestion.job.completed",
|
||||
tenant_id=str(auth.tenant_id),
|
||||
file_id=str(source_file_id),
|
||||
ingestion_job_id=str(ingestion_job_id),
|
||||
points_indexed=indexed.points_upserted,
|
||||
file_id=str(source_file_id),
|
||||
chunks_parsed=len(chunks),
|
||||
points_upserted=indexed.points_upserted,
|
||||
points_soft_deleted=indexed.points_soft_deleted,
|
||||
)
|
||||
return UploadResult(
|
||||
file_id=source_file_id,
|
||||
|
||||
Reference in New Issue
Block a user