Skip to content

Commit

Permalink
Fix prometheus_client reload behavior
Browse files Browse the repository at this point in the history
fixes #2282
  • Loading branch information
sparrc committed Feb 16, 2017
1 parent e0a36c3 commit dfddcc5
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 19 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ be deprecated eventually.
- [#2356](https://github.com/influxdata/telegraf/issues/2356): cpu input panic when /proc/stat is empty.
- [#2341](https://github.com/influxdata/telegraf/issues/2341): telegraf swallowing panics in --test mode.
- [#2358](https://github.com/influxdata/telegraf/pull/2358): Create pidfile with 644 permissions & defer file deletion.
- [#2282](https://github.com/influxdata/telegraf/issues/2282): Reloading telegraf freezes prometheus output.

## v1.2.1 [2017-02-01]

Expand Down
1 change: 1 addition & 0 deletions agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,5 +398,6 @@ func (a *Agent) Run(shutdown chan struct{}) error {
}

wg.Wait()
a.Close()
return nil
}
6 changes: 3 additions & 3 deletions circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ machine:
post:
- sudo service zookeeper stop
- go version
- go version | grep 1.7.5 || sudo rm -rf /usr/local/go
- wget https://storage.googleapis.com/golang/go1.7.5.linux-amd64.tar.gz
- sudo tar -C /usr/local -xzf go1.7.5.linux-amd64.tar.gz
- sudo rm -rf /usr/local/go
- wget https://storage.googleapis.com/golang/go1.8.linux-amd64.tar.gz
- sudo tar -C /usr/local -xzf go1.8.linux-amd64.tar.gz
- go version

dependencies:
Expand Down
30 changes: 14 additions & 16 deletions plugins/outputs/prometheus_client/prometheus_client.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package prometheus_client

import (
"context"
"fmt"
"log"
"net/http"
Expand All @@ -24,6 +25,7 @@ type MetricWithExpiration struct {
type PrometheusClient struct {
Listen string
ExpirationInterval internal.Duration `toml:"expiration_interval"`
server *http.Server

metrics map[string]*MetricWithExpiration

Expand All @@ -41,30 +43,25 @@ var sampleConfig = `
func (p *PrometheusClient) Start() error {
p.metrics = make(map[string]*MetricWithExpiration)
prometheus.Register(p)
defer func() {
if r := recover(); r != nil {
// recovering from panic here because there is no way to stop a
// running http go server except by a kill signal. Since the server
// does not stop on SIGHUP, Start() will panic when the process
// is reloaded.
}
}()

if p.Listen == "" {
p.Listen = "localhost:9126"
}

http.Handle("/metrics", prometheus.Handler())
server := &http.Server{
Addr: p.Listen,
mux := http.NewServeMux()
mux.Handle("/metrics", prometheus.Handler())

p.server = &http.Server{
Addr: p.Listen,
Handler: mux,
}

go server.ListenAndServe()
go p.server.ListenAndServe()
return nil
}

func (p *PrometheusClient) Stop() {
// TODO: Use a listener for http.Server that counts active connections
// that can be stopped and closed gracefully
// plugin gets cleaned up in Close() already.
}

func (p *PrometheusClient) Connect() error {
Expand All @@ -73,8 +70,9 @@ func (p *PrometheusClient) Connect() error {
}

func (p *PrometheusClient) Close() error {
// This service output does not need to close any of its connections
return nil
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
return p.server.Shutdown(ctx)
}

func (p *PrometheusClient) SampleConfig() string {
Expand Down

0 comments on commit dfddcc5

Please sign in to comment.