Skip to content

Commit

Permalink
Listen before returning from Connect in prometheus output (#5509)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielnelson authored Mar 1, 2019
1 parent 41286d1 commit 2c09010
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions plugins/outputs/prometheus_client/prometheus_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package prometheus_client
import (
"context"
"crypto/subtle"
"crypto/tls"
"fmt"
"log"
"net"
Expand All @@ -16,7 +17,7 @@ import (

"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/internal"
"github.com/influxdata/telegraf/internal/tls"
tlsint "github.com/influxdata/telegraf/internal/tls"
"github.com/influxdata/telegraf/plugins/outputs"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
Expand Down Expand Up @@ -66,7 +67,7 @@ type PrometheusClient struct {
StringAsLabel bool `toml:"string_as_label"`
ExportTimestamp bool `toml:"export_timestamp"`

tls.ServerConfig
tlsint.ServerConfig

server *http.Server

Expand Down Expand Up @@ -199,13 +200,18 @@ func (p *PrometheusClient) Connect() error {
TLSConfig: tlsConfig,
}

var listener net.Listener
if tlsConfig != nil {
listener, err = tls.Listen("tcp", p.Listen, tlsConfig)
} else {
listener, err = net.Listen("tcp", p.Listen)
}
if err != nil {
return err
}

go func() {
var err error
if p.TLSCert != "" && p.TLSKey != "" {
err = p.server.ListenAndServeTLS("", "")
} else {
err = p.server.ListenAndServe()
}
err := p.server.Serve(listener)
if err != nil && err != http.ErrServerClosed {
log.Printf("E! Error creating prometheus metric endpoint, err: %s\n",
err.Error())
Expand Down

0 comments on commit 2c09010

Please sign in to comment.