Fix separation of multiple prometheus_client outputs (#3570)

(cherry picked from commit 8484de6c12)
This commit is contained in:
Daniel Nelson 2017-12-11 18:00:19 -08:00 committed by Daniel Nelson
parent fd964bd4eb
commit 8d4a09c3ea
No known key found for this signature in database
GPG Key ID: CAAD59C9444F6155
1 changed files with 14 additions and 6 deletions

View File

@ -79,19 +79,28 @@ var sampleConfig = `
` `
func (p *PrometheusClient) Start() error { func (p *PrometheusClient) Start() error {
prometheus.Register(p) defaultCollectors := map[string]bool{
"gocollector": true,
"process": true,
}
for _, collector := range p.CollectorsExclude { for _, collector := range p.CollectorsExclude {
delete(defaultCollectors, collector)
}
registry := prometheus.NewRegistry()
for collector, _ := range defaultCollectors {
switch collector { switch collector {
case "gocollector": case "gocollector":
prometheus.Unregister(prometheus.NewGoCollector()) registry.Register(prometheus.NewGoCollector())
case "process": case "process":
prometheus.Unregister(prometheus.NewProcessCollector(os.Getpid(), "")) registry.Register(prometheus.NewProcessCollector(os.Getpid(), ""))
default: default:
return fmt.Errorf("unrecognized collector %s", collector) return fmt.Errorf("unrecognized collector %s", collector)
} }
} }
registry.Register(p)
if p.Listen == "" { if p.Listen == "" {
p.Listen = "localhost:9273" p.Listen = "localhost:9273"
} }
@ -102,8 +111,7 @@ func (p *PrometheusClient) Start() error {
mux := http.NewServeMux() mux := http.NewServeMux()
mux.Handle(p.Path, promhttp.HandlerFor( mux.Handle(p.Path, promhttp.HandlerFor(
prometheus.DefaultGatherer, registry, promhttp.HandlerOpts{ErrorHandling: promhttp.ContinueOnError}))
promhttp.HandlerOpts{ErrorHandling: promhttp.ContinueOnError}))
p.server = &http.Server{ p.server = &http.Server{
Addr: p.Listen, Addr: p.Listen,