forked from mtusi2026/mtusi2026lab6
Переиспользован стек lab5. API/worker шлют OTLP (traces/metrics/logs) в otel-collector -> Prometheus/Loki/Tempo; Grafana с provisioning датасорсов, дашборда и alert rules в Git. W3C trace context переносится через поле traceparent в задании очереди (API producer span -> worker consumer span в одном trace, подтверждено прямым запросом к Tempo). Три контролируемых инцидента прогнаны и задокументированы (worker down -> queue lag + alert, DB down отличим от медленного handler по error-span, telemetry backend down не роняет API/Collector). В процессе найдены и исправлены 4 реальных бага (middleware после старта приложения, detached span context, замерзающая метрика очереди при падении её единственного источника, неатомарный XACK+XDEL) — см. REPORT.md и evidence/. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
38 lines
1.0 KiB
Python
38 lines
1.0 KiB
Python
"""Canonical error envelope: {"error": {"code", "message", "request_id"}}."""
|
|
|
|
|
|
class ApiError(Exception):
|
|
def __init__(self, status: int, code: str, message: str):
|
|
self.status = status
|
|
self.code = code
|
|
self.message = message
|
|
super().__init__(message)
|
|
|
|
|
|
def bad_request(message: str) -> ApiError:
|
|
return ApiError(400, "invalid_request", message)
|
|
|
|
|
|
def unauthorized(message: str = "authentication required") -> ApiError:
|
|
return ApiError(401, "unauthorized", message)
|
|
|
|
|
|
def not_found(message: str = "not found") -> ApiError:
|
|
return ApiError(404, "not_found", message)
|
|
|
|
|
|
def conflict(message: str) -> ApiError:
|
|
return ApiError(409, "conflict", message)
|
|
|
|
|
|
def unavailable(message: str = "dependency unavailable") -> ApiError:
|
|
return ApiError(503, "unavailable", message)
|
|
|
|
|
|
def payload_too_large(message: str) -> ApiError:
|
|
return ApiError(413, "payload_too_large", message)
|
|
|
|
|
|
def unsupported_media_type(message: str) -> ApiError:
|
|
return ApiError(415, "unsupported_media_type", message)
|