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 ( import (
"context" "context"
"crypto/subtle" "crypto/subtle"
"crypto/tls"
"fmt" "fmt"
"log" "log"
"net" "net"
@ -16,7 +17,7 @@ import (
"github.com/influxdata/telegraf" "github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/internal" "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/influxdata/telegraf/plugins/outputs"
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp" "github.com/prometheus/client_golang/prometheus/promhttp"
@ -66,7 +67,7 @@ type PrometheusClient struct {
StringAsLabel bool `toml:"string_as_label"` StringAsLabel bool `toml:"string_as_label"`
ExportTimestamp bool `toml:"export_timestamp"` ExportTimestamp bool `toml:"export_timestamp"`
tls.ServerConfig tlsint.ServerConfig
server *http.Server server *http.Server
@ -199,13 +200,18 @@ func (p *PrometheusClient) Connect() error {
TLSConfig: tlsConfig, TLSConfig: tlsConfig,
} }
go func() { var listener net.Listener
var err error if tlsConfig != nil {
if p.TLSCert != "" && p.TLSKey != "" { listener, err = tls.Listen("tcp", p.Listen, tlsConfig)
err = p.server.ListenAndServeTLS("", "")
} else { } 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 { if err != nil && err != http.ErrServerClosed {
log.Printf("E! Error creating prometheus metric endpoint, err: %s\n", log.Printf("E! Error creating prometheus metric endpoint, err: %s\n",
err.Error()) err.Error())