Skip to content

Commit

Permalink
Prometheus output: do not remake metrics map each write
Browse files Browse the repository at this point in the history
closes #1775
  • Loading branch information
sparrc committed Sep 16, 2016
1 parent 9b2e2cc commit 78af310
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions plugins/outputs/prometheus_client/prometheus_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ var invalidNameCharRE = regexp.MustCompile(`[^a-zA-Z0-9_]`)
type PrometheusClient struct {
Listen string

metrics map[string]prometheus.Metric
metrics map[string]prometheus.Metric
lastMetrics map[string]prometheus.Metric

sync.Mutex
}
Expand All @@ -28,6 +29,8 @@ var sampleConfig = `
`

func (p *PrometheusClient) Start() error {
p.metrics = make(map[string]prometheus.Metric)
p.lastMetrics = make(map[string]prometheus.Metric)
prometheus.Register(p)
defer func() {
if r := recover(); r != nil {
Expand Down Expand Up @@ -83,17 +86,24 @@ func (p *PrometheusClient) Collect(ch chan<- prometheus.Metric) {
p.Lock()
defer p.Unlock()

for _, m := range p.metrics {
ch <- m
if len(p.metrics) > 0 {
p.lastMetrics = make(map[string]prometheus.Metric)
for k, m := range p.metrics {
ch <- m
p.lastMetrics[k] = m
}
p.metrics = make(map[string]prometheus.Metric)
} else {
for _, m := range p.lastMetrics {
ch <- m
}
}
}

func (p *PrometheusClient) Write(metrics []telegraf.Metric) error {
p.Lock()
defer p.Unlock()

p.metrics = make(map[string]prometheus.Metric)

if len(metrics) == 0 {
return nil
}
Expand Down

0 comments on commit 78af310

Please sign in to comment.