From 2630978ac2e2e4a5bd16512c16360baae174da6f Mon Sep 17 00:00:00 2001 From: Amir-Hadadi Date: Tue, 29 Oct 2024 14:59:53 +0200 Subject: [PATCH] Added go runtime metrics collection flag 'collector.disable-go-runtime-metrics' --- cmd/process-exporter/main.go | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/cmd/process-exporter/main.go b/cmd/process-exporter/main.go index 6dfc0a0..fe1145c 100644 --- a/cmd/process-exporter/main.go +++ b/cmd/process-exporter/main.go @@ -3,6 +3,7 @@ package main import ( "flag" "fmt" + "github.com/prometheus/client_golang/prometheus/collectors" "log" "net/http" _ "net/http/pprof" @@ -140,9 +141,12 @@ func (nmr *nameMapperRegex) MatchAndName(nacl common.ProcAttributes) (bool, stri return false, "" } +// Create a new custom Prometheus registry +var registry = prometheus.NewRegistry() + func init() { promVersion.Version = version - prometheus.MustRegister(verCollector.NewCollector("process_exporter")) + registry.MustRegister(verCollector.NewCollector("process_exporter")) } func main() { @@ -180,6 +184,8 @@ func main() { showVersion = flag.Bool("version", false, "print version information and exit") removeEmptyGroups = flag.Bool("remove-empty-groups", false, "forget process groups with no processes") + disableGoMetrics = flag.Bool("collector.disable-go-runtime-metrics", false, + "Disable collection of Go runtime metrics") ) flag.Parse() @@ -256,7 +262,11 @@ func main() { log.Fatalf("Error initializing: %v", err) } - prometheus.MustRegister(pc) + registry.MustRegister(pc) + if !*disableGoMetrics { + // Register Go runtime metrics if the flag is not set + registry.MustRegister(collectors.NewGoCollector()) + } if *onceToStdoutDelay != 0 { // We throw away the first result because that first collection primes the pump, and @@ -269,7 +279,7 @@ func main() { return } - http.Handle(*metricsPath, promhttp.Handler()) + http.Handle(*metricsPath, promhttp.HandlerFor(registry, promhttp.HandlerOpts{})) http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { w.Write([]byte(`