Developer builds structured JSON logging and Prometheus metrics with no extra dependencies
A solo Python developer replaced the popular structlog and prometheus_client libraries by writing roughly 200 lines of custom code, eliminating approximately 16 MB of transitive dependencies. The approach delivers four core observability features: structured JSON log lines, per-request trace IDs propagated via Python's ContextVar, stderr output compatible with journald and Docker, and Prometheus-format metrics at a stable URL. The custom JsonFormatter class serializes each log record into a single JSON object and merges any extra fields passed by the caller. Python's ContextVar mechanism ensures trace IDs flow correctly across asyncio boundaries, including into threads spawned with asyncio.to_thread. The author argues that for single-process, single-author projects with a small number of metrics, full-featured libraries introduce more complexity than the problem warrants.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.
Discussion (0)
Log in to join the discussion and vote.
Log in