Skip to content

HAProxy with Lua script to export metrics in Prometheus format

License

Notifications You must be signed in to change notification settings

AnchorFree/haproxy-selfie

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

haproxy-selfie -- HAProxy with Prometheus metrics by Lua

Table of Contents

Description

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:

Usage

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.

Comparison with HAProxy exporter

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 of counter). 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. Version v0.0.1 has metric types hardcoded, so it does not need the socket. If eventually HAProxy metric types get exposed to Lua (something like Proxy.get_stats_typed()), there won't be any need in exposing the stat socket at all.

Exported metrics

Metric names we export to Prometheus are pretty self-explanatory, for more details it's strongly adviced to read the official HAProxy docs.