Carry over the lab 4 solution

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

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

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Artyom
2026-10-29 00:34:03 +03:00
co-authored by Claude Opus 5.5
parent 1210dd899c
commit 540fcb35ba
171 changed files with 12909 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)