Skip to content

Commit

Permalink
Revert "Improve Prometheus metrics" (#148)
Browse files Browse the repository at this point in the history
* Revert "default handler for 404 metrics to '__404__'"

This reverts commit 298b6ae.

* Revert "update comments"

This reverts commit bec4a80.

* Revert "address comments"

This reverts commit 91ef576.

* Revert "Add panic if metric was not registered."

This reverts commit 1f73b81.

* Revert "Improve Prometheus metrics"

This reverts commit 99001b6.
  • Loading branch information
oliver-nyt authored and jprobinson committed Jun 5, 2018
1 parent 298b6ae commit 5cd7239
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 63 deletions.
64 changes: 3 additions & 61 deletions server/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,6 @@ 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"
)

var (
// DefaultBuckets are used by prometheus instrumentation.
// If you wish to have different buckets, alter this variable before registering your service.
DefaultBuckets = []float64{0.05, 0.50, 0.90, 0.95, 0.99}
)

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

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

func prometheusMetricName(name string) string {
return strings.Replace(name, "/", "_", -1)
}

func prometheusCounter(name string) *prometheus.CounterVec {
mname := prometheusMetricName(name)

// 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)
} else {
Log.Fatal("Fail to register prometheus.CounterVec: ", err)
}
}
return counter
}

func prometheusHistogram(name string) *prometheus.HistogramVec {
mname := prometheusMetricName(name)

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

err := prometheus.Register(histogram)
if err != nil {
if are, ok := err.(prometheus.AlreadyRegisteredError); ok {
histogram = are.ExistingCollector.(*prometheus.HistogramVec)
} else {
Log.Fatal("Fail to register prometheus.HistogramVec: ", err)
}
isProm: true,
handler: prometheus.InstrumentHandler(name, handler),
}
return histogram
}
3 changes: 1 addition & 2 deletions server/simple_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ 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 @@ -147,7 +146,7 @@ func (s *SimpleServer) Start() error {
s.cfg.Metrics.Path = "/metrics"
}
s.mux.HandleFunc("GET", s.cfg.Metrics.Path,
promhttp.HandlerFor(prometheus.DefaultGatherer, promhttp.HandlerOpts{}).ServeHTTP)
prometheus.InstrumentHandler("prometheus", prometheus.UninstrumentedHandler()))
}

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

0 comments on commit 5cd7239

Please sign in to comment.