RousselTMLEARNING

Proxy & networking

Monitoring and statistics in HAProxy

How to enable the native statistics dashboard and expose metrics for Prometheus.

1. The native statistics page #

A lightweight web interface to monitor your servers.

Configuring the listen block #

It is recommended to listen on a separate port and protect access with a password.

listen stats
    bind *:8404
    stats enable
    stats uri /admin?stats
    stats refresh 10s
    stats auth admin:MonMotDePasse

2. Integration with Prometheus #

Exposing metrics for long-term monitoring.

Using the prometheus-exporter module #

Since version 2.0, HAProxy can expose metrics directly in Prometheus format without a third-party agent.

frontend stats
    bind *:8405
    http-request use-service prometheus-exporter if { path /metrics }

3. Native OpenTelemetry (OTLP) support #

HAProxy now integrates with the industry standard for traces and metrics.

Exporting to an OTLP collector #

Since version 2.9, HAProxy natively supports OpenTelemetry. This feature sends telemetry data directly to an OTel collector (or tools such as Jaeger, Honeycomb or Grafana Tempo) without an intermediate agent.

What it brings:
  • Standardization: a single format for traces and metrics, compatible with all modern tools.
  • Distributed traces: correlate a request going through HAProxy with your microservices' traces for end-to-end debugging.
  • Rich data: export precise metadata about the HTTP request life cycle.
# Exemple de configuration (nécessite le module otlp.so)
module-load /usr/lib/haproxy/otlp.so

otlp
    endpoint my-otel-collector:4317
    interval 5s
    add-header x-otel-traceid

4. Interpreting key indicators #

Which metrics should you monitor first?

Sessions and errors #

  • Cur: number of active connections.
  • Rate: number of new connections per second.
  • Check status: health of the backend servers.
  • Errors (Req/Resp): 4xx or 5xx error rate.

Conclusion #

Observability is key.

Anticipating failures #

With statistics, you can detect a server that is slowing down before it fails completely.

Hands-on: stats page, Runtime API and Prometheus #

Enabling and using statistics.

Enabling the statistics page #

listen stats
    bind 127.0.0.1:8404
    mode http
    stats enable
    stats uri /stats
    stats refresh 5s
    stats auth admin:MotDePasseFort
    stats admin if LOCALHOST        # permet de mettre un serveur en maintenance depuis l'UI
curl -s -u admin:MotDePasseFort "http://127.0.0.1:8404/stats;csv" | head -3
ssh -L 8404:127.0.0.1:8404 admin@lb1     # accéder à la page sans l'exposer

Runtime API with socat #

Statistics through the Runtime API
The status field shows UP, DOWN or MAINT for each server.
# global : stats socket /var/run/haproxy.sock mode 660 level admin
echo "show info" | sudo socat stdio /var/run/haproxy.sock | head
echo "show servers state" | sudo socat stdio /var/run/haproxy.sock
echo "set server app/web3 state maint" | sudo socat stdio /var/run/haproxy.sock      # sortir un serveur
echo "set server app/web3 state ready" | sudo socat stdio /var/run/haproxy.sock
echo "set server app/web1 weight 50%" | sudo socat stdio /var/run/haproxy.sock

Exporting to Prometheus #

frontend prometheus
    bind *:8405
    mode http
    http-request use-service prometheus-exporter if { path /metrics }
    no log
# prometheus.yml
scrape_configs:
  - job_name: haproxy
    static_configs: [{ targets: ["lb1:8405", "lb2:8405"] }]

# PromQL utiles
sum by (proxy) (rate(haproxy_frontend_http_responses_total{code="5xx"}[5m]))
haproxy_backend_up == 0
haproxy_backend_current_queue > 0

Related articles

Anatomy of HAProxy configuration, clustering and high availability

Understand the structure of an haproxy.cfg file: the global, defaults, frontend (high performance), backend a…

HAProxy in Kubernetes: the high-performance Ingress Controller

Discover in depth how HAProxy integrates with Kubernetes: direct-to-pod architecture, hot update options (DNS…

What's new in HAProxy: OpenTelemetry, native JWT and performance optimizations

Explore HAProxy's latest innovations: native OpenTelemetry support for observability, JWT validation at the e…

Keep learning

Support us

♥ SUPPORT US

Support RousselTM Learning

Courses, articles, labs and certificates stay free with no intrusive ads. A voluntary contribution, of any amount you choose, pays for hosting, lab environments and new content.

Contribute with PayPal Any amount · card or PayPal accountLearn more
QR code PayPal
Or scan this QR code with your phone

Thank you, every contribution counts!