Skip to content
Permalink
Browse files

move version directly into comp-base/metrics, simplify legacy registy…

… codepaths
  • Loading branch information...
logicalhan committed Aug 13, 2019
1 parent 87e0687 commit 9734ed8e6e4270e7021420b4a3e4733f0843e751
Showing with 323 additions and 385 deletions.
  1. +1 −0 hack/lib/version.sh
  2. +1 −2 staging/src/k8s.io/component-base/go.mod
  3. +8 −2 staging/src/k8s.io/component-base/go.sum
  4. +4 −0 staging/src/k8s.io/component-base/metrics/BUILD
  5. +2 −2 staging/src/k8s.io/component-base/metrics/counter_test.go
  6. +2 −2 staging/src/k8s.io/component-base/metrics/gauge_test.go
  7. +2 −2 staging/src/k8s.io/component-base/metrics/histogram_test.go
  8. +1 −16 staging/src/k8s.io/component-base/metrics/legacyregistry/BUILD
  9. +32 −91 staging/src/k8s.io/component-base/metrics/legacyregistry/registry.go
  10. +0 −227 staging/src/k8s.io/component-base/metrics/legacyregistry/registry_test.go
  11. +2 −3 staging/src/k8s.io/component-base/metrics/prometheus/clientgo/BUILD
  12. +1 −0 staging/src/k8s.io/component-base/metrics/prometheus/clientgo/leaderelection/BUILD
  13. +3 −3 staging/src/k8s.io/component-base/metrics/prometheus/clientgo/leaderelection/metrics.go
  14. +3 −11 staging/src/k8s.io/component-base/metrics/prometheus/clientgo/metrics.go
  15. +1 −0 staging/src/k8s.io/component-base/metrics/prometheus/restclient/BUILD
  16. +5 −5 staging/src/k8s.io/component-base/metrics/prometheus/restclient/metrics.go
  17. +1 −0 staging/src/k8s.io/component-base/metrics/prometheus/workqueue/BUILD
  18. +4 −3 staging/src/k8s.io/component-base/metrics/prometheus/workqueue/metrics.go
  19. +16 −4 staging/src/k8s.io/component-base/metrics/registry.go
  20. +3 −3 staging/src/k8s.io/component-base/metrics/registry_test.go
  21. +2 −2 staging/src/k8s.io/component-base/metrics/summary_test.go
  22. +37 −0 staging/src/k8s.io/component-base/metrics/version.go
  23. +1 −0 staging/src/k8s.io/component-base/metrics/version/.gitattributes
  24. +27 −0 staging/src/k8s.io/component-base/metrics/version/BUILD
  25. +63 −0 staging/src/k8s.io/component-base/metrics/version/base.go
  26. +39 −0 staging/src/k8s.io/component-base/metrics/version/def.bzl
  27. +42 −0 staging/src/k8s.io/component-base/metrics/version/version.go
  28. +0 −1 staging/src/k8s.io/kube-controller-manager/go.mod
  29. +7 −2 staging/src/k8s.io/kube-controller-manager/go.sum
  30. +0 −1 staging/src/k8s.io/kube-proxy/go.mod
  31. +4 −0 staging/src/k8s.io/kube-proxy/go.sum
  32. +1 −1 staging/src/k8s.io/kube-scheduler/go.mod
  33. +7 −2 staging/src/k8s.io/kube-scheduler/go.sum
  34. +1 −0 vendor/modules.txt
@@ -161,6 +161,7 @@ kube::version::ldflags() {
"-X '${KUBE_GO_PACKAGE}/vendor/k8s.io/client-go/pkg/version.${key}=${val}'"
"-X '${KUBE_GO_PACKAGE}/vendor/k8s.io/kubectl/pkg/version.${key}=${val}'"
"-X '${KUBE_GO_PACKAGE}/cmd/kubeadm/app/version.${key}=${val}'"
"-X '${KUBE_GO_PACKAGE}/vendor/k8s.io/component-base/metrics/version.${key}=${val}'"
)
}

@@ -13,14 +13,13 @@ require (
github.com/spf13/pflag v1.0.3
github.com/stretchr/testify v1.3.0
k8s.io/apimachinery v0.0.0
k8s.io/klog v0.4.0
k8s.io/client-go v0.0.0
k8s.io/klog v0.4.0
k8s.io/utils v0.0.0-20190801114015-581e00157fb1
)

