Skip to content

Commit

Permalink
Merge 8babf77 into 610de4b
Browse files Browse the repository at this point in the history
  • Loading branch information
gaplyk committed Mar 29, 2018
2 parents 610de4b + 8babf77 commit 912766e
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
50 changes: 47 additions & 3 deletions server/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/go-kit/kit/metrics"
"github.com/go-kit/kit/metrics/provider"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)

func expvarHandler(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -148,10 +149,53 @@ func TimedAndCounted(handler http.Handler, fullPath string, method string, p pro
}
}

// PrometheusTimedAndCounted wraps a http.Handler with via prometheus.InstrumentHandler
// PrometheusTimedAndCounted wraps a http.Handler with via promhttp.InstrumentHandlerCounter and promhttp.InstrumentHandlerDuration
func PrometheusTimedAndCounted(handler http.Handler, name string) *Timer {
return &Timer{
isProm: true,
handler: prometheus.InstrumentHandler(name, handler),
isProm: true,
handler: promhttp.InstrumentHandlerCounter(prometheusCounter(name),
promhttp.InstrumentHandlerDuration(prometheusHistogram(name), handler)),
}
}

func prometheusCounter(name string) *prometheus.CounterVec {
mname := strings.Replace(name, "/", "_", -1)

// create a request code counter
counter := prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: mname + "_requests_total",
Help: "Total Requests for " + name,
},
[]string{"code", "method"},
)
// do not panic when metric already registered
err := prometheus.Register(counter)
if err != nil {
if are, ok := err.(prometheus.AlreadyRegisteredError); ok {
counter = are.ExistingCollector.(*prometheus.CounterVec)
}
}
return counter
}

func prometheusHistogram(name string) *prometheus.HistogramVec {
mname := strings.Replace(name, "/", "_", -1)

histogram := prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Name: mname + "_requests_duration",
Help: "Duration of Requests for " + name,
Buckets: []float64{0.05, 0.50, 0.90, 0.95, 0.99},
},
[]string{"code", "method"},
)

err := prometheus.Register(histogram)
if err != nil {
if are, ok := err.(prometheus.AlreadyRegisteredError); ok {
histogram = are.ExistingCollector.(*prometheus.HistogramVec)
}
}
return histogram
}
3 changes: 2 additions & 1 deletion server/simple_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/go-kit/kit/metrics/provider"
"github.com/gorilla/mux"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
netContext "golang.org/x/net/context"
"google.golang.org/appengine"

Expand Down Expand Up @@ -143,7 +144,7 @@ func (s *SimpleServer) Start() error {
s.cfg.Metrics.Path = "/metrics"
}
s.mux.HandleFunc("GET", s.cfg.Metrics.Path,
prometheus.InstrumentHandler("prometheus", prometheus.UninstrumentedHandler()))
promhttp.HandlerFor(prometheus.DefaultGatherer, promhttp.HandlerOpts{}).ServeHTTP)
}

// if this is an App Engine setup, just run it here
Expand Down

0 comments on commit 912766e

Please sign in to comment.