mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
b2db7c8bc9
bump docker/go-metrics v0.0.1: full diff:d466d4f6fd
...v0.0.1 - docker/go-metrics#16 fix the compilation error against prometheus/client-golang master - fixes docker/go-metrics#12 No longer builds against Prom master - docker/go-metrics#18 metrics: address compile error correctly - fixes docker/go-metrics#12 No longer builds against Prom master - docker/go-metrics#15 Add functions that instruments http handler using promhttp - docker/go-metrics#20 Rename LICENSE.code → LICENSE - docker/go-metrics#22 Support Go Modules bump prometheus/client_golang v0.9.4: full diff:c5b7fccd20
...v0.9.4 version v0.9.0 is the minimum required version to work with go-metrics v0.0.1, as it depends on `prometheus.Observer`: vendor/github.com/docker/go-metrics/timer.go:39:4: undefined: prometheus.Observer Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
74 lines
2.3 KiB
Go
74 lines
2.3 KiB
Go
package metrics
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
|
)
|
|
|
|
// HTTPHandlerOpts describes a set of configurable options of http metrics
|
|
type HTTPHandlerOpts struct {
|
|
DurationBuckets []float64
|
|
RequestSizeBuckets []float64
|
|
ResponseSizeBuckets []float64
|
|
}
|
|
|
|
const (
|
|
InstrumentHandlerResponseSize = iota
|
|
InstrumentHandlerRequestSize
|
|
InstrumentHandlerDuration
|
|
InstrumentHandlerCounter
|
|
InstrumentHandlerInFlight
|
|
)
|
|
|
|
type HTTPMetric struct {
|
|
prometheus.Collector
|
|
handlerType int
|
|
}
|
|
|
|
var (
|
|
defaultDurationBuckets = []float64{.005, .01, .025, .05, .1, .25, .5, 1, 2.5, 5, 10, 25, 60}
|
|
defaultRequestSizeBuckets = prometheus.ExponentialBuckets(1024, 2, 22) //1K to 4G
|
|
defaultResponseSizeBuckets = defaultRequestSizeBuckets
|
|
)
|
|
|
|
// Handler returns the global http.Handler that provides the prometheus
|
|
// metrics format on GET requests. This handler is no longer instrumented.
|
|
func Handler() http.Handler {
|
|
return promhttp.Handler()
|
|
}
|
|
|
|
func InstrumentHandler(metrics []*HTTPMetric, handler http.Handler) http.HandlerFunc {
|
|
return InstrumentHandlerFunc(metrics, handler.ServeHTTP)
|
|
}
|
|
|
|
func InstrumentHandlerFunc(metrics []*HTTPMetric, handlerFunc http.HandlerFunc) http.HandlerFunc {
|
|
var handler http.Handler
|
|
handler = http.HandlerFunc(handlerFunc)
|
|
for _, metric := range metrics {
|
|
switch metric.handlerType {
|
|
case InstrumentHandlerResponseSize:
|
|
if collector, ok := metric.Collector.(prometheus.ObserverVec); ok {
|
|
handler = promhttp.InstrumentHandlerResponseSize(collector, handler)
|
|
}
|
|
case InstrumentHandlerRequestSize:
|
|
if collector, ok := metric.Collector.(prometheus.ObserverVec); ok {
|
|
handler = promhttp.InstrumentHandlerRequestSize(collector, handler)
|
|
}
|
|
case InstrumentHandlerDuration:
|
|
if collector, ok := metric.Collector.(prometheus.ObserverVec); ok {
|
|
handler = promhttp.InstrumentHandlerDuration(collector, handler)
|
|
}
|
|
case InstrumentHandlerCounter:
|
|
if collector, ok := metric.Collector.(*prometheus.CounterVec); ok {
|
|
handler = promhttp.InstrumentHandlerCounter(collector, handler)
|
|
}
|
|
case InstrumentHandlerInFlight:
|
|
if collector, ok := metric.Collector.(prometheus.Gauge); ok {
|
|
handler = promhttp.InstrumentHandlerInFlight(collector, handler)
|
|
}
|
|
}
|
|
}
|
|
return handler.ServeHTTP
|
|
}
|