From cf24da73d08a61f2fe24775ec55a36043b7f547f Mon Sep 17 00:00:00 2001 From: Cameron Sparr Date: Fri, 16 Sep 2016 15:43:53 +0100 Subject: [PATCH] Prometheus output: do not remake metrics map each write closes #1775 --- CHANGELOG.md | 8 +++++++- .../prometheus_client/prometheus_client.go | 20 ++++++++++++++----- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 369b93361fbd9..2341c4ebc075b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,12 +23,18 @@ ### Bugfixes - [#1628](https://github.com/influxdata/telegraf/issues/1628): Fix mongodb input panic on version 2.2. -- [#1738](https://github.com/influxdata/telegraf/issues/1738): Fix unmarshal of influxdb metrics with null tags - [#1733](https://github.com/influxdata/telegraf/issues/1733): Fix statsd scientific notation parsing - [#1716](https://github.com/influxdata/telegraf/issues/1716): Sensors plugin strconv.ParseFloat: parsing "": invalid syntax - [#1530](https://github.com/influxdata/telegraf/issues/1530): Fix prometheus_client reload panic - [#1764](https://github.com/influxdata/telegraf/issues/1764): Fix kafka consumer panic when nil error is returned down errs channel. +## v1.0.1 [unreleased] + +### Bugfixes + +- [#1775](https://github.com/influxdata/telegraf/issues/1775): Prometheus output: Fix bug with multi-batch writes. +- [#1738](https://github.com/influxdata/telegraf/issues/1738): Fix unmarshal of influxdb metrics with null tags + ## v1.0 [2016-09-08] ### Release Notes diff --git a/plugins/outputs/prometheus_client/prometheus_client.go b/plugins/outputs/prometheus_client/prometheus_client.go index 1f212073733e0..0598a5a41278e 100644 --- a/plugins/outputs/prometheus_client/prometheus_client.go +++ b/plugins/outputs/prometheus_client/prometheus_client.go @@ -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 } @@ -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 { @@ -83,8 +86,17 @@ 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 + } } } @@ -92,8 +104,6 @@ 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 }