Skip to content

Commit

Permalink
Adding prom metrics handler
Browse files Browse the repository at this point in the history
- This is really just a hammer to add in prom metrics exposed on :8080/metrics.  As you see
it wraps every endpoint.

Additionally, it does not attempt to match on the actual pattern of the URL and instead
uses the URL.  This in reality is not a good practice as Prometheus does not scale well
with super high cardinality data
  • Loading branch information
Justin Nauman authored and jbeda committed Dec 21, 2017
1 parent ba741ff commit caca90d
Show file tree
Hide file tree
Showing 108 changed files with 24,530 additions and 2 deletions.
1 change: 1 addition & 0 deletions Dockerfile.build
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ RUN apk update && apk upgrade && apk add --no-cache git nodejs bash
# Install any binaries in to /usr/local/bin instead of /go/bin as we will mask
# /go/bin when doing real builds.
RUN GOPATH=/tmp GOBIN=/usr/local/bin go get -u github.com/jteeuwen/go-bindata/...
RUN GOPATH=/tmp GOBIN=/usr/local/bin go get github.com/tools/godep
49 changes: 48 additions & 1 deletion Godeps/Godeps.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 23 additions & 1 deletion pkg/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"net/http/httputil"
"os"
"path/filepath"
"strconv"
"strings"

"github.com/kubernetes-up-and-running/kuard/pkg/debugprobe"
Expand All @@ -35,9 +36,28 @@ import (
"github.com/kubernetes-up-and-running/kuard/pkg/sitedata"
"github.com/kubernetes-up-and-running/kuard/pkg/version"

"github.com/felixge/httpsnoop"
"github.com/julienschmidt/httprouter"
"github.com/prometheus/client_golang/prometheus"
)

func init() {
prometheus.MustRegister(requestDuration)
}

var requestDuration = prometheus.NewHistogramVec(prometheus.HistogramOpts{
Name: "request_duration_seconds",
Help: "Time serving HTTP request",
Buckets: prometheus.DefBuckets,
}, []string{"method", "route", "status_code"})

func promMiddleware(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
m := httpsnoop.CaptureMetrics(h, w, r)
requestDuration.WithLabelValues(r.Method, r.URL.Path, strconv.Itoa(m.Code)).Observe(m.Duration.Seconds())
})
}

func loggingMiddleware(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Printf("%s %s %s", r.RemoteAddr, r.Method, r.URL)
Expand Down Expand Up @@ -107,7 +127,7 @@ func fileExists(name string) bool {
}

func (k *App) Run() {
r := loggingMiddleware(k.r)
r := promMiddleware(loggingMiddleware(k.r))

// Look to see if we can find TLS certs
certFile := filepath.Join(k.c.TLSDir, "kuard.crt")
Expand Down Expand Up @@ -137,6 +157,8 @@ func NewApp() *App {
router.GET("/", k.rootHandler)
router.GET("/-/*path", k.rootHandler)

router.Handler("GET", "/metrics", prometheus.Handler())

// Add the static files
sitedata.AddRoutes(router, "/built")
sitedata.AddRoutes(router, "/static")
Expand Down
20 changes: 20 additions & 0 deletions vendor/github.com/beorn7/perks/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit caca90d

Please sign in to comment.