Register Prometheus etcdmetrics only for apiserver

Removed automatic registration with `init` funciton and use `Register` function
to register metrics for etcd storage only when requested.
This commit is contained in:
Martin Vladev 2018-04-20 17:19:13 +03:00
parent 2bfe40a1d1
commit 40cf788013
3 changed files with 42 additions and 21 deletions

View File

@ -18,12 +18,12 @@ package metrics
import ( import (
"bufio" "bufio"
//"fmt"
"net" "net"
"net/http" "net/http"
"regexp" "regexp"
"strconv" "strconv"
"strings" "strings"
"sync"
"time" "time"
utilnet "k8s.io/apimachinery/pkg/util/net" utilnet "k8s.io/apimachinery/pkg/util/net"
@ -33,6 +33,13 @@ import (
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
) )
// resettableCollector is the interface implemented by prometheus.MetricVec
// that can be used by Prometheus to collect metrics and reset their values.
type resettableCollector interface {
prometheus.Collector
Reset()
}
var ( var (
// TODO(a-robinson): Add unit tests for the handling of these metrics once // TODO(a-robinson): Add unit tests for the handling of these metrics once
// the upstream library supports it. // the upstream library supports it.
@ -96,6 +103,16 @@ var (
[]string{"requestKind"}, []string{"requestKind"},
) )
kubectlExeRegexp = regexp.MustCompile(`^.*((?i:kubectl\.exe))`) kubectlExeRegexp = regexp.MustCompile(`^.*((?i:kubectl\.exe))`)
metrics = []resettableCollector{
requestCounter,
longRunningRequestGauge,
requestLatencies,
requestLatenciesSummary,
responseSizes,
DroppedRequests,
currentInflightRequests,
}
) )
const ( const (
@ -105,15 +122,22 @@ const (
MutatingKind = "mutating" MutatingKind = "mutating"
) )
func init() { var registerMetrics sync.Once
// Register all metrics.
prometheus.MustRegister(requestCounter) // Register all metrics.
prometheus.MustRegister(longRunningRequestGauge) func Register() {
prometheus.MustRegister(requestLatencies) registerMetrics.Do(func() {
prometheus.MustRegister(requestLatenciesSummary) for _, metric := range metrics {
prometheus.MustRegister(responseSizes) prometheus.MustRegister(metric)
prometheus.MustRegister(DroppedRequests) }
prometheus.MustRegister(currentInflightRequests) })
}
// Reset all metrics.
func Reset() {
for _, metric := range metrics {
metric.Reset()
}
} }
func UpdateInflightRequestMetrics(nonmutating, mutating int) { func UpdateInflightRequestMetrics(nonmutating, mutating int) {
@ -170,13 +194,6 @@ func MonitorRequest(req *http.Request, verb, resource, subresource, scope, conte
} }
} }
func Reset() {
requestCounter.Reset()
requestLatencies.Reset()
requestLatenciesSummary.Reset()
responseSizes.Reset()
}
// InstrumentRouteFunc works like Prometheus' InstrumentHandlerFunc but wraps // InstrumentRouteFunc works like Prometheus' InstrumentHandlerFunc but wraps
// the go-restful RouteFunction instead of a HandlerFunc plus some Kubernetes endpoint specific information. // the go-restful RouteFunction instead of a HandlerFunc plus some Kubernetes endpoint specific information.
func InstrumentRouteFunc(verb, resource, subresource, scope string, routeFunc restful.RouteFunction) restful.RouteFunction { func InstrumentRouteFunc(verb, resource, subresource, scope string, routeFunc restful.RouteFunction) restful.RouteFunction {

View File

@ -32,6 +32,7 @@ type DefaultMetrics struct{}
// Install adds the DefaultMetrics handler // Install adds the DefaultMetrics handler
func (m DefaultMetrics) Install(c *mux.PathRecorderMux) { func (m DefaultMetrics) Install(c *mux.PathRecorderMux) {
register()
c.Handle("/metrics", prometheus.Handler()) c.Handle("/metrics", prometheus.Handler())
} }
@ -41,6 +42,7 @@ type MetricsWithReset struct{}
// Install adds the MetricsWithReset handler // Install adds the MetricsWithReset handler
func (m MetricsWithReset) Install(c *mux.PathRecorderMux) { func (m MetricsWithReset) Install(c *mux.PathRecorderMux) {
register()
defaultMetricsHandler := prometheus.Handler().ServeHTTP defaultMetricsHandler := prometheus.Handler().ServeHTTP
c.HandleFunc("/metrics", func(w http.ResponseWriter, req *http.Request) { c.HandleFunc("/metrics", func(w http.ResponseWriter, req *http.Request) {
if req.Method == "DELETE" { if req.Method == "DELETE" {
@ -52,3 +54,9 @@ func (m MetricsWithReset) Install(c *mux.PathRecorderMux) {
defaultMetricsHandler(w, req) defaultMetricsHandler(w, req)
}) })
} }
// register apiserver and etcd metrics
func register() {
apimetrics.Register()
etcdmetrics.Register()
}

View File

@ -101,10 +101,6 @@ type etcdHelper struct {
cache utilcache.Cache cache utilcache.Cache
} }
func init() {
metrics.Register()
}
// Implements storage.Interface. // Implements storage.Interface.
func (h *etcdHelper) Versioner() storage.Versioner { func (h *etcdHelper) Versioner() storage.Versioner {
return h.versioner return h.versioner