replace (
golang.org/x/crypto => golang.org/x/crypto v0.0.0-20181025213731-e84da0312774
golang.org/x/net => golang.org/x/net v0.0.0-20190206173232-65e2d4e15006
golang.org/x/sync => golang.org/x/sync v0.0.0-20181108010431-42b317875d0f
golang.org/x/sys => golang.org/x/sys v0.0.0-20190209173611-3b5209105503
golang.org/x/text => golang.org/x/text v0.3.1-0.20181227161524-e6919f6577db

Some generated files are not rendered by default. Learn more.

@@ -5,6 +5,7 @@ load(
"go_library",
"go_test",
)
load("//staging/src/k8s.io/component-base/metrics/version:def.bzl", "version_x_defs")

go_library(
name = "go_default_library",
@@ -17,13 +18,15 @@ go_library(
"processstarttime.go",
"registry.go",
"summary.go",
"version.go",
"version_parser.go",
"wrappers.go",
],
importmap = "k8s.io/kubernetes/vendor/k8s.io/component-base/metrics",
importpath = "k8s.io/component-base/metrics",
deps = [
"//staging/src/k8s.io/apimachinery/pkg/version:go_default_library",
"//staging/src/k8s.io/component-base/metrics/version:go_default_library",
"//vendor/github.com/blang/semver:go_default_library",
"//vendor/github.com/prometheus/client_golang/prometheus:go_default_library",
"//vendor/github.com/prometheus/client_model/go:go_default_library",
@@ -67,6 +70,7 @@ filegroup(
"//staging/src/k8s.io/component-base/metrics/prometheus/clientgo:all-srcs",
"//staging/src/k8s.io/component-base/metrics/prometheus/restclient:all-srcs",
"//staging/src/k8s.io/component-base/metrics/prometheus/workqueue:all-srcs",
"//staging/src/k8s.io/component-base/metrics/version:all-srcs",
],
tags = ["automanaged"],
)
@@ -74,7 +74,7 @@ func TestCounter(t *testing.T) {

for _, test := range tests {
t.Run(test.desc, func(t *testing.T) {
registry := NewKubeRegistry(apimachineryversion.Info{
registry := newKubeRegistry(apimachineryversion.Info{
Major: "1",
Minor: "15",
GitVersion: "v1.15.0-alpha-1.12345",
@@ -188,7 +188,7 @@ func TestCounterVec(t *testing.T) {

for _, test := range tests {
t.Run(test.desc, func(t *testing.T) {
registry := NewKubeRegistry(apimachineryversion.Info{
registry := newKubeRegistry(apimachineryversion.Info{
Major: "1",
Minor: "15",
GitVersion: "v1.15.0-alpha-1.12345",
@@ -74,7 +74,7 @@ func TestGauge(t *testing.T) {

for _, test := range tests {
t.Run(test.desc, func(t *testing.T) {
registry := NewKubeRegistry(apimachineryversion.Info{
registry := newKubeRegistry(apimachineryversion.Info{
Major: "1",
Minor: "15",
GitVersion: "v1.15.0-alpha-1.12345",
@@ -171,7 +171,7 @@ func TestGaugeVec(t *testing.T) {

for _, test := range tests {
t.Run(test.desc, func(t *testing.T) {
registry := NewKubeRegistry(apimachineryversion.Info{
registry := newKubeRegistry(apimachineryversion.Info{
Major: "1",
Minor: "15",
GitVersion: "v1.15.0-alpha-1.12345",
@@ -78,7 +78,7 @@ func TestHistogram(t *testing.T) {

for _, test := range tests {
t.Run(test.desc, func(t *testing.T) {
registry := NewKubeRegistry(apimachineryversion.Info{
registry := newKubeRegistry(apimachineryversion.Info{
Major: "1",
Minor: "15",
GitVersion: "v1.15.0-alpha-1.12345",
@@ -179,7 +179,7 @@ func TestHistogramVec(t *testing.T) {

for _, test := range tests {
t.Run(test.desc, func(t *testing.T) {
registry := NewKubeRegistry(apimachineryversion.Info{
registry := newKubeRegistry(apimachineryversion.Info{
Major: "1",
Minor: "15",
GitVersion: "v1.15.0-alpha-1.12345",
@@ -1,18 +1,16 @@
package(default_visibility = ["//visibility:public"])

load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
name = "go_default_library",
srcs = ["registry.go"],
importmap = "k8s.io/kubernetes/vendor/k8s.io/component-base/metrics/legacyregistry",
importpath = "k8s.io/component-base/metrics/legacyregistry",
deps = [
"//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",
],
)

@@ -28,16 +26,3 @@ filegroup(
srcs = [":package-srcs"],
tags = ["automanaged"],
)

go_test(
name = "go_default_test",
srcs = ["registry_test.go"],
embed = [":go_default_library"],
deps = [
"//staging/src/k8s.io/apimachinery/pkg/version:go_default_library",
"//staging/src/k8s.io/component-base/metrics:go_default_library",
"//vendor/github.com/blang/semver:go_default_library",
"//vendor/github.com/prometheus/client_golang/prometheus:go_default_library",
"//vendor/github.com/stretchr/testify/assert:go_default_library",
],
)
@@ -17,112 +17,53 @@ limitations under the License.
package legacyregistry

import (
"fmt"
"net/http"
"reflect"
"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"
"net/http"
)

var (
globalRegistryFactory = metricsRegistryFactory{
registerQueue: make([]metrics.Registerable, 0),
mustRegisterQueue: make([]metrics.Registerable, 0),
globalRegistry: noopRegistry{},
}
defaultRegistry = metrics.NewKubeRegistry()
)

type noopRegistry struct{}

func (noopRegistry) Register(metrics.Registerable) error { return nil }
func (noopRegistry) MustRegister(...metrics.Registerable) {}
func (noopRegistry) RawRegister(prometheus.Collector) error { return nil }
func (noopRegistry) RawMustRegister(...prometheus.Collector) {}
func (noopRegistry) Unregister(metrics.Registerable) bool { return true }
func (noopRegistry) Gather() ([]*dto.MetricFamily, error) { return nil, nil }

type metricsRegistryFactory struct {
globalRegistry metrics.KubeRegistry
kubeVersion *apimachineryversion.Info
registrationLock sync.Mutex
registerQueue []metrics.Registerable
mustRegisterQueue []metrics.Registerable
func init() {
RawMustRegister(prometheus.NewProcessCollector(prometheus.ProcessCollectorOpts{}))
RawMustRegister(prometheus.NewGoCollector())
}

// 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)
// Handler returns an HTTP handler for the DefaultGatherer. It is
// already instrumented with InstrumentHandler (using "prometheus" as handler
// name).
//
// Deprecated: Please note the issues described in the doc comment of
// InstrumentHandler. You might want to consider using promhttp.Handler instead.
func Handler() http.Handler {
return prometheus.InstrumentHandler("prometheus", promhttp.HandlerFor(defaultRegistry, promhttp.HandlerOpts{}))
}

// 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
func SetRegistryFactoryVersion(ver apimachineryversion.Info) []error {
globalRegistryFactory.registrationLock.Lock()
defer globalRegistryFactory.registrationLock.Unlock()
if globalRegistryFactory.kubeVersion != nil {
if globalRegistryFactory.kubeVersion.String() != ver.String() {
panic(fmt.Sprintf("Cannot load a global registry more than once, had %s tried to load %s",
globalRegistryFactory.kubeVersion.String(),
ver.String()))
}
return nil
}
registrationErrs := make([]error, 0)
preloadedMetrics := []prometheus.Collector{
prometheus.NewGoCollector(),
prometheus.NewProcessCollector(prometheus.ProcessCollectorOpts{}),
}
globalRegistryFactory.globalRegistry = metrics.NewKubeRegistry(ver)
globalRegistryFactory.globalRegistry.RawMustRegister(preloadedMetrics...)
globalRegistryFactory.kubeVersion = &ver
for _, c := range globalRegistryFactory.registerQueue {
err := globalRegistryFactory.globalRegistry.Register(c)
if err != nil {
registrationErrs = append(registrationErrs, err)
}
}
for _, c := range globalRegistryFactory.mustRegisterQueue {
globalRegistryFactory.globalRegistry.MustRegister(c)
}
return registrationErrs
}

// Register registers a collectable metric, but it uses a global registry. Registration is deferred
// until the global registry has a version to use.
// Register registers a collectable metric but uses the global registry
func Register(c metrics.Registerable) error {
globalRegistryFactory.registrationLock.Lock()
defer globalRegistryFactory.registrationLock.Unlock()

if reflect.DeepEqual(globalRegistryFactory.globalRegistry, noopRegistry{}) {
globalRegistryFactory.registerQueue = append(globalRegistryFactory.registerQueue, c)
return nil
}

return globalRegistryFactory.globalRegistry.Register(c)
return defaultRegistry.Register(c)
}

// MustRegister works like Register but registers any number of
// Collectors and panics upon the first registration that causes an
// error. Registration is deferred until the global registry has a version to use.
// MustRegister registers registerable metrics but uses the global registry.
func MustRegister(cs ...metrics.Registerable) {
globalRegistryFactory.registrationLock.Lock()
defer globalRegistryFactory.registrationLock.Unlock()
defaultRegistry.MustRegister(cs...)
}

// RawMustRegister registers prometheus collectors but uses the global registry, this
// bypasses the metric stability framework
//
// Deprecated
func RawMustRegister(cs ...prometheus.Collector) {
defaultRegistry.RawMustRegister(cs...)
}

if reflect.DeepEqual(globalRegistryFactory.globalRegistry, noopRegistry{}) {
for _, c := range cs {
globalRegistryFactory.mustRegisterQueue = append(globalRegistryFactory.mustRegisterQueue, c)
}
return
}
globalRegistryFactory.globalRegistry.MustRegister(cs...)
return
// RawRegister registers a prometheus collector but uses the global registry, this
// bypasses the metric stability framework
//
// Deprecated
func RawRegister(c prometheus.Collector) error {
return defaultRegistry.RawRegister(c)
}

0 comments on commit 9734ed8

Please sign in to comment.
You can’t perform that action at this time.