- Current number of in-progress requests
- Free memory, queue length, or number of running jobs
- Resource values that both increase and decrease
- Important: the order of
labelnamesmust match the order of values passed to.labels(...). In this example we define['method', 'path']and call.labels(request.method, request.path).
inc()— increment the gauge by 1 (or a specified amount)dec()— decrement the gauge by 1 (or a specified amount)set(value)— explicitly set the gauge tovalue
Ensure every
inc() is paired with a corresponding dec() (even when errors occur). In frameworks like Flask, prefer teardown_request, middleware, or a try/finally block around request handling so the gauge is always decremented when a request finishes.
Additional guidance
- Use a Gauge when you need an up-and-down metric (active jobs, queue length, free resources).
- For request duration or size distributions, prefer
HistogramorSummary. - Label cardinality: avoid high-cardinality label values (e.g., raw user IDs or long paths) to prevent memory blowups in the Prometheus server.
Never allow unpaired increments. Missing decrements will inflate your gauge and produce misleading monitoring alerts. Implement error paths and teardown hooks to guarantee balanced
inc()/dec() calls.