now does integrated metrics! fixes #5.
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is passing

thanks to @slok for setting me straight with my dumb question
on how to use his cool package:

https://github.com/slok/go-http-metrics/issues/41
This commit is contained in:
2020-10-03 00:22:23 -07:00
parent df2b0fb1ac
commit 2a45cb3a5b
5 changed files with 100 additions and 13 deletions

View File

@@ -6,6 +6,9 @@ import (
"time"
"github.com/go-chi/chi/middleware"
metrics "github.com/slok/go-http-metrics/metrics/prometheus"
ghmm "github.com/slok/go-http-metrics/middleware"
"github.com/slok/go-http-metrics/middleware/std"
)
// the following is from
@@ -78,3 +81,12 @@ func (s *server) AuthMiddleware() func(http.Handler) http.Handler {
})
}
}
func (s *server) MetricsMiddleware() func(http.Handler) http.Handler {
mdlw := ghmm.New(ghmm.Config{
Recorder: metrics.NewRecorder(metrics.Config{}),
})
return func(next http.Handler) http.Handler {
return std.Handler("", mdlw, next)
}
}

View File

@@ -1,11 +1,13 @@
package httpserver
import (
"net/http"
"time"
sentryhttp "github.com/getsentry/sentry-go/http"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
func (s *server) routes() {
@@ -20,10 +22,11 @@ func (s *server) routes() {
s.router.Use(middleware.RequestID)
s.router.Use(s.LoggingMiddleware())
s.router.Use(s.MetricsMiddleware())
// CHANGEME to suit your needs, or pull from config.
// timeout for request context: your handlers must finish within
// this window:
// CHANGEME to suit your needs, or pull from config.
s.router.Use(middleware.Timeout(60 * time.Second))
// this adds a sentry reporting middleware if and only if sentry is
@@ -32,7 +35,6 @@ func (s *server) routes() {
// routes are, so now it's up here. unfortunately this cannot
// coexist with the normal chi Recoverer handler which prints a nice
// colorful stack trace to the console
if s.sentryEnabled {
// Options docs at
// https://docs.sentry.io/platforms/go/guides/http/
@@ -76,4 +78,14 @@ func (s *server) routes() {
s.handlePanic(),
)
// CHANGEME you probably want to wrap the following in some kind of
// auth like http basic auth which is easy to set up on your
// rometheus collector
// TODO(sneak): read http basic auth user/pass for /metrics
// out of environment vars
s.router.Get(
"/metrics",
http.HandlerFunc(promhttp.Handler().ServeHTTP),
)
}