Listen before returning from Connect in prometheus output (#5509)

This commit is contained in:
Daniel Nelson 2019-03-01 11:19:31 -08:00 committed by GitHub
parent 41286d10c2
commit 2c09010f72
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 14 additions and 8 deletions

View File

@ -3,6 +3,7 @@ package prometheus_client
import (
"context"
"crypto/subtle"
"crypto/tls"
"fmt"
"log"
"net"
@ -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"
@ -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
@ -199,13 +200,18 @@ func (p *PrometheusClient) Connect() error {
TLSConfig: tlsConfig,
}
go func() {
var err error
if p.TLSCert != "" && p.TLSKey != "" {
err = p.server.ListenAndServeTLS("", "")
var listener net.Listener
if tlsConfig != nil {
listener, err = tls.Listen("tcp", p.Listen, tlsConfig)
} else {
err = p.server.ListenAndServe()
listener, err = net.Listen("tcp", p.Listen)
}
if err != nil {
return err
}
go func() {
err := p.server.Serve(listener)
if err != nil && err != http.ErrServerClosed {
log.Printf("E! Error creating prometheus metric endpoint, err: %s\n",
err.Error())