feat(api): tag each error with a ref and count errors by service

Every structured error carries a short ref, appended to its message and logged with the request; /api/dashboard/status gains per-service error counts. Gzip now compresses in place.
This commit is contained in:
imSp4rky
2026-10-21 14:39:37 -06:00
parent 39ec591c48
commit afaff8e070
9 changed files with 107 additions and 29 deletions
+3 -9
View File
@@ -49,12 +49,6 @@ async def compression_middleware(request: web.Request, handler) -> web.StreamRes
if not level:
return response
compressed = gzip.compress(body, compresslevel=level)
headers = dict(response.headers)
headers["Content-Encoding"] = "gzip"
headers["Content-Length"] = str(len(compressed))
return web.Response(
body=compressed,
status=response.status,
headers=headers,
)
response.body = gzip.compress(body, compresslevel=level)
response.headers["Content-Encoding"] = "gzip"
return response
+10 -2
View File
@@ -7,6 +7,7 @@ and optional debug information for the unshackle REST API.
from __future__ import annotations
import secrets
import traceback
from datetime import datetime, timezone
from enum import Enum
@@ -114,6 +115,9 @@ def build_error_response(
"""
Assemble a structured JSON error response.
Each error gets a short ``ref``, also appended to ``message`` so that every client prints it.
The stats middleware logs the ref with the request, so a reported ref finds the server-side record.
Args:
error: APIError or generic Exception to convert to response
debug_mode: Whether to include technical debug information
@@ -135,10 +139,12 @@ def build_error_response(
http_status = 500
retryable = False
ref = secrets.token_hex(3)
response_data: dict[str, Any] = {
"status": "error",
"error_code": error_code,
"message": message,
"message": f"{message} (ref {ref})",
"ref": ref,
"timestamp": datetime.now(timezone.utc).isoformat(),
}
@@ -161,7 +167,9 @@ def build_error_response(
response_data["debug_info"] = debug_info
return web.json_response(response_data, status=http_status)
response = web.json_response(response_data, status=http_status)
response["error"] = {"code": error_code, "ref": ref, "message": message}
return response
def categorize_exception(
+3 -2
View File
@@ -147,6 +147,7 @@ def heartbeat(handler: Handler) -> Handler:
log.exception(f"Error in {request.path}")
result = handle_api_exception(e, debug_mode=debug_mode)
response["late_status"] = result.status
response["error"] = result.get("error")
body = result.body if isinstance(result, web.Response) else None
await response.write(bytes(body) if isinstance(body, (bytes, bytearray)) else b"{}")
await response.write_eof()
@@ -2581,8 +2582,8 @@ async def dashboard_status(request: web.Request) -> web.Response:
---
summary: Dashboard server status
description: >
Version, uptime, bind address, mode, request counters per API key, and counts of
services, remote sessions and jobs. Every /api/dashboard/ route needs
Version, uptime, bind address, mode, request counters per API key, error counts by
service and error code, and counts of services, remote sessions and jobs. Every /api/dashboard/ route needs
`serve.dashboard.key` in X-Secret-Key; a user key never passes, and the routes do not
exist without a dashboard key.
tags: [Dashboard]
+36 -8
View File
@@ -7,7 +7,7 @@ import hashlib
import json
import logging
import time
from collections import Counter, deque
from collections import Counter, defaultdict, deque
from dataclasses import dataclass, field
from functools import lru_cache
from typing import Any, Deque, Dict, List, Optional, Tuple
@@ -143,6 +143,7 @@ class ServerStats:
requests_total: int = 0
requests_rejected: int = 0
keys: Dict[str, KeyStats] = field(default_factory=dict)
errors: Dict[str, Counter] = field(default_factory=lambda: defaultdict(Counter)) # service -> error_code -> count
def key_stats(self, key: Optional[str]) -> KeyStats:
"""The counters for an API key, created on first sight."""
@@ -194,6 +195,7 @@ class ServerStats:
"requests_total": self.requests_total,
"requests_rejected": self.requests_rejected,
"requests_by_key": dict(self.requests_by_key),
"errors": {service: dict(codes) for service, codes in self.errors.items()},
}
@@ -203,16 +205,18 @@ stats = ServerStats()
@web.middleware
async def stats_middleware(request: web.Request, handler: Any) -> web.StreamResponse:
from unshackle.core.api.handlers import request_secret_key
from unshackle.core.api.sanitize import sanitize_log
stats.requests_total += 1
started = time.perf_counter()
response = await handler(request)
status = response.get("late_status", response.status)
bytes_out = getattr(response, "content_length", None) or response.body_length
key = None if status == 401 else configured_key(request_secret_key(request))
if status == 401:
stats.requests_rejected += 1
else:
entry = stats.key_stats(configured_key(request_secret_key(request)))
entry = stats.key_stats(key)
entry.requests += 1
entry.last_seen = time.time()
entry.bytes_out += bytes_out
@@ -235,9 +239,33 @@ async def stats_middleware(request: web.Request, handler: Any) -> web.StreamResp
"bytes_out": bytes_out,
},
)
error = response.get("error")
if error:
service = await _request_service(request, session_id)
stats.errors[service][error["code"]] += 1
logging.getLogger("api").warning(
f"Error ref {error['ref']}: {error['code']} {status} {request.method} {sanitize_log(request.path)} "
f"service={service} user={sanitize_log(mask_key(key))}: {sanitize_log(error['message'])}"
)
return response
async def _request_service(request: web.Request, session_id: Optional[str]) -> str:
"""The installed service tag a request is for, from its remote session or its JSON body; ``-`` if none.
Only an installed tag counts, so a caller cannot grow ``stats.errors`` with made-up names.
"""
from unshackle.core.api.session_store import get_session_store
from unshackle.core.services import Services
entry = get_session_store().peek(session_id) if session_id else None
service = entry.service_tag if entry is not None else (await _consumed_json(request)).get("service")
if not isinstance(service, str):
return "-"
tag = Services.get_tag(service)
return tag if tag in Services.get_tags() else "-"
class SeqRing:
"""Keep the last N records in memory, each with a ``seq`` one above the record before it."""
@@ -347,20 +375,20 @@ def _reply_json(response: Any) -> Dict[str, Any]:
return data if isinstance(data, dict) else {}
async def _request_session_id(request: web.Request) -> Any:
"""The ``session_id`` of the JSON request body, when the handler consumed it: aiohttp caches that read.
async def _consumed_json(request: web.Request) -> Dict[str, Any]:
"""The JSON object of the request body when the handler consumed it (aiohttp caches that read), else ``{}``.
A body nobody read belongs to a call refused before the handler, a 401 or a 429. It stays
unread: reading it would spend time and memory on an unauthenticated caller, and wait on a
body that never finishes arriving.
"""
if not request.content.at_eof():
return None
return {}
try:
body = await request.json()
except Exception:
return None
return body.get("session_id") if isinstance(body, dict) else None
return {}
return body if isinstance(body, dict) else {}
async def _cdm_record(request: web.Request, drm: str, op: str, outcome: Any) -> Dict[str, Any]:
@@ -388,7 +416,7 @@ async def _cdm_record(request: web.Request, drm: str, op: str, outcome: Any) ->
elif op == "close":
session_id = request.match_info.get("session_id")
else:
session_id = await _request_session_id(request)
session_id = (await _consumed_json(request)).get("session_id")
return {
"drm": drm,
"op": op,