When metrics eat your memory 🧠 🍽️

Follow-up to my earlier post on Prometheus + Multiprocess Apps ...

 

A few days in, I noticed the metrics directory was ballooning in memory 🎈

 

Digging in, I realized the Prometheus Python client (in multiprocess mode) writes separate files per metric per process. By default, those files are named things like counter_12345.db, where 12345 is the PID.

 

So when a uWSGI worker dies and gets replaced β€” totally normal behavior β€” the new process gets its own set of metric files. But the old files? They just stay there.

 

Since the client doesn’t automatically clean up stale files, the directory just keeps growing.

 

βœ… Fix: I configured a cleanup step to remove metrics for dead processes.

 

πŸ’‘ Takeaway: In multiprocess mode, the metrics client tracks data per PID. Without cleanup, these files accumulate and quietly consume memory β€” especially in environments with frequent process restarts.


Goodbye temp venv hacks πŸ‘‹

Today I learned how much I enjoy using uv scripts for quick, one-off tasks.

 

You can define dependencies right at the top of the script, and when you run it with uv, it spins up a temporary virtual environment automatically. Once the script finishes, the environment is destroyed β€” super clean 🧹

 

This is perfect for things like initial tasks when starting a container, or scripts that import data, run a migration, or do any kind of setup that isn't needed once the main app is running.

 

πŸ’‘ Takeaway: uv scripts give you a disposable, isolated environment without any manual setup β€” ideal for clean, repeatable scripting without leaving a mess behind.


Prometheus + Multiprocess Apps: A Lesson from the Trenches

I recently deployed an API using uWSGI with multiple workers. I exposed a /metrics endpoint for Prometheus scraping β€” all looked good.

 

Until I realized… the metrics were off 🫠

 

Turns out, when you're using multiple uWSGI workers, Prometheus' Python client needs multiprocess mode enabled to aggregate metrics across all worker processes. Without it, each process exposes its own separate metrics β€” so counters, for example, appear to jump up and down instead of increasing cumulatively across all workers.

 

βœ… Fix: Configured multiprocess mode, so all workers write metrics to a shared directory.

 

πŸ’‘ Takeaway: With multiple workers per replica, Prometheus scrapes the /metrics endpoint from only one worker per replica at random β€” so without multiprocess mode, your Prometheus metrics won't reflect the true state of your API β€” making it impossible to accurately track what's really happening.