From ebc0ae70adcfa052edc17eb28483690744bb3ecd Mon Sep 17 00:00:00 2001 From: Dan Rammer Date: Wed, 23 Aug 2023 17:42:47 -0500 Subject: [PATCH] adding controller-runtime manager metrics handler to http endpoint (#607) * adding controller-runtime manager metrics handler to http endpoint Signed-off-by: Daniel Rammer * fix net/http import Signed-off-by: Daniel Rammer * fixed lint Signed-off-by: Daniel Rammer --------- Signed-off-by: Daniel Rammer --- cmd/controller/cmd/root.go | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/cmd/controller/cmd/root.go b/cmd/controller/cmd/root.go index 2bebc269b..465e625a8 100644 --- a/cmd/controller/cmd/root.go +++ b/cmd/controller/cmd/root.go @@ -4,6 +4,7 @@ package cmd import ( "context" "flag" + "net/http" "os" "runtime" @@ -21,6 +22,8 @@ import ( "github.com/flyteorg/flytestdlib/promutils/labeled" "github.com/flyteorg/flytestdlib/version" + "github.com/prometheus/client_golang/prometheus/promhttp" + "github.com/spf13/cobra" "github.com/spf13/pflag" @@ -32,6 +35,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/cache" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/manager" + "sigs.k8s.io/controller-runtime/pkg/metrics" ) const ( @@ -140,9 +144,17 @@ func executeRootCmd(baseCtx context.Context, cfg *config2.Config) error { return err } + handlers := map[string]http.Handler{ + "/k8smetrics": promhttp.HandlerFor(metrics.Registry, + promhttp.HandlerOpts{ + ErrorHandling: promhttp.HTTPErrorOnError, + }, + ), + } + g, childCtx := errgroup.WithContext(ctx) g.Go(func() error { - err := profutils.StartProfilingServerWithDefaultHandlers(childCtx, cfg.ProfilerPort.Port, nil) + err := profutils.StartProfilingServerWithDefaultHandlers(childCtx, cfg.ProfilerPort.Port, handlers) if err != nil { logger.Fatalf(childCtx, "Failed to Start profiling and metrics server. Error: %v", err) }