mirror of https://github.com/k3s-io/k3s
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
97 lines
3.5 KiB
97 lines
3.5 KiB
/*
|
|
Copyright 2019 The Kubernetes Authors.
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
package metrics
|
|
|
|
import (
|
|
"github.com/blang/semver"
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
dto "github.com/prometheus/client_model/go"
|
|
apimachineryversion "k8s.io/apimachinery/pkg/version"
|
|
)
|
|
|
|
// KubeRegistry is an interface which implements a subset of prometheus.Registerer and
|
|
// prometheus.Gatherer interfaces
|
|
type KubeRegistry interface {
|
|
Register(KubeCollector) error
|
|
MustRegister(...KubeCollector)
|
|
Unregister(KubeCollector) bool
|
|
Gather() ([]*dto.MetricFamily, error)
|
|
}
|
|
|
|
// kubeRegistry is a wrapper around a prometheus registry-type object. Upon initialization
|
|
// the kubernetes binary version information is loaded into the registry object, so that
|
|
// automatic behavior can be configured for metric versioning.
|
|
type kubeRegistry struct {
|
|
PromRegistry
|
|
version semver.Version
|
|
}
|
|
|
|
// Register registers a new Collector to be included in metrics
|
|
// collection. It returns an error if the descriptors provided by the
|
|
// Collector are invalid or if they — in combination with descriptors of
|
|
// already registered Collectors — do not fulfill the consistency and
|
|
// uniqueness criteria described in the documentation of metric.Desc.
|
|
func (kr *kubeRegistry) Register(c KubeCollector) error {
|
|
if c.Create(&kr.version) {
|
|
return kr.PromRegistry.Register(c)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// MustRegister works like Register but registers any number of
|
|
// Collectors and panics upon the first registration that causes an
|
|
// error.
|
|
func (kr *kubeRegistry) MustRegister(cs ...KubeCollector) {
|
|
metrics := make([]prometheus.Collector, 0, len(cs))
|
|
for _, c := range cs {
|
|
if c.Create(&kr.version) {
|
|
metrics = append(metrics, c)
|
|
}
|
|
}
|
|
kr.PromRegistry.MustRegister(metrics...)
|
|
}
|
|
|
|
// Unregister unregisters the Collector that equals the Collector passed
|
|
// in as an argument. (Two Collectors are considered equal if their
|
|
// Describe method yields the same set of descriptors.) The function
|
|
// returns whether a Collector was unregistered. Note that an unchecked
|
|
// Collector cannot be unregistered (as its Describe method does not
|
|
// yield any descriptor).
|
|
func (kr *kubeRegistry) Unregister(collector KubeCollector) bool {
|
|
return kr.PromRegistry.Unregister(collector)
|
|
}
|
|
|
|
// Gather calls the Collect method of the registered Collectors and then
|
|
// gathers the collected metrics into a lexicographically sorted slice
|
|
// of uniquely named MetricFamily protobufs. Gather ensures that the
|
|
// returned slice is valid and self-consistent so that it can be used
|
|
// for valid exposition. As an exception to the strict consistency
|
|
// requirements described for metric.Desc, Gather will tolerate
|
|
// different sets of label names for metrics of the same metric family.
|
|
func (kr *kubeRegistry) Gather() ([]*dto.MetricFamily, error) {
|
|
return kr.PromRegistry.Gather()
|
|
}
|
|
|
|
// NewKubeRegistry creates a new vanilla Registry without any Collectors
|
|
// pre-registered.
|
|
func NewKubeRegistry(v apimachineryversion.Info) KubeRegistry {
|
|
return &kubeRegistry{
|
|
PromRegistry: prometheus.NewRegistry(),
|
|
version: parseVersion(v),
|
|
}
|
|
}
|