HAProxy since version 1.6 has built in support for Lua scripting, and exposes a bunch of its' internal stats and APIs to Lua.
This is a pretty powerful feature, which let HAProxy users tweak HAProxy load balancing behaviour, gather additional stats, inspect/manipulate HTTP request/repsonse headers, register background tasks, etc.
And, of course, we can convert internal HAProxy stats and export them in Prometheus format. This is what haproxy-selfie
is about.
This project consists of several components:
- Lua scripts to export metrics in Prometheus format.
- Dockerfile and sample HAProxy config to build a docker image with HAProxy preconfigured to load the script and serve metrics.
- docker-compose file to show how to use this image with a custom config.
- Sample helm chart to show how to use this image in k8s.
haproxy-selfie
docker image has the builtin config which
configures default timeouts, HAProxy stats socket, loads Lua metrics scripts and defines metrics frontend.
By default haproxy-selfie
docker image runs HAProxy with -- /etc/haproxy/conf
command line argument, which
means that besides builtin config HAProxy will also look for configuration files in /etc/haproxy/conf
directory.
If you have docker and docker-compose installed, you can
start haproxy-selfie
locally with a custom config:
# create /etc/haproxy/conf dir locally
sudo mkdir -p /etc/haproxy/conf
# put your own config there, we are using a sample one
sudo cp cfg/sample.cfg /etc/haproxy/conf/
docker-compose -f docker-compose/haproxy-selfie.yml -p haproxy-selfie up -d
If you want to override the builtin config entirely, mount your own config at /etc/haproxy/builtin.cfg
inside the container.
There is already HAProxy Exporter, so why use haproxy-selfie
instead?
- You don't need to configure and run an external process alongside HAProxy.
- HAProxy Exporter exports a bunch of metrics with incorrect type (
gauge
instead ofcounter
). The issue has been there for a while.haproxy-selfie
uses internal HAProxy information about metric types and just translates it into prometheus types. - Although
haproxy-selfie
still uses HAProxy stats socket, the only reason for that is to get metric types directly from HAProxy. Versionv0.0.1
has metric types hardcoded, so it does not need the socket. If eventually HAProxy metric types get exposed to Lua (something likeProxy.get_stats_typed()
), there won't be any need in exposing the stat socket at all.
Metric names we export to Prometheus are pretty self-explanatory, for more details it's strongly adviced to read the official HAProxy docs.