पाठ 10 / 35
Counters, Gauges, Histograms and Summaries
यह पाठ अभी हिंदी में अनुवादित नहीं है, इसलिए अंग्रेज़ी में दिखाया जा रहा है।
Choose the correct metric type for a measurement and read the Prometheus text exposition format.
लेखक: Rahul Agarwal · 15 साल से वेब डेवलपर
Four metric types
A counter only goes up (or resets to zero when the process restarts). Use it for things that accumulate: requests served, errors, bytes sent. The raw value is rarely interesting; you look at its rate of increase.
A gauge is a value that can go up and down: memory in use, queue length, temperature, requests currently in flight. You read its current value directly.
A histogram counts observations into configurable buckets (for example, requests that took at most 0.1 s, at most 0.25 s, and so on) and also tracks their total sum and count. Because buckets from many instances can be added together, histograms let you compute percentiles across a whole fleet on the server side.
A summary computes quantiles such as p99 inside the client process. It is accurate for that one process, but quantiles from different instances cannot be meaningfully combined, so histograms are usually preferred for services that run as many replicas.
Choosing a metric type
Read the figure as a checklist from left to right and stop at the first type that fits the measurement: does it only accumulate (counter)? Does it move both ways (gauge)? Do you need its distribution, such as latency (histogram, the usual default)? The summary comes last because it fits only the rare case where one process needs client-side quantiles that you never aggregate.
Instrumenting with prometheus_client
The official Python client registers metrics globally and serves them over HTTP. It needs pip install prometheus-client; the loop runs forever, so there is no printed output.
import random
import time
from prometheus_client import Counter, Gauge, Histogram, start_http_server
REQUESTS = Counter("http_requests_total", "HTTP requests handled", ["method", "status"])
IN_FLIGHT = Gauge("http_requests_in_flight", "Requests currently being served")
LATENCY = Histogram(
"http_request_duration_seconds", "Request latency in seconds",
buckets=[0.05, 0.1, 0.25, 0.5, 1, 2.5],
)
def handle():
with IN_FLIGHT.track_inprogress(), LATENCY.time():
time.sleep(random.uniform(0.01, 0.3)) # pretend to do work
REQUESTS.labels(method="GET", status="200").inc()
if __name__ == "__main__":
start_http_server(8000) # exposes /metrics on port 8000
while True:
handle()What Prometheus scrapes
A trimmed sample of the text served at /metrics. Histogram buckets are cumulative: the le="0.25" bucket includes everything in le="0.1", and +Inf always equals _count.
# HELP http_requests_total HTTP requests handled
# TYPE http_requests_total counter
http_requests_total{method="GET",status="200"} 1027
# HELP http_requests_in_flight Requests currently being served
# TYPE http_requests_in_flight gauge
http_requests_in_flight 3
# TYPE http_request_duration_seconds histogram
http_request_duration_seconds_bucket{le="0.1"} 380
http_request_duration_seconds_bucket{le="0.25"} 905
http_request_duration_seconds_bucket{le="+Inf"} 1027
http_request_duration_seconds_sum 168.4
http_request_duration_seconds_count 1027त्वरित जाँच: Which metric type fits "number of messages currently waiting in a queue"?
- Gauge
- Counter
- Histogram
- Summary
Answer
Gauge — Queue depth rises and falls, so it is a gauge. A counter would only fit "total messages ever enqueued".