Interview Prep/Python for DevOps/Testing, services & metrics

What changes when you run `prometheus_client` under gunicorn with multiple workers?

AdvancedConceptTesting, services & metrics
Answer

Each gunicorn worker is a separate process with its own in-memory metrics, so a scrape hits one random worker and you see inconsistent, jumping counters. Use multiprocess mode: set PROMETHEUS_MULTIPROC_DIR to an empty directory before workers start, build the /metrics response with a CollectorRegistry plus multiprocess.MultiProcessCollector(registry), call multiprocess.mark_process_dead(worker.pid) in gunicorn's child_exit hook, and set multiprocess_mode on Gauges (for example "livesum" or "max"). Clear the directory on each start.

All Python for DevOps questions Roadmap: Python for automation

More on Testing, services & metrics