use noopRegistry for default global legacy prom registry and expose an http handler

This commit is contained in:
Han Kang 2019-06-10 16:22:27 -07:00
parent 8f4cc7d81f
commit 9c44ba22ca
3 changed files with 40 additions and 13 deletions

View File

@ -11,6 +11,8 @@ go_library(
"//staging/src/k8s.io/apimachinery/pkg/version:go_default_library",
"//staging/src/k8s.io/component-base/metrics:go_default_library",
"//vendor/github.com/prometheus/client_golang/prometheus:go_default_library",
"//vendor/github.com/prometheus/client_golang/prometheus/promhttp:go_default_library",
"//vendor/github.com/prometheus/client_model/go:go_default_library",
],
)

View File

@ -18,17 +18,31 @@ package legacyregistry
import (
"fmt"
"net/http"
"sync"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
dto "github.com/prometheus/client_model/go"
apimachineryversion "k8s.io/apimachinery/pkg/version"
"k8s.io/component-base/metrics"
)
var globalRegistryFactory = metricsRegistryFactory{
registerQueue: make([]metrics.KubeCollector, 0),
mustRegisterQueue: make([]metrics.KubeCollector, 0),
}
var (
globalRegistryFactory = metricsRegistryFactory{
registerQueue: make([]metrics.KubeCollector, 0),
mustRegisterQueue: make([]metrics.KubeCollector, 0),
globalRegistry: noopRegistry{},
}
)
type noopRegistry struct{}
func (noopRegistry) Register(metrics.KubeCollector) error { return nil }
func (noopRegistry) MustRegister(...metrics.KubeCollector) {}
func (noopRegistry) Unregister(metrics.KubeCollector) bool { return true }
func (noopRegistry) Gather() ([]*dto.MetricFamily, error) { return nil, nil }
type metricsRegistryFactory struct {
globalRegistry metrics.KubeRegistry
@ -38,6 +52,13 @@ type metricsRegistryFactory struct {
mustRegisterQueue []metrics.KubeCollector
}
// HandlerForGlobalRegistry returns a http handler for the global registry. This
// allows us to return a handler for the global registry without having to expose
// the global registry itself directly.
func HandlerForGlobalRegistry(opts promhttp.HandlerOpts) http.Handler {
return promhttp.HandlerFor(globalRegistryFactory.globalRegistry, opts)
}
// SetRegistryFactoryVersion sets the kubernetes version information for all
// subsequent metrics registry initializations. Only the first call has an effect.
// If a version is not set, then metrics registry creation will no-opt
@ -77,11 +98,12 @@ func Register(c metrics.KubeCollector) error {
globalRegistryFactory.registrationLock.Lock()
defer globalRegistryFactory.registrationLock.Unlock()
if globalRegistryFactory.kubeVersion != nil {
return globalRegistryFactory.globalRegistry.Register(c)
if globalRegistryFactory.globalRegistry == (noopRegistry{}) {
globalRegistryFactory.registerQueue = append(globalRegistryFactory.registerQueue, c)
return nil
}
globalRegistryFactory.registerQueue = append(globalRegistryFactory.registerQueue, c)
return nil
return globalRegistryFactory.globalRegistry.Register(c)
}
// MustRegister works like Register but registers any number of
@ -91,11 +113,12 @@ func MustRegister(cs ...metrics.KubeCollector) {
globalRegistryFactory.registrationLock.Lock()
defer globalRegistryFactory.registrationLock.Unlock()
if globalRegistryFactory.kubeVersion != nil {
globalRegistryFactory.globalRegistry.MustRegister(cs...)
if globalRegistryFactory.globalRegistry == (noopRegistry{}) {
for _, c := range cs {
globalRegistryFactory.mustRegisterQueue = append(globalRegistryFactory.mustRegisterQueue, c)
}
return
}
for _, c := range cs {
globalRegistryFactory.mustRegisterQueue = append(globalRegistryFactory.mustRegisterQueue, c)
}
globalRegistryFactory.globalRegistry.MustRegister(cs...)
return
}

View File

@ -150,6 +150,7 @@ func TestDeferredRegister(t *testing.T) {
globalRegistryFactory = metricsRegistryFactory{
registerQueue: make([]metrics.KubeCollector, 0),
mustRegisterQueue: make([]metrics.KubeCollector, 0),
globalRegistry: noopRegistry{},
}
var err error
err = Register(alphaDeprecatedCounter)
@ -179,6 +180,7 @@ func TestDeferredMustRegister(t *testing.T) {
globalRegistryFactory = metricsRegistryFactory{
registerQueue: make([]metrics.KubeCollector, 0),
mustRegisterQueue: make([]metrics.KubeCollector, 0),
globalRegistry: noopRegistry{},
}
MustRegister(alphaDeprecatedCounter)