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 cf24da7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
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 cf24da7

Please sign in to comment.