Newer
Older

Christophe de Vienne
committed
package orusapi
import (
"fmt"
"net/http"
"strings"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
// Prometheus serves the '/metrics' endpoint.

Christophe de Vienne
committed
func Prometheus(path string) func(next http.Handler) http.Handler {
handler := promhttp.Handler()
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
if strings.HasPrefix(r.URL.Path, path) {
handler.ServeHTTP(rw, r)

Christophe de Vienne
committed
return
}
if next == nil {
rw.Header().Set("Content-Type", "text/plain")
rw.WriteHeader(http.StatusNotFound)
_, _ = rw.Write([]byte(fmt.Sprintf("%q not found", path)))

Christophe de Vienne
committed
return
}
next.ServeHTTP(rw, r)
})
}
}