forked from mtusi2026/mtusi2026lab6
Carry over the lab 5 solution
Bring the application, own tests, Dockerfile, toolchain, compose stack, HAProxy, k6, RustFS and RabbitMQ configuration, migrations, hooks, scenario scripts and the CI job from the lab 5 solution commit 64d5c67, keep the lab 5 Makefile targets on top of the unchanged skeleton targets, and append the lab 5 solution variables to the skeleton .env.example. The package and the application follow the contract version 1.6.0, and the image, the throwaway test containers and the broker vhost description are tagged lab6 so they do not collide with a lab 5 stand on the same host. This is the starting point of the lab 6 cycle: lint, own tests and the offline skeleton check pass before any lab 6 change. 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
a0d3947710
commit
5ae3d77152
@@ -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)
|
||||
Reference in New Issue
Block a user