Metrics for Prometheus
Logs tell you what happened to one request; metrics tell you how the whole front door is doing right now: request rates, error ratios, latency distributions, backend health, open connections. Prometheus scrapes them over HTTP from a /metrics endpoint (Prometheus Architecture). HAProxy, Caddy and Traefik ship a Prometheus endpoint built in; open-source Nginx has only a tiny status page and needs an exporter.
| Server | Endpoint | Enable with |
|---|---|---|
| Nginx | stub_status page (text), turned into metrics by nginx-prometheus-exporter |
location = /basic_status { stub_status; } |
| HAProxy | /metrics from the built-in exporter (Ubuntu's build has USE_PROMEX) |
http-request use-service prometheus-exporter if { path /metrics } |
| Caddy | /metrics on the admin endpoint (localhost:2019) |
process metrics always; HTTP metrics with the metrics global option |
| Traefik | /metrics on an entry point |
metrics.prometheus in the static configuration |
All four in one session
python3 -m http.server 9000 --bind 127.0.0.1 > /dev/null 2>&1 &
for i in $(seq 50); do curl -s 127.0.0.1:9000/ > /dev/null && break; sleep 0.1; done
# --- Nginx: stub_status
cat > nginx.conf <<'EOF'
pid nginx.pid;
error_log stderr;
events {}
http {
access_log off;
server {
listen 127.0.0.1:8081;
location / { proxy_pass http://127.0.0.1:9000; }
location = /basic_status { stub_status; allow 127.0.0.1; deny all; }
}
}
EOF
nginx -p "$PWD" -c nginx.conf -e stderr
for i in 1 2 3; do curl -s -o /dev/null 127.0.0.1:8081/; done
echo "=== nginx stub_status"; curl -s 127.0.0.1:8081/basic_status
# --- HAProxy: built-in Prometheus exporter
cat > haproxy.cfg <<'EOF'
global
pidfile haproxy.pid
defaults
mode http
timeout connect 1s
timeout client 5s
timeout server 5s
frontend prometheus
bind 127.0.0.1:8405
http-request use-service prometheus-exporter if { path /metrics }
no log
frontend fe
bind 127.0.0.1:8082
default_backend be_app
backend be_app
server app1 127.0.0.1:9000 check
EOF
haproxy -D -f haproxy.cfg
for i in 1 2 3; do curl -s -o /dev/null 127.0.0.1:8082/; done
echo "=== haproxy /metrics"
curl -s 127.0.0.1:8405/metrics | grep -E '^haproxy_(backend_http_responses_total\{proxy="be_app",code="2xx"|server_status\{.*state="UP")'
# --- Caddy: metrics on the admin endpoint
cat > Caddyfile <<'EOF'
{
auto_https off
metrics
}
:8083 {
reverse_proxy 127.0.0.1:9000
}
EOF
caddy start --config Caddyfile > caddy.log 2>&1
for i in 1 2 3; do curl -s -o /dev/null 127.0.0.1:8083/; done
echo "=== caddy /metrics"
curl -s localhost:2019/metrics | grep -E '^caddy_(http_requests_total|http_request_duration_seconds_count|reverse_proxy_upstreams_healthy)'
# --- Traefik: metrics on the "traefik" entry point
cat > traefik.yml <<'EOF'
global: {checkNewVersion: false, sendAnonymousUsage: false}
entryPoints:
web: {address: "127.0.0.1:8084"}
traefik: {address: "127.0.0.1:8090"}
metrics:
prometheus:
entryPoint: traefik
addRoutersLabels: true
providers:
file: {filename: routes.yml, watch: false}
EOF
cat > routes.yml <<'EOF'
http:
routers:
app: {rule: "PathPrefix(`/`)", service: app, entryPoints: [web]}
services:
app: {loadBalancer: {servers: [{url: "http://127.0.0.1:9000"}]}}
EOF
traefik --configFile=traefik.yml > traefik.log 2>&1 &
for i in $(seq 100); do curl -sf -o /dev/null 127.0.0.1:8084/ && break; sleep 0.1; done # 1st request
for i in 1 2; do curl -s -o /dev/null 127.0.0.1:8084/; done; curl -s -o /dev/null 127.0.0.1:8084/missing
echo "=== traefik /metrics"
curl -s 127.0.0.1:8090/metrics | grep -E '^traefik_(router|service)_requests_total'
nginx -p "$PWD" -c nginx.conf -e stderr -s quit 2>/dev/null; kill $(cat haproxy.pid); caddy stop > /dev/null 2>&1; kill %2 %1=== nginx stub_status
Active connections: 1
server accepts handled requests
4 4 4
Reading: 0 Writing: 1 Waiting: 0
=== haproxy /metrics
haproxy_backend_http_responses_total{proxy="be_app",code="2xx"} 3
haproxy_server_status{proxy="be_app",server="app1",state="UP"} 1
=== caddy /metrics
caddy_http_request_duration_seconds_count{code="200",handler="reverse_proxy",method="GET",server="srv0"} 3
caddy_http_requests_total{handler="reverse_proxy",server="srv0"} 3
caddy_reverse_proxy_upstreams_healthy{upstream="127.0.0.1:9000"} 1
=== traefik /metrics
traefik_router_requests_total{code="200",method="GET",protocol="http",router="app@file",service="app@file"} 3
traefik_router_requests_total{code="404",method="GET",protocol="http",router="app@file",service="app@file"} 1
traefik_service_requests_total{code="200",method="GET",protocol="http",service="app@file"} 3
traefik_service_requests_total{code="404",method="GET",protocol="http",service="app@file"} 1
What each gives you:
- Nginx
stub_statushas only global counters: active connections, accepted/handled connections, total requests, and connections reading/writing/waiting (idle keep-alive). The 4 requests are the 3 proxied ones plus the status request itself. There is nothing per location, per status code or per upstream; for that you parse access logs (or use NGINX Plus's API, or a module such as the VTS module).nginx-prometheus-exporterconverts the page intonginx_connections_active,nginx_http_requests_totaland so on. - HAProxy exports hundreds of series per frontend, backend and server: responses by status class,
haproxy_server_statusper state, check results, queue lengths, response-time averages. On busy configurations, restrict it with query parameters (/metrics?scope=backend&scope=server) to keep the scrape small. - Caddy labels HTTP metrics by
serverandhandler(addper_hostto themetricsoption for ahostlabel) and reports upstream health from the reverse proxy. - Traefik labels by router, service, entry point, status code and method: per-route dashboards come for free. The
404from an unmatched path was still counted on theapp@filerouter, because the backend returned it.
Turning them into the signals you watch
With the RED method (Rate, Errors, Duration, see RED & USE), the queries for Traefik look like this:
# requests per second, per router
sum by (router) (rate(traefik_router_requests_total[5m]))
# share of 5xx responses, per service
sum by (service) (rate(traefik_service_requests_total{code=~"5.."}[5m]))
/ sum by (service) (rate(traefik_service_requests_total[5m]))
# 95th percentile latency, per service
histogram_quantile(0.95, sum by (le, service) (rate(traefik_service_request_duration_seconds_bucket[5m])))And alerts that catch front-door problems early:
# Sample Prometheus alerting rules
groups:
- name: edge
rules:
- alert: BackendServerDown
expr: haproxy_server_status{state="UP"} == 0
for: 2m
labels: {severity: page}
annotations:
summary: "{{ $labels.proxy }}/{{ $labels.server }} is not UP"
- alert: EdgeHigh5xxRatio
expr: |
sum(rate(haproxy_backend_http_responses_total{code="5xx"}[5m]))
/ sum(rate(haproxy_backend_http_responses_total[5m])) > 0.02
for: 10m
labels: {severity: ticket}Keep the metrics endpoints off the public internet: bind them to an internal address (as the sessions did with 127.0.0.1), or protect them at the network level. They reveal backend names, addresses and traffic patterns.
Test yourself
Which of the four servers needs a separate exporter for Prometheus in its open-source version?
- HAProxy
- Caddy
- Traefik
- Nginx
Can Nginx's stub_status tell you the rate of 5xx responses?
- Yes
- No: it has only global connection and request counters; status codes come from access logs (or NGINX Plus/modules)
- Only with gzip
- Only for upstreams
Why add addRoutersLabels: true to Traefik's Prometheus configuration?
- It is required
- To get per-router request metrics, so dashboards and alerts can be broken down by route
- To enable TLS
- To reduce memory