Fix separation of multiple prometheus_client outputs (#3570)

This commit is contained in:
Daniel Nelson 2017-12-11 18:00:19 -08:00 committed by GitHub
parent ab8376de03
commit 8484de6c12
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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,