@@ -17,112 +17,69 @@ limitations under the License.
17
17
package legacyregistry
18
18
19
19
import (
20
- "fmt"
21
- "net/http"
22
- "reflect"
23
- "sync"
24
-
25
20
"github.com/prometheus/client_golang/prometheus"
26
21
"github.com/prometheus/client_golang/prometheus/promhttp"
27
- dto "github.com/prometheus/client_model/go"
28
-
29
- apimachineryversion "k8s.io/apimachinery/pkg/version"
30
22
"k8s.io/component-base/metrics"
23
+ "net/http"
31
24
)
32
25
33
26
var (
34
- globalRegistryFactory = metricsRegistryFactory {
35
- registerQueue : make ([]metrics.Registerable , 0 ),
36
- mustRegisterQueue : make ([]metrics.Registerable , 0 ),
37
- globalRegistry : noopRegistry {},
38
- }
27
+ defaultRegistry = metrics .NewKubeRegistry ()
28
+ // DefaultGatherer exposes the global registry gatherer
29
+ DefaultGatherer prometheus.Gatherer = defaultRegistry
39
30
)
40
31
41
- type noopRegistry struct {}
42
-
43
- func (noopRegistry ) Register (metrics.Registerable ) error { return nil }
44
- func (noopRegistry ) MustRegister (... metrics.Registerable ) {}
45
- func (noopRegistry ) RawRegister (prometheus.Collector ) error { return nil }
46
- func (noopRegistry ) RawMustRegister (... prometheus.Collector ) {}
47
- func (noopRegistry ) Unregister (metrics.Registerable ) bool { return true }
48
- func (noopRegistry ) Gather () ([]* dto.MetricFamily , error ) { return nil , nil }
32
+ func init () {
33
+ RawMustRegister (prometheus .NewProcessCollector (prometheus.ProcessCollectorOpts {}))
34
+ RawMustRegister (prometheus .NewGoCollector ())
35
+ }
49
36
50
- type metricsRegistryFactory struct {
51
- globalRegistry metrics.KubeRegistry
52
- kubeVersion * apimachineryversion.Info
53
- registrationLock sync.Mutex
54
- registerQueue []metrics.Registerable
55
- mustRegisterQueue []metrics.Registerable
37
+ // Handler returns an HTTP handler for the DefaultGatherer. It is
38
+ // already instrumented with InstrumentHandler (using "prometheus" as handler
39
+ // name).
40
+ //
41
+ // Deprecated: Please note the issues described in the doc comment of
42
+ // InstrumentHandler. You might want to consider using promhttp.Handler instead.
43
+ func Handler () http.Handler {
44
+ return prometheus .InstrumentHandler ("prometheus" , promhttp .HandlerFor (defaultRegistry , promhttp.HandlerOpts {}))
56
45
}
57
46
58
- // HandlerForGlobalRegistry returns a http handler for the global registry. This
59
- // allows us to return a handler for the global registry without having to expose
60
- // the global registry itself directly.
61
- func HandlerForGlobalRegistry (opts promhttp.HandlerOpts ) http.Handler {
62
- return promhttp .HandlerFor (globalRegistryFactory .globalRegistry , opts )
47
+ // Register registers a collectable metric but uses the global registry
48
+ func Register (c metrics.Registerable ) error {
49
+ err := defaultRegistry .Register (c )
50
+ // sideload global prom registry as fallback
51
+ prometheus .Register (c )
52
+ return err
63
53
}
64
54
65
- // SetRegistryFactoryVersion sets the kubernetes version information for all
66
- // subsequent metrics registry initializations. Only the first call has an effect.
67
- // If a version is not set, then metrics registry creation will no-opt
68
- func SetRegistryFactoryVersion (ver apimachineryversion.Info ) []error {
69
- globalRegistryFactory .registrationLock .Lock ()
70
- defer globalRegistryFactory .registrationLock .Unlock ()
71
- if globalRegistryFactory .kubeVersion != nil {
72
- if globalRegistryFactory .kubeVersion .String () != ver .String () {
73
- panic (fmt .Sprintf ("Cannot load a global registry more than once, had %s tried to load %s" ,
74
- globalRegistryFactory .kubeVersion .String (),
75
- ver .String ()))
76
- }
77
- return nil
78
- }
79
- registrationErrs := make ([]error , 0 )
80
- preloadedMetrics := []prometheus.Collector {
81
- prometheus .NewGoCollector (),
82
- prometheus .NewProcessCollector (prometheus.ProcessCollectorOpts {}),
83
- }
84
- globalRegistryFactory .globalRegistry = metrics .NewKubeRegistry (ver )
85
- globalRegistryFactory .globalRegistry .RawMustRegister (preloadedMetrics ... )
86
- globalRegistryFactory .kubeVersion = & ver
87
- for _ , c := range globalRegistryFactory .registerQueue {
88
- err := globalRegistryFactory .globalRegistry .Register (c )
89
- if err != nil {
90
- registrationErrs = append (registrationErrs , err )
91
- }
92
- }
93
- for _ , c := range globalRegistryFactory .mustRegisterQueue {
94
- globalRegistryFactory .globalRegistry .MustRegister (c )
55
+ // MustRegister registers registerable metrics but uses the global registry.
56
+ func MustRegister (cs ... metrics.Registerable ) {
57
+ defaultRegistry .MustRegister (cs ... )
58
+ // sideload global prom registry as fallback
59
+ for _ , c := range cs {
60
+ prometheus .Register (c )
95
61
}
96
- return registrationErrs
97
62
}
98
63
99
- // Register registers a collectable metric, but it uses a global registry. Registration is deferred
100
- // until the global registry has a version to use.
101
- func Register ( c metrics. Registerable ) error {
102
- globalRegistryFactory . registrationLock . Lock ()
103
- defer globalRegistryFactory . registrationLock . Unlock ()
104
-
105
- if reflect . DeepEqual ( globalRegistryFactory . globalRegistry , noopRegistry {}) {
106
- globalRegistryFactory . registerQueue = append ( globalRegistryFactory . registerQueue , c )
107
- return nil
64
+ // RawMustRegister registers prometheus collectors but uses the global registry, this
65
+ // bypasses the metric stability framework
66
+ //
67
+ // Deprecated
68
+ func RawMustRegister ( cs ... prometheus. Collector ) {
69
+ defaultRegistry . RawMustRegister ( cs ... )
70
+ // sideload global prom registry as fallback
71
+ for _ , c := range cs {
72
+ prometheus . Register ( c )
108
73
}
109
-
110
- return globalRegistryFactory .globalRegistry .Register (c )
111
74
}
112
75
113
- // MustRegister works like Register but registers any number of
114
- // Collectors and panics upon the first registration that causes an
115
- // error. Registration is deferred until the global registry has a version to use.
116
- func MustRegister (cs ... metrics.Registerable ) {
117
- globalRegistryFactory .registrationLock .Lock ()
118
- defer globalRegistryFactory .registrationLock .Unlock ()
119
-
120
- if reflect .DeepEqual (globalRegistryFactory .globalRegistry , noopRegistry {}) {
121
- for _ , c := range cs {
122
- globalRegistryFactory .mustRegisterQueue = append (globalRegistryFactory .mustRegisterQueue , c )
123
- }
124
- return
125
- }
126
- globalRegistryFactory .globalRegistry .MustRegister (cs ... )
127
- return
76
+ // RawRegister registers a prometheus collector but uses the global registry, this
77
+ // bypasses the metric stability framework
78
+ //
79
+ // Deprecated
80
+ func RawRegister (c prometheus.Collector ) error {
81
+ err := defaultRegistry .RawRegister (c )
82
+ // sideload global prom registry as fallback
83
+ prometheus .Register (c )
84
+ return err
128
85
}
0 commit comments