Prometheus output: do not remake metrics map each write

closes #1775
This commit is contained in:
Cameron Sparr 2016-09-16 15:43:53 +01:00
parent 7fac74919c
commit 532223a9cb
1 changed files with 15 additions and 5 deletions

View File

@ -18,6 +18,7 @@ type PrometheusClient struct {
Listen string Listen string
metrics map[string]prometheus.Metric metrics map[string]prometheus.Metric
lastMetrics map[string]prometheus.Metric
sync.Mutex sync.Mutex
} }
@ -28,6 +29,8 @@ var sampleConfig = `
` `
func (p *PrometheusClient) Start() error { func (p *PrometheusClient) Start() error {
p.metrics = make(map[string]prometheus.Metric)
p.lastMetrics = make(map[string]prometheus.Metric)
prometheus.MustRegister(p) prometheus.MustRegister(p)
defer func() { defer func() {
if r := recover(); r != nil { if r := recover(); r != nil {
@ -83,8 +86,17 @@ func (p *PrometheusClient) Collect(ch chan<- prometheus.Metric) {
p.Lock() p.Lock()
defer p.Unlock() defer p.Unlock()
for _, m := range p.metrics { if len(p.metrics) > 0 {
p.lastMetrics = make(map[string]prometheus.Metric)
for k, m := range p.metrics {
ch <- m ch <- m
p.lastMetrics[k] = m
}
p.metrics = make(map[string]prometheus.Metric)
} else {
for _, m := range p.lastMetrics {
ch <- m
}
} }
} }
@ -92,8 +104,6 @@ func (p *PrometheusClient) Write(metrics []telegraf.Metric) error {
p.Lock() p.Lock()
defer p.Unlock() defer p.Unlock()
p.metrics = make(map[string]prometheus.Metric)
if len(metrics) == 0 { if len(metrics) == 0 {
return nil return nil
} }