Enable Prometheus metrics in Caddy

March 2, 2016


The Caddy webserver is a modern webserver written in Go. I like it because I can easily extend it by writing some middleware (in Go). One of the things missing is metrics, which prompted me to implement enough metrics to make me happy.

Using this is relatively straight forward, but you’ll need to compile Caddy yourself and add this middleware.


(Assuming you have Go installed), first get the goodies:

go get github.com/mholt/caddy
This is needed to compile in the new middleware, go get github.com/caddyserver/caddyext
go get github.com/miekg/caddy-prometheus

Then, with caddyext register the new middleware:

caddyext install prometheus:github.com/miekg/caddy-prometheus

Optionally you can check your middleware stack with caddyext stack, but be sure to move prometheus early in the stack:

caddyext move prometheus 0

Now compile your new Caddy:

caddyext build

This should create a customCaddy binary which you can run. To enable metrics you’ll need to add the directive prometheus to each host section that you’ll want metrics for. The default address and path for the metrics’s is http://localhost:9180/metrics

Curling this should result in something like this:

caddy_http_request_count_total{host="archive.miek.nl"} 1416
caddy_http_request_duration_seconds_bucket{host="archive.miek.nl",le="0.005"} 1338
caddy_http_request_duration_seconds_bucket{host="archive.miek.nl",le="0.01"} 1370
caddy_http_request_duration_seconds_bucket{host="archive.miek.nl",le="0.025"} 1396
caddy_http_request_duration_seconds_bucket{host="archive.miek.nl",le="0.05"} 1396
caddy_http_request_duration_seconds_bucket{host="archive.miek.nl",le="0.1"} 1396

Grafana and Metrics

Then if you are running prometheus and grafana you can the query and plot neat graphs. For instance plotting number of qps for miek.nl, can be done with sum(rate(caddy_http_request_count_total{host=~"miek.nl"}[5m])) by (instance), which results on my (highly popular web site):

Queries per second for miek.nl.

Queries per second for miek.nl.

Some day I hope to hit 1 qps.

monitoring  prometheus  caddy