From 0897e9970167c22cf4f3ccd54bcb2869ce0f40ac Mon Sep 17 00:00:00 2001 From: pancho horrillo Date: Fri, 1 Feb 2019 11:44:35 +0100 Subject: [PATCH] Use prometheus.Gatherers to avoid duplicating std collectors Seen on github.com/minio/minio/cmd/metrics.go This way, the ProcessCollector and the GoCollector will operate on the Prometheus DefaultRegistry, whose durability is the same as the process. On the other hand, the registry we instantiate per server will live and die with it. I was inclined to implement this because I think that otherwise we ended up with a redundant set of *Collectors, ones instantiated by us, and the othe ones instantiated by the prometheus library, as goodies of the DefaultRegistry. Besides, now I know how to make use of multiple registries (via Gatherers, which kinda melds them). --- api/metricshttp/metricshttp.go | 6 +++++- server/server.go | 9 --------- 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/api/metricshttp/metricshttp.go b/api/metricshttp/metricshttp.go index b30015bf8..ae381fbd4 100644 --- a/api/metricshttp/metricshttp.go +++ b/api/metricshttp/metricshttp.go @@ -9,12 +9,16 @@ import ( func NewMetricsHTTP(r *prometheus.Registry) *http.ServeMux { mux := http.NewServeMux() + g := prometheus.Gatherers{ + prometheus.DefaultGatherer, + r, + } mux.Handle( "/metrics", promhttp.InstrumentMetricHandler( r, promhttp.HandlerFor( - r, + g, promhttp.HandlerOpts{}, ), ), diff --git a/server/server.go b/server/server.go index 21b7676e9..f5657b218 100644 --- a/server/server.go +++ b/server/server.go @@ -175,15 +175,6 @@ func NewServer(conf *Config) (*Server, error) { server.prometheusRegistry = r metricsMux := metricshttp.NewMetricsHTTP(r) - r.MustRegister( - prometheus.NewProcessCollector( - prometheus.ProcessCollectorOpts{}, - ), - ) - r.MustRegister( - prometheus.NewGoCollector(), - ) - server.metricsServer = newHTTPServer("localhost:9990", metricsMux) }