Back to feed
Dev.to
Dev.to
7/13/2026
How I shipped structured JSON logging + Prometheus metrics with zero new dependencies

How I shipped structured JSON logging + Prometheus metrics with zero new dependencies

Short summary

A developer explains how to implement structured JSON logging and Prometheus metrics in a single-process Python service without adding structlog or prometheus_client as dependencies. The approach uses a custom logging.Formatter that outputs JSON with trace IDs propagated via ContextVar across asyncio boundaries, plus a minimal Prometheus text-format metrics endpoint. The trade-off: ~200 lines of custom code vs 16 MB of transitive dependencies, giving full control over output for small projects.

  • Custom JsonFormatter outputs one JSON object per log line with trace IDs via ContextVar
  • Prometheus metrics exposed in text format at a stable URL with no external library
  • Trade-off: 200 lines of code vs 16 MB of dependencies — justified for single-process services

Generated with AI, which can make mistakes.

Is this a good recommendation for you?

Comments

Failed to load comments. Please try again.

Explore more