@@ -38,14 +38,17 @@ import (
38
38
)
39
39
40
40
const (
41
- defaultMetricsEndpoint = "/metrics"
41
+ DefaultMetricsEndpoint = "/metrics"
42
42
)
43
43
44
44
// DefaultBindAddress is the default bind address for the metrics server.
45
45
var DefaultBindAddress = ":8080"
46
46
47
47
// Server is a server that serves metrics.
48
48
type Server interface {
49
+ // AddExtraHandler adds extra handler served on path to the http server that serves metrics.
50
+ AddExtraHandler (path string , handler http.Handler ) error
51
+
49
52
// NeedLeaderElection implements the LeaderElectionRunnable interface, which indicates
50
53
// the metrics server doesn't need leader election.
51
54
NeedLeaderElection () bool
@@ -120,8 +123,8 @@ func NewServer(o Options, config *rest.Config, httpClient *http.Client) (Server,
120
123
121
124
// Validate that ExtraHandlers is not overwriting the default /metrics endpoint.
122
125
if o .ExtraHandlers != nil {
123
- if _ , ok := o .ExtraHandlers [defaultMetricsEndpoint ]; ok {
124
- return nil , fmt .Errorf ("overriding builtin %s endpoint is not allowed" , defaultMetricsEndpoint )
126
+ if _ , ok := o .ExtraHandlers [DefaultMetricsEndpoint ]; ok {
127
+ return nil , fmt .Errorf ("overriding builtin %s endpoint is not allowed" , DefaultMetricsEndpoint )
125
128
}
126
129
}
127
130
@@ -182,6 +185,20 @@ func (*defaultServer) NeedLeaderElection() bool {
182
185
return false
183
186
}
184
187
188
+ // AddMetricsExtraHandler adds extra handler served on path to the http server that serves metrics.
189
+ func (s * defaultServer ) AddExtraHandler (path string , handler http.Handler ) error {
190
+ s .mu .Lock ()
191
+ defer s .mu .Unlock ()
192
+ if s .options .ExtraHandlers == nil {
193
+ s .options .ExtraHandlers = make (map [string ]http.Handler )
194
+ }
195
+ if _ , found := s .options .ExtraHandlers [path ]; found {
196
+ return fmt .Errorf ("can't register extra handler by duplicate path %q on metrics http server" , path )
197
+ }
198
+ s .options .ExtraHandlers [path ] = handler
199
+ return nil
200
+ }
201
+
185
202
// Start runs the server.
186
203
// It will install the metrics related resources depend on the server configuration.
187
204
func (s * defaultServer ) Start (ctx context.Context ) error {
@@ -202,15 +219,15 @@ func (s *defaultServer) Start(ctx context.Context) error {
202
219
ErrorHandling : promhttp .HTTPErrorOnError ,
203
220
})
204
221
if s .metricsFilter != nil {
205
- log := log .WithValues ("path" , defaultMetricsEndpoint )
222
+ log := log .WithValues ("path" , DefaultMetricsEndpoint )
206
223
var err error
207
224
handler , err = s .metricsFilter (log , handler )
208
225
if err != nil {
209
226
return fmt .Errorf ("failed to start metrics server: failed to add metrics filter: %w" , err )
210
227
}
211
228
}
212
229
// TODO(JoelSpeed): Use existing Kubernetes machinery for serving metrics
213
- mux .Handle (defaultMetricsEndpoint , handler )
230
+ mux .Handle (DefaultMetricsEndpoint , handler )
214
231
215
232
for path , extraHandler := range s .options .ExtraHandlers {
216
233
if s .metricsFilter != nil {
0 commit comments