2020-01-14 10:38:55 +00:00
|
|
|
/*
|
|
|
|
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 legacyregistry
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2023-08-17 05:15:28 +00:00
|
|
|
"time"
|
2020-01-14 10:38:55 +00:00
|
|
|
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2023-02-01 17:06:36 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus/collectors"
|
2020-01-14 10:38:55 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
|
|
|
|
|
|
|
"k8s.io/component-base/metrics"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
defaultRegistry = metrics.NewKubeRegistry()
|
|
|
|
// DefaultGatherer exposes the global registry gatherer
|
|
|
|
DefaultGatherer metrics.Gatherer = defaultRegistry
|
2020-12-17 12:28:29 +00:00
|
|
|
// Reset calls reset on the global registry
|
|
|
|
Reset = defaultRegistry.Reset
|
|
|
|
// MustRegister registers registerable metrics but uses the global registry.
|
|
|
|
MustRegister = defaultRegistry.MustRegister
|
|
|
|
// RawMustRegister registers prometheus collectors but uses the global registry, this
|
|
|
|
// bypasses the metric stability framework
|
|
|
|
//
|
|
|
|
// Deprecated
|
|
|
|
RawMustRegister = defaultRegistry.RawMustRegister
|
|
|
|
|
|
|
|
// Register registers a collectable metric but uses the global registry
|
|
|
|
Register = defaultRegistry.Register
|
2023-03-07 00:32:05 +00:00
|
|
|
|
|
|
|
// Registerer exposes the global registerer
|
|
|
|
Registerer = defaultRegistry.Registerer
|
2023-08-17 05:15:28 +00:00
|
|
|
|
|
|
|
processStart time.Time
|
2020-01-14 10:38:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2023-02-01 17:06:36 +00:00
|
|
|
RawMustRegister(collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}))
|
|
|
|
RawMustRegister(collectors.NewGoCollector(collectors.WithGoCollectorRuntimeMetrics(collectors.MetricsAll)))
|
|
|
|
defaultRegistry.RegisterMetaMetrics()
|
2023-08-17 05:15:28 +00:00
|
|
|
processStart = time.Now()
|
2020-01-14 10:38:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Handler returns an HTTP handler for the DefaultGatherer. It is
|
|
|
|
// already instrumented with InstrumentHandler (using "prometheus" as handler
|
|
|
|
// name).
|
|
|
|
func Handler() http.Handler {
|
2023-08-17 05:15:28 +00:00
|
|
|
return promhttp.InstrumentMetricHandler(prometheus.DefaultRegisterer, promhttp.HandlerFor(defaultRegistry, promhttp.HandlerOpts{ProcessStartTime: processStart}))
|
2020-01-14 10:38:55 +00:00
|
|
|
}
|
|
|
|
|
2020-12-17 12:28:29 +00:00
|
|
|
// HandlerWithReset returns an HTTP handler for the DefaultGatherer but invokes
|
|
|
|
// registry reset if the http method is DELETE.
|
|
|
|
func HandlerWithReset() http.Handler {
|
|
|
|
return promhttp.InstrumentMetricHandler(
|
|
|
|
prometheus.DefaultRegisterer,
|
2023-08-17 05:15:28 +00:00
|
|
|
metrics.HandlerWithReset(defaultRegistry, metrics.HandlerOpts{ProcessStartTime: processStart}))
|
2020-01-14 10:38:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CustomRegister registers a custom collector but uses the global registry.
|
|
|
|
func CustomRegister(c metrics.StableCollector) error {
|
|
|
|
err := defaultRegistry.CustomRegister(c)
|
|
|
|
|
|
|
|
//TODO(RainbowMango): Maybe we can wrap this error by error wrapping.(Golang 1.13)
|
|
|
|
_ = prometheus.Register(c)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// CustomMustRegister registers custom collectors but uses the global registry.
|
|
|
|
func CustomMustRegister(cs ...metrics.StableCollector) {
|
|
|
|
defaultRegistry.CustomMustRegister(cs...)
|
|
|
|
|
|
|
|
for _, c := range cs {
|
|
|
|
prometheus.MustRegister(c)
|
|
|
|
}
|
|
|
|
}
|