Skip to content

Commit 6396a49

Browse files
vinceprik8s-infra-cherrypick-robot
authored and
k8s-infra-cherrypick-robot
committed
Reintroduce AddMetricsExtraHandler on manager
Signed-off-by: Vince Prignano <vince@prigna.com>
1 parent cd2885e commit 6396a49

File tree

4 files changed

+53
-0
lines changed

4 files changed

+53
-0
lines changed

pkg/manager/internal.go

+18
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,24 @@ func (cm *controllerManager) add(r Runnable) error {
179179
return cm.runnables.Add(r)
180180
}
181181

182+
// AddMetricsServerExtraHandler adds extra handler served on path to the http server that serves metrics.
183+
func (cm *controllerManager) AddMetricsServerExtraHandler(path string, handler http.Handler) error {
184+
cm.Lock()
185+
defer cm.Unlock()
186+
if cm.started {
187+
return fmt.Errorf("unable to add new metrics handler because metrics endpoint has already been created")
188+
}
189+
if cm.metricsServer == nil {
190+
cm.GetLogger().Info("warn: metrics server is currently disabled, registering extra handler %q will be ignored", path)
191+
return nil
192+
}
193+
if err := cm.metricsServer.AddExtraHandler(path, handler); err != nil {
194+
return err
195+
}
196+
cm.logger.V(2).Info("Registering metrics http server extra handler", "path", path)
197+
return nil
198+
}
199+
182200
// AddHealthzCheck allows you to add Healthz checker.
183201
func (cm *controllerManager) AddHealthzCheck(name string, check healthz.Checker) error {
184202
cm.Lock()

pkg/manager/manager.go

+9
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,15 @@ type Manager interface {
6464
// election was configured.
6565
Elected() <-chan struct{}
6666

67+
// AddMetricsServerExtraHandler adds an extra handler served on path to the http server that serves metrics.
68+
// Might be useful to register some diagnostic endpoints e.g. pprof.
69+
//
70+
// Note that these endpoints are meant to be sensitive and shouldn't be exposed publicly.
71+
//
72+
// If the simple path -> handler mapping offered here is not enough,
73+
// a new http server/listener should be added as Runnable to the manager via Add method.
74+
AddMetricsServerExtraHandler(path string, handler http.Handler) error
75+
6776
// AddHealthzCheck allows you to add Healthz checker
6877
AddHealthzCheck(name string, check healthz.Checker) error
6978

pkg/manager/manager_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -1312,6 +1312,12 @@ var _ = Describe("manger.Manager", func() {
13121312
m, err := New(cfg, opts)
13131313
Expect(err).NotTo(HaveOccurred())
13141314

1315+
// Should error when we add another extra endpoint on the already registered path.
1316+
err = m.AddMetricsServerExtraHandler("/debug", http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
1317+
_, _ = w.Write([]byte("Another debug info"))
1318+
}))
1319+
Expect(err).To(HaveOccurred())
1320+
13151321
ctx, cancel := context.WithCancel(context.Background())
13161322
defer cancel()
13171323
go func() {

pkg/metrics/server/server.go

+20
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ var DefaultBindAddress = ":8080"
4646

4747
// Server is a server that serves metrics.
4848
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+
4952
// NeedLeaderElection implements the LeaderElectionRunnable interface, which indicates
5053
// the metrics server doesn't need leader election.
5154
NeedLeaderElection() bool
@@ -182,6 +185,23 @@ func (*defaultServer) NeedLeaderElection() bool {
182185
return false
183186
}
184187

188+
// AddExtraHandler 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 path == defaultMetricsEndpoint {
196+
return fmt.Errorf("overriding builtin %s endpoint is not allowed", defaultMetricsEndpoint)
197+
}
198+
if _, found := s.options.ExtraHandlers[path]; found {
199+
return fmt.Errorf("can't register extra handler by duplicate path %q on metrics http server", path)
200+
}
201+
s.options.ExtraHandlers[path] = handler
202+
return nil
203+
}
204+
185205
// Start runs the server.
186206
// It will install the metrics related resources depend on the server configuration.
187207
func (s *defaultServer) Start(ctx context.Context) error {

0 commit comments

Comments
 (0)