Merge pull request #63009 from mvladev/etcd-disable-automatic-metric-registration

Automatic merge from submit-queue (batch tested with PRs 63009, 63062). If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>.

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.



**What this PR does / why we need it**: Prevents leaking etcd metrics to other k8s components

**Which issue(s) this PR fixes** *(optional, in `fixes #<issue number>(, fixes #<issue_number>, ...)` format, will close the issue(s) when PR gets merged)*:
Partially #63004

**Special notes for your reviewer**:

**Release note**:

```release-note
NONE
```
pull/8/head
Kubernetes Submit Queue 2018-04-26 05:53:09 -07:00 committed by GitHub
commit b181af1c68
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 42 additions and 21 deletions

View File

@ -18,12 +18,12 @@ package metrics
import (
"bufio"
//"fmt"
"net"
"net/http"
"regexp"
"strconv"
"strings"
"sync"
"time"
utilnet "k8s.io/apimachinery/pkg/util/net"
@ -33,6 +33,13 @@ import (
"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 (
// TODO(a-robinson): Add unit tests for the handling of these metrics once
// the upstream library supports it.
@ -96,6 +103,16 @@ var (
[]string{"requestKind"},
)
kubectlExeRegexp = regexp.MustCompile(`^.*((?i:kubectl\.exe))`)
metrics = []resettableCollector{
requestCounter,
longRunningRequestGauge,
requestLatencies,
requestLatenciesSummary,
responseSizes,
DroppedRequests,
currentInflightRequests,
}
)
const (
@ -105,15 +122,22 @@ const (
MutatingKind = "mutating"
)
func init() {
// Register all metrics.
prometheus.MustRegister(requestCounter)
prometheus.MustRegister(longRunningRequestGauge)
prometheus.MustRegister(requestLatencies)
prometheus.MustRegister(requestLatenciesSummary)
prometheus.MustRegister(responseSizes)
prometheus.MustRegister(DroppedRequests)
prometheus.MustRegister(currentInflightRequests)
var registerMetrics sync.Once
// Register all metrics.
func Register() {
registerMetrics.Do(func() {
for _, metric := range metrics {
prometheus.MustRegister(metric)
}
})
}
// Reset all metrics.
func Reset() {
for _, metric := range metrics {
metric.Reset()
}
}
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
// 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 {

View File

@ -32,6 +32,7 @@ type DefaultMetrics struct{}
// Install adds the DefaultMetrics handler
func (m DefaultMetrics) Install(c *mux.PathRecorderMux) {
register()
c.Handle("/metrics", prometheus.Handler())
}
@ -41,6 +42,7 @@ type MetricsWithReset struct{}
// Install adds the MetricsWithReset handler
func (m MetricsWithReset) Install(c *mux.PathRecorderMux) {
register()
defaultMetricsHandler := prometheus.Handler().ServeHTTP
c.HandleFunc("/metrics", func(w http.ResponseWriter, req *http.Request) {
if req.Method == "DELETE" {
@ -52,3 +54,9 @@ func (m MetricsWithReset) Install(c *mux.PathRecorderMux) {
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
}
func init() {
metrics.Register()
}
// Implements storage.Interface.
func (h *etcdHelper) Versioner() storage.Versioner {
return h.versioner