A simple, configurable middleware for recording request metrics with Prometheus
Make sure to have Go installed (Version 1.16
or higher).
Install promware
with go get
:
$ go get -u github.com/imbue11235/promware
middleware := promware.Default()
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
fmt.Fprintln(w, "hello world")
})
mux := http.NewServeMux()
mux.Handle("/metrics", promhttp.Handler())
mux.Handle("/", middleware(handler))
http.ListenAndServe("<addr>", mux)
middleware := promware.New(
// Skip depending on request data
promware.WithSkipFunc(func(r *http.Request) bool {
return r.URL.Path == "/something"
})
// Set subsystem
promware.WithSubsystem("my-subsystem")
// Set namespace
promware.WithNamespace("my-namespace")
// Add request counter, set name
promware.WithRequestCounter("total-requests")
// Add latency histogram, set name and buckets
promware.WithLatencyHistogram("request-latency", []float64{0.5, 1, 2})
)
This project is licensed under the MIT license.