From 8d4a09c3eacdbd674314bd495a47b3e0bc705c31 Mon Sep 17 00:00:00 2001 From: Daniel Nelson Date: Mon, 11 Dec 2017 18:00:19 -0800 Subject: [PATCH] Fix separation of multiple prometheus_client outputs (#3570) (cherry picked from commit 8484de6c12873b2815f679bf1d7a7d61a794ee6e) --- .../prometheus_client/prometheus_client.go | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/plugins/outputs/prometheus_client/prometheus_client.go b/plugins/outputs/prometheus_client/prometheus_client.go index f0b0a7673..5b3ee2189 100644 --- a/plugins/outputs/prometheus_client/prometheus_client.go +++ b/plugins/outputs/prometheus_client/prometheus_client.go @@ -79,19 +79,28 @@ var sampleConfig = ` ` func (p *PrometheusClient) Start() error { - prometheus.Register(p) - + defaultCollectors := map[string]bool{ + "gocollector": true, + "process": true, + } for _, collector := range p.CollectorsExclude { + delete(defaultCollectors, collector) + } + + registry := prometheus.NewRegistry() + for collector, _ := range defaultCollectors { switch collector { case "gocollector": - prometheus.Unregister(prometheus.NewGoCollector()) + registry.Register(prometheus.NewGoCollector()) case "process": - prometheus.Unregister(prometheus.NewProcessCollector(os.Getpid(), "")) + registry.Register(prometheus.NewProcessCollector(os.Getpid(), "")) default: return fmt.Errorf("unrecognized collector %s", collector) } } + registry.Register(p) + if p.Listen == "" { p.Listen = "localhost:9273" } @@ -102,8 +111,7 @@ func (p *PrometheusClient) Start() error { mux := http.NewServeMux() mux.Handle(p.Path, promhttp.HandlerFor( - prometheus.DefaultGatherer, - promhttp.HandlerOpts{ErrorHandling: promhttp.ContinueOnError})) + registry, promhttp.HandlerOpts{ErrorHandling: promhttp.ContinueOnError})) p.server = &http.Server{ Addr: p.Listen,