Carry over the lab 3 solution

Bring the application, own tests, Dockerfile, toolchain, compose stack,
HAProxy and k6 configuration, migrations, hooks, evidence and level 4
scripts and the CI job from the lab 3 solution commit 61c0c28, merge
the lab 3 Makefile targets into the lab 4 skeleton Makefile without
touching its targets, and append the lab 3 solution variables to the
skeleton .env.example. The package and the application follow the
contract version 1.4.0, and the image and the throwaway test containers
are tagged lab4 so they do not collide with a lab 3 stand on the same
host.

This is the starting point of the lab 4 cycle: lint, own tests and the
offline skeleton check pass before any lab 4 change.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Artyom
2026-10-28 20:17:51 +03:00
co-authored by Claude Opus 5.5
parent b0f463ac5c
commit bf8e4e3cd1
129 changed files with 8698 additions and 1 deletions
@@ -0,0 +1,113 @@
import logging
import sys
from typing import Any
import orjson
import structlog
from structlog.typing import EventDict, Processor, WrappedLogger
PLATFORM_FIELDS = frozenset(
{
"method",
"route",
"status",
"duration_ms",
"request_id",
"event_name",
"error",
"reason",
"limit",
}
)
def render(obj: object, **_kwargs: object) -> str:
return orjson.dumps(obj).decode()
RESERVED = frozenset({"event", "level", "ts", "logger"})
FOREIGN_LOGGERS = (
"uvicorn",
"uvicorn.error",
"gunicorn",
"gunicorn.error",
"asyncio",
)
SILENCED_LOGGERS = ("uvicorn.access", "gunicorn.access")
def field_filter(allowed: frozenset[str]) -> Processor:
def drop_unlisted_fields(
_logger: WrappedLogger, _method: str, event_dict: EventDict
) -> EventDict:
return {
key: value
for key, value in event_dict.items()
if (key in RESERVED or key in allowed) and value is not None
}
return drop_unlisted_fields
def configure(level: str = "INFO", allowed_fields: frozenset[str] = PLATFORM_FIELDS) -> None:
shared = [
structlog.contextvars.merge_contextvars,
structlog.processors.add_log_level,
structlog.processors.TimeStamper(fmt="iso", utc=True, key="ts"),
]
structlog.configure(
processors=[*shared, structlog.stdlib.ProcessorFormatter.wrap_for_formatter],
logger_factory=structlog.stdlib.LoggerFactory(),
wrapper_class=structlog.stdlib.BoundLogger,
cache_logger_on_first_use=False,
)
handler = logging.StreamHandler(sys.stdout)
handler.setFormatter(
structlog.stdlib.ProcessorFormatter(
foreign_pre_chain=shared,
processors=[
structlog.stdlib.ProcessorFormatter.remove_processors_meta,
field_filter(allowed_fields),
structlog.processors.JSONRenderer(serializer=render),
],
)
)
root = logging.getLogger()
for existing in list(root.handlers):
root.removeHandler(existing)
root.addHandler(handler)
root.setLevel(level.upper())
for name in FOREIGN_LOGGERS:
foreign = logging.getLogger(name)
for existing in list(foreign.handlers):
foreign.removeHandler(existing)
foreign.propagate = True
for name in SILENCED_LOGGERS:
silenced = logging.getLogger(name)
for existing in list(silenced.handlers):
silenced.removeHandler(existing)
silenced.propagate = False
class StructlogLogger:
__slots__ = ("_logger",)
def __init__(
self,
name: str = "messenger",
level: str = "INFO",
allowed_fields: frozenset[str] = PLATFORM_FIELDS,
) -> None:
configure(level, allowed_fields)
self._logger = structlog.get_logger(name)
def info(self, event: str, /, **fields: Any) -> None:
self._logger.info(event, **fields)
def warning(self, event: str, /, **fields: Any) -> None:
self._logger.warning(event, **fields)
def error(self, event: str, /, **fields: Any) -> None:
self._logger.error(event, **fields)