vendor: bump docker/go-metrics v0.0.1:
full diff: 4ea375f775...v0.0.1
- 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
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
			
			
This commit is contained in:
		
							
								
								
									
										12
									
								
								vendor/github.com/docker/go-metrics/README.md
									
									
									
										generated
									
									
										vendored
									
									
								
							
							
						
						
									
										12
									
								
								vendor/github.com/docker/go-metrics/README.md
									
									
									
										generated
									
									
										vendored
									
									
								
							@@ -68,6 +68,18 @@ If you need to use a unit but it is not defined in the package please open a PR
 | 
			
		||||
 | 
			
		||||
Package documentation can be found [here](https://godoc.org/github.com/docker/go-metrics).
 | 
			
		||||
 | 
			
		||||
## HTTP Metrics
 | 
			
		||||
 | 
			
		||||
To instrument a http handler, you can wrap the code like this:
 | 
			
		||||
 | 
			
		||||
```go
 | 
			
		||||
namespace := metrics.NewNamespace("docker_distribution", "http", metrics.Labels{"handler": "your_http_handler_name"})
 | 
			
		||||
httpMetrics := namespace.NewDefaultHttpMetrics()
 | 
			
		||||
metrics.Register(namespace)
 | 
			
		||||
instrumentedHandler = metrics.InstrumentHandler(httpMetrics, unInstrumentedHandler)
 | 
			
		||||
```
 | 
			
		||||
Note: The `handler` label must be provided when a new namespace is created.
 | 
			
		||||
 | 
			
		||||
## Additional Metrics
 | 
			
		||||
 | 
			
		||||
Additional metrics are also defined here that are not available in the prometheus client.
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										5
									
								
								vendor/github.com/docker/go-metrics/go.mod
									
									
									
										generated
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										5
									
								
								vendor/github.com/docker/go-metrics/go.mod
									
									
									
										generated
									
									
										vendored
									
									
										Normal file
									
								
							@@ -0,0 +1,5 @@
 | 
			
		||||
module github.com/docker/go-metrics
 | 
			
		||||
 | 
			
		||||
go 1.11
 | 
			
		||||
 | 
			
		||||
require github.com/prometheus/client_golang v1.1.0
 | 
			
		||||
							
								
								
									
										65
									
								
								vendor/github.com/docker/go-metrics/handler.go
									
									
									
										generated
									
									
										vendored
									
									
								
							
							
						
						
									
										65
									
								
								vendor/github.com/docker/go-metrics/handler.go
									
									
									
										generated
									
									
										vendored
									
									
								
							@@ -4,10 +4,71 @@ 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
 | 
			
		||||
// metrics format on GET requests. This handler is no longer instrumented.
 | 
			
		||||
func Handler() http.Handler {
 | 
			
		||||
	return prometheus.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
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										134
									
								
								vendor/github.com/docker/go-metrics/namespace.go
									
									
									
										generated
									
									
										vendored
									
									
								
							
							
						
						
									
										134
									
								
								vendor/github.com/docker/go-metrics/namespace.go
									
									
									
										generated
									
									
										vendored
									
									
								
							@@ -179,3 +179,137 @@ func makeName(name string, unit Unit) string {
 | 
			
		||||
 | 
			
		||||
	return fmt.Sprintf("%s_%s", name, unit)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (n *Namespace) NewDefaultHttpMetrics(handlerName string) []*HTTPMetric {
 | 
			
		||||
	return n.NewHttpMetricsWithOpts(handlerName, HTTPHandlerOpts{
 | 
			
		||||
		DurationBuckets:     defaultDurationBuckets,
 | 
			
		||||
		RequestSizeBuckets:  defaultResponseSizeBuckets,
 | 
			
		||||
		ResponseSizeBuckets: defaultResponseSizeBuckets,
 | 
			
		||||
	})
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (n *Namespace) NewHttpMetrics(handlerName string, durationBuckets, requestSizeBuckets, responseSizeBuckets []float64) []*HTTPMetric {
 | 
			
		||||
	return n.NewHttpMetricsWithOpts(handlerName, HTTPHandlerOpts{
 | 
			
		||||
		DurationBuckets:     durationBuckets,
 | 
			
		||||
		RequestSizeBuckets:  requestSizeBuckets,
 | 
			
		||||
		ResponseSizeBuckets: responseSizeBuckets,
 | 
			
		||||
	})
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (n *Namespace) NewHttpMetricsWithOpts(handlerName string, opts HTTPHandlerOpts) []*HTTPMetric {
 | 
			
		||||
	var httpMetrics []*HTTPMetric
 | 
			
		||||
	inFlightMetric := n.NewInFlightGaugeMetric(handlerName)
 | 
			
		||||
	requestTotalMetric := n.NewRequestTotalMetric(handlerName)
 | 
			
		||||
	requestDurationMetric := n.NewRequestDurationMetric(handlerName, opts.DurationBuckets)
 | 
			
		||||
	requestSizeMetric := n.NewRequestSizeMetric(handlerName, opts.RequestSizeBuckets)
 | 
			
		||||
	responseSizeMetric := n.NewResponseSizeMetric(handlerName, opts.ResponseSizeBuckets)
 | 
			
		||||
	httpMetrics = append(httpMetrics, inFlightMetric, requestDurationMetric, requestTotalMetric, requestSizeMetric, responseSizeMetric)
 | 
			
		||||
	return httpMetrics
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (n *Namespace) NewInFlightGaugeMetric(handlerName string) *HTTPMetric {
 | 
			
		||||
	labels := prometheus.Labels(n.labels)
 | 
			
		||||
	labels["handler"] = handlerName
 | 
			
		||||
	metric := prometheus.NewGauge(prometheus.GaugeOpts{
 | 
			
		||||
		Namespace:   n.name,
 | 
			
		||||
		Subsystem:   n.subsystem,
 | 
			
		||||
		Name:        "in_flight_requests",
 | 
			
		||||
		Help:        "The in-flight HTTP requests",
 | 
			
		||||
		ConstLabels: prometheus.Labels(labels),
 | 
			
		||||
	})
 | 
			
		||||
	httpMetric := &HTTPMetric{
 | 
			
		||||
		Collector:   metric,
 | 
			
		||||
		handlerType: InstrumentHandlerInFlight,
 | 
			
		||||
	}
 | 
			
		||||
	n.Add(httpMetric)
 | 
			
		||||
	return httpMetric
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (n *Namespace) NewRequestTotalMetric(handlerName string) *HTTPMetric {
 | 
			
		||||
	labels := prometheus.Labels(n.labels)
 | 
			
		||||
	labels["handler"] = handlerName
 | 
			
		||||
	metric := prometheus.NewCounterVec(
 | 
			
		||||
		prometheus.CounterOpts{
 | 
			
		||||
			Namespace:   n.name,
 | 
			
		||||
			Subsystem:   n.subsystem,
 | 
			
		||||
			Name:        "requests_total",
 | 
			
		||||
			Help:        "Total number of HTTP requests made.",
 | 
			
		||||
			ConstLabels: prometheus.Labels(labels),
 | 
			
		||||
		},
 | 
			
		||||
		[]string{"code", "method"},
 | 
			
		||||
	)
 | 
			
		||||
	httpMetric := &HTTPMetric{
 | 
			
		||||
		Collector:   metric,
 | 
			
		||||
		handlerType: InstrumentHandlerCounter,
 | 
			
		||||
	}
 | 
			
		||||
	n.Add(httpMetric)
 | 
			
		||||
	return httpMetric
 | 
			
		||||
}
 | 
			
		||||
func (n *Namespace) NewRequestDurationMetric(handlerName string, buckets []float64) *HTTPMetric {
 | 
			
		||||
	if len(buckets) == 0 {
 | 
			
		||||
		panic("DurationBuckets must be provided")
 | 
			
		||||
	}
 | 
			
		||||
	labels := prometheus.Labels(n.labels)
 | 
			
		||||
	labels["handler"] = handlerName
 | 
			
		||||
	opts := prometheus.HistogramOpts{
 | 
			
		||||
		Namespace:   n.name,
 | 
			
		||||
		Subsystem:   n.subsystem,
 | 
			
		||||
		Name:        "request_duration_seconds",
 | 
			
		||||
		Help:        "The HTTP request latencies in seconds.",
 | 
			
		||||
		Buckets:     buckets,
 | 
			
		||||
		ConstLabels: prometheus.Labels(labels),
 | 
			
		||||
	}
 | 
			
		||||
	metric := prometheus.NewHistogramVec(opts, []string{"method"})
 | 
			
		||||
	httpMetric := &HTTPMetric{
 | 
			
		||||
		Collector:   metric,
 | 
			
		||||
		handlerType: InstrumentHandlerDuration,
 | 
			
		||||
	}
 | 
			
		||||
	n.Add(httpMetric)
 | 
			
		||||
	return httpMetric
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (n *Namespace) NewRequestSizeMetric(handlerName string, buckets []float64) *HTTPMetric {
 | 
			
		||||
	if len(buckets) == 0 {
 | 
			
		||||
		panic("RequestSizeBuckets must be provided")
 | 
			
		||||
	}
 | 
			
		||||
	labels := prometheus.Labels(n.labels)
 | 
			
		||||
	labels["handler"] = handlerName
 | 
			
		||||
	opts := prometheus.HistogramOpts{
 | 
			
		||||
		Namespace:   n.name,
 | 
			
		||||
		Subsystem:   n.subsystem,
 | 
			
		||||
		Name:        "request_size_bytes",
 | 
			
		||||
		Help:        "The HTTP request sizes in bytes.",
 | 
			
		||||
		Buckets:     buckets,
 | 
			
		||||
		ConstLabels: prometheus.Labels(labels),
 | 
			
		||||
	}
 | 
			
		||||
	metric := prometheus.NewHistogramVec(opts, []string{})
 | 
			
		||||
	httpMetric := &HTTPMetric{
 | 
			
		||||
		Collector:   metric,
 | 
			
		||||
		handlerType: InstrumentHandlerRequestSize,
 | 
			
		||||
	}
 | 
			
		||||
	n.Add(httpMetric)
 | 
			
		||||
	return httpMetric
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (n *Namespace) NewResponseSizeMetric(handlerName string, buckets []float64) *HTTPMetric {
 | 
			
		||||
	if len(buckets) == 0 {
 | 
			
		||||
		panic("ResponseSizeBuckets must be provided")
 | 
			
		||||
	}
 | 
			
		||||
	labels := prometheus.Labels(n.labels)
 | 
			
		||||
	labels["handler"] = handlerName
 | 
			
		||||
	opts := prometheus.HistogramOpts{
 | 
			
		||||
		Namespace:   n.name,
 | 
			
		||||
		Subsystem:   n.subsystem,
 | 
			
		||||
		Name:        "response_size_bytes",
 | 
			
		||||
		Help:        "The HTTP response sizes in bytes.",
 | 
			
		||||
		Buckets:     buckets,
 | 
			
		||||
		ConstLabels: prometheus.Labels(labels),
 | 
			
		||||
	}
 | 
			
		||||
	metrics := prometheus.NewHistogramVec(opts, []string{})
 | 
			
		||||
	httpMetric := &HTTPMetric{
 | 
			
		||||
		Collector:   metrics,
 | 
			
		||||
		handlerType: InstrumentHandlerResponseSize,
 | 
			
		||||
	}
 | 
			
		||||
	n.Add(httpMetric)
 | 
			
		||||
	return httpMetric
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user