forked from mtusi2026/mtusi2026lab2
Name the serving replica in every response
The platform settings default the instance id to the container hostname, computed once per process, so every gunicorn worker of a replica reports the same value and it matches docker ps. The request pipeline stamps X-Instance-Id next to X-Request-Id on every response it sends, and the unhandled error handler adds it itself, because the 500 answer is produced outside the pipeline. Own tests cover 200, 204, 400, 401, 404, the hidden 405, 413 and 500 with a fixed instance id, and the hostname default. The application version follows the contract version 1.3.0. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
9db7a24362
commit
bf0f0b4c07
@@ -10,6 +10,7 @@ from messenger.shared.domain.errors import DomainError
|
||||
from messenger.shared.entrypoints.schemas import ErrorEnvelope
|
||||
|
||||
REQUEST_ID_HEADER = "X-Request-Id"
|
||||
INSTANCE_ID_HEADER = "X-Instance-Id"
|
||||
|
||||
type ErrorCodes = Mapping[type[DomainError], tuple[str, int]]
|
||||
type Challenge = Callable[[str], str]
|
||||
@@ -56,10 +57,12 @@ def route_of(request: Request) -> str:
|
||||
return getattr(route, "path", request.url.path)
|
||||
|
||||
|
||||
def install(app: FastAPI, logger: Logger, codes: ErrorCodes, challenge: Challenge) -> None:
|
||||
def install(
|
||||
app: FastAPI, logger: Logger, codes: ErrorCodes, challenge: Challenge, instance: str
|
||||
) -> None:
|
||||
def error_response(request: Request, code: str, detail: str, http_status: int) -> Response:
|
||||
request_id = getattr(request.state, "request_id", "") or "unknown"
|
||||
headers = {REQUEST_ID_HEADER: request_id}
|
||||
headers = {REQUEST_ID_HEADER: request_id, INSTANCE_ID_HEADER: instance}
|
||||
if http_status == status.HTTP_401_UNAUTHORIZED:
|
||||
headers["WWW-Authenticate"] = challenge(request.url.path)
|
||||
return Response(
|
||||
|
||||
Reference in New Issue
Block a user