diff --git a/cmd/main.go b/cmd/main.go index 91f651e9..67e15dc1 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -64,6 +64,8 @@ func main() { flag.Var(&watchNamespaces, "namespace", "Namespace to watch") var opVersion bool flag.BoolVar(&opVersion, "version", false, "Print operator version") + var metricsList bool + flag.BoolVar(&metricsList, "list-metrics", false, "List operator metrics") // AGENT POOL CONTROLLER OPTIONS var agentPoolWorkers int flag.IntVar(&agentPoolWorkers, "agent-pool-workers", 1, @@ -112,6 +114,14 @@ func main() { os.Exit(0) } + if metricsList { + if err := controller.ListHCPTMetrics(); err != nil { + fmt.Println("Failed to list metrics:", err) + os.Exit(1) + } + os.Exit(0) + } + zapConfig := zap.NewProductionConfig() zapConfig.EncoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder zapConfig.EncoderConfig.EncodeTime = zapcore.RFC3339TimeEncoder diff --git a/internal/controller/metrics.go b/internal/controller/metrics.go index 84657550..2f2e406c 100644 --- a/internal/controller/metrics.go +++ b/internal/controller/metrics.go @@ -4,6 +4,9 @@ package controller import ( + "fmt" + "strings" + "github.com/prometheus/client_golang/prometheus" "sigs.k8s.io/controller-runtime/pkg/metrics" ) @@ -45,4 +48,26 @@ func RegisterMetrics() { metricRuns, metricRunsTotal, ) + // Initialize all run status metrics to 0. + for _, s := range runStatuses { + metricRuns.WithLabelValues(string(s)).Set(0) + } + // Initialize total runs metric to 0. + metricRunsTotal.WithLabelValues().Set(0) +} + +func ListHCPTMetrics() error { + mfs, err := metrics.Registry.Gather() + if err != nil { + return err + } + for _, mf := range mfs { + name := mf.GetName() + if strings.HasPrefix(name, "hcp_tf") { + fmt.Printf("# HELP %s %s\n", name, mf.GetHelp()) + fmt.Printf("# TYPE %s %s\n", name, mf.Type.String()) + } + } + + return nil }