2017-11-08 14:14:03 +00:00
|
|
|
package metrics
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2018-11-14 09:18:03 +00:00
|
|
|
"context"
|
2018-05-29 20:58:03 +00:00
|
|
|
"fmt"
|
|
|
|
"net/url"
|
|
|
|
"regexp"
|
2017-11-08 14:14:03 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
kitlog "github.com/go-kit/kit/log"
|
|
|
|
"github.com/go-kit/kit/metrics/influx"
|
2019-07-18 19:36:05 +00:00
|
|
|
influxdb "github.com/influxdata/influxdb1-client/v2"
|
2020-09-16 13:46:04 +00:00
|
|
|
"github.com/traefik/traefik/v2/pkg/log"
|
|
|
|
"github.com/traefik/traefik/v2/pkg/safe"
|
|
|
|
"github.com/traefik/traefik/v2/pkg/types"
|
2017-11-08 14:14:03 +00:00
|
|
|
)
|
|
|
|
|
2018-05-29 20:58:03 +00:00
|
|
|
var influxDBClient *influx.Influx
|
2017-11-08 14:14:03 +00:00
|
|
|
|
|
|
|
type influxDBWriter struct {
|
|
|
|
buf bytes.Buffer
|
|
|
|
config *types.InfluxDB
|
|
|
|
}
|
|
|
|
|
|
|
|
var influxDBTicker *time.Ticker
|
|
|
|
|
|
|
|
const (
|
2021-04-30 08:22:04 +00:00
|
|
|
influxDBConfigReloadsName = "traefik.config.reload.total"
|
|
|
|
influxDBConfigReloadsFailureName = influxDBConfigReloadsName + ".failure"
|
|
|
|
influxDBLastConfigReloadSuccessName = "traefik.config.reload.lastSuccessTimestamp"
|
|
|
|
influxDBLastConfigReloadFailureName = "traefik.config.reload.lastFailureTimestamp"
|
|
|
|
|
2020-12-18 17:44:03 +00:00
|
|
|
influxDBTLSCertsNotAfterTimestampName = "traefik.tls.certs.notAfterTimestamp"
|
2021-04-30 08:22:04 +00:00
|
|
|
|
|
|
|
influxDBEntryPointReqsName = "traefik.entrypoint.requests.total"
|
|
|
|
influxDBEntryPointReqsTLSName = "traefik.entrypoint.requests.tls.total"
|
|
|
|
influxDBEntryPointReqDurationName = "traefik.entrypoint.request.duration"
|
|
|
|
influxDBEntryPointOpenConnsName = "traefik.entrypoint.connections.open"
|
|
|
|
|
|
|
|
influxDBRouterReqsName = "traefik.router.requests.total"
|
|
|
|
influxDBRouterReqsTLSName = "traefik.router.requests.tls.total"
|
|
|
|
influxDBRouterReqsDurationName = "traefik.router.request.duration"
|
|
|
|
influxDBORouterOpenConnsName = "traefik.router.connections.open"
|
|
|
|
|
|
|
|
influxDBServiceReqsName = "traefik.service.requests.total"
|
|
|
|
influxDBServiceReqsTLSName = "traefik.service.requests.tls.total"
|
|
|
|
influxDBServiceReqsDurationName = "traefik.service.request.duration"
|
|
|
|
influxDBServiceRetriesTotalName = "traefik.service.retries.total"
|
|
|
|
influxDBServiceOpenConnsName = "traefik.service.connections.open"
|
|
|
|
influxDBServiceServerUpName = "traefik.service.server.up"
|
2017-11-08 14:14:03 +00:00
|
|
|
)
|
|
|
|
|
2018-11-14 09:18:03 +00:00
|
|
|
const (
|
|
|
|
protocolHTTP = "http"
|
|
|
|
protocolUDP = "udp"
|
|
|
|
)
|
|
|
|
|
2017-11-08 14:14:03 +00:00
|
|
|
// RegisterInfluxDB registers the metrics pusher if this didn't happen yet and creates a InfluxDB Registry instance.
|
2018-11-14 09:18:03 +00:00
|
|
|
func RegisterInfluxDB(ctx context.Context, config *types.InfluxDB) Registry {
|
2018-05-29 20:58:03 +00:00
|
|
|
if influxDBClient == nil {
|
2018-11-14 09:18:03 +00:00
|
|
|
influxDBClient = initInfluxDBClient(ctx, config)
|
2018-05-29 20:58:03 +00:00
|
|
|
}
|
2017-11-08 14:14:03 +00:00
|
|
|
if influxDBTicker == nil {
|
2019-07-18 19:36:05 +00:00
|
|
|
influxDBTicker = initInfluxDBTicker(ctx, config)
|
2017-11-08 14:14:03 +00:00
|
|
|
}
|
|
|
|
|
2019-07-18 19:36:05 +00:00
|
|
|
registry := &standardRegistry{
|
2020-12-18 17:44:03 +00:00
|
|
|
configReloadsCounter: influxDBClient.NewCounter(influxDBConfigReloadsName),
|
|
|
|
configReloadsFailureCounter: influxDBClient.NewCounter(influxDBConfigReloadsFailureName),
|
|
|
|
lastConfigReloadSuccessGauge: influxDBClient.NewGauge(influxDBLastConfigReloadSuccessName),
|
|
|
|
lastConfigReloadFailureGauge: influxDBClient.NewGauge(influxDBLastConfigReloadFailureName),
|
|
|
|
tlsCertsNotAfterTimestampGauge: influxDBClient.NewGauge(influxDBTLSCertsNotAfterTimestampName),
|
2017-11-08 14:14:03 +00:00
|
|
|
}
|
2019-07-18 19:36:05 +00:00
|
|
|
|
|
|
|
if config.AddEntryPointsLabels {
|
|
|
|
registry.epEnabled = config.AddEntryPointsLabels
|
|
|
|
registry.entryPointReqsCounter = influxDBClient.NewCounter(influxDBEntryPointReqsName)
|
2021-04-30 08:22:04 +00:00
|
|
|
registry.entryPointReqsTLSCounter = influxDBClient.NewCounter(influxDBEntryPointReqsTLSName)
|
2020-03-05 14:10:07 +00:00
|
|
|
registry.entryPointReqDurationHistogram, _ = NewHistogramWithScale(influxDBClient.NewHistogram(influxDBEntryPointReqDurationName), time.Second)
|
2019-07-18 19:36:05 +00:00
|
|
|
registry.entryPointOpenConnsGauge = influxDBClient.NewGauge(influxDBEntryPointOpenConnsName)
|
|
|
|
}
|
|
|
|
|
2021-04-30 08:22:04 +00:00
|
|
|
if config.AddRoutersLabels {
|
|
|
|
registry.routerEnabled = config.AddRoutersLabels
|
|
|
|
registry.routerReqsCounter = influxDBClient.NewCounter(influxDBRouterReqsName)
|
|
|
|
registry.routerReqsTLSCounter = influxDBClient.NewCounter(influxDBRouterReqsTLSName)
|
|
|
|
registry.routerReqDurationHistogram, _ = NewHistogramWithScale(influxDBClient.NewHistogram(influxDBRouterReqsDurationName), time.Second)
|
|
|
|
registry.routerOpenConnsGauge = influxDBClient.NewGauge(influxDBORouterOpenConnsName)
|
|
|
|
}
|
|
|
|
|
2019-07-18 19:36:05 +00:00
|
|
|
if config.AddServicesLabels {
|
|
|
|
registry.svcEnabled = config.AddServicesLabels
|
2021-04-30 08:22:04 +00:00
|
|
|
registry.serviceReqsCounter = influxDBClient.NewCounter(influxDBServiceReqsName)
|
|
|
|
registry.serviceReqsTLSCounter = influxDBClient.NewCounter(influxDBServiceReqsTLSName)
|
|
|
|
registry.serviceReqDurationHistogram, _ = NewHistogramWithScale(influxDBClient.NewHistogram(influxDBServiceReqsDurationName), time.Second)
|
|
|
|
registry.serviceRetriesCounter = influxDBClient.NewCounter(influxDBServiceRetriesTotalName)
|
|
|
|
registry.serviceOpenConnsGauge = influxDBClient.NewGauge(influxDBServiceOpenConnsName)
|
|
|
|
registry.serviceServerUpGauge = influxDBClient.NewGauge(influxDBServiceServerUpName)
|
2019-07-18 19:36:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return registry
|
2017-11-08 14:14:03 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 10:06:07 +00:00
|
|
|
// initInfluxDBTicker creates a influxDBClient.
|
2018-11-14 09:18:03 +00:00
|
|
|
func initInfluxDBClient(ctx context.Context, config *types.InfluxDB) *influx.Influx {
|
|
|
|
logger := log.FromContext(ctx)
|
|
|
|
|
2018-05-29 20:58:03 +00:00
|
|
|
// TODO deprecated: move this switch into configuration.SetEffectiveConfiguration when web provider will be removed.
|
|
|
|
switch config.Protocol {
|
2018-11-14 09:18:03 +00:00
|
|
|
case protocolUDP:
|
2018-05-29 20:58:03 +00:00
|
|
|
if len(config.Database) > 0 || len(config.RetentionPolicy) > 0 {
|
2018-11-14 09:18:03 +00:00
|
|
|
logger.Warn("Database and RetentionPolicy options have no effect with UDP.")
|
2018-05-29 20:58:03 +00:00
|
|
|
config.Database = ""
|
|
|
|
config.RetentionPolicy = ""
|
|
|
|
}
|
2018-11-14 09:18:03 +00:00
|
|
|
case protocolHTTP:
|
2018-05-29 20:58:03 +00:00
|
|
|
if u, err := url.Parse(config.Address); err == nil {
|
|
|
|
if u.Scheme != "http" && u.Scheme != "https" {
|
2018-11-14 09:18:03 +00:00
|
|
|
logger.Warnf("InfluxDB address %s should specify a scheme (http or https): falling back on HTTP.", config.Address)
|
2018-05-29 20:58:03 +00:00
|
|
|
config.Address = "http://" + config.Address
|
|
|
|
}
|
|
|
|
} else {
|
2018-11-14 09:18:03 +00:00
|
|
|
logger.Errorf("Unable to parse the InfluxDB address %v: falling back on UDP.", err)
|
|
|
|
config.Protocol = protocolUDP
|
2018-05-29 20:58:03 +00:00
|
|
|
config.Database = ""
|
|
|
|
config.RetentionPolicy = ""
|
|
|
|
}
|
|
|
|
default:
|
2018-11-14 09:18:03 +00:00
|
|
|
logger.Warnf("Unsupported protocol %s: falling back on UDP.", config.Protocol)
|
|
|
|
config.Protocol = protocolUDP
|
2018-05-29 20:58:03 +00:00
|
|
|
config.Database = ""
|
|
|
|
config.RetentionPolicy = ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return influx.New(
|
|
|
|
map[string]string{},
|
|
|
|
influxdb.BatchPointsConfig{
|
|
|
|
Database: config.Database,
|
|
|
|
RetentionPolicy: config.RetentionPolicy,
|
|
|
|
},
|
|
|
|
kitlog.LoggerFunc(func(keyvals ...interface{}) error {
|
2018-11-14 09:18:03 +00:00
|
|
|
log.WithoutContext().WithField(log.MetricsProviderName, "influxdb").Info(keyvals)
|
2018-05-29 20:58:03 +00:00
|
|
|
return nil
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2020-05-11 10:06:07 +00:00
|
|
|
// initInfluxDBTicker initializes metrics pusher.
|
2019-07-18 19:36:05 +00:00
|
|
|
func initInfluxDBTicker(ctx context.Context, config *types.InfluxDB) *time.Ticker {
|
2019-06-17 09:48:05 +00:00
|
|
|
report := time.NewTicker(time.Duration(config.PushInterval))
|
2017-11-08 14:14:03 +00:00
|
|
|
|
|
|
|
safe.Go(func() {
|
|
|
|
var buf bytes.Buffer
|
2019-07-18 19:36:05 +00:00
|
|
|
influxDBClient.WriteLoop(ctx, report.C, &influxDBWriter{buf: buf, config: config})
|
2017-11-08 14:14:03 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
return report
|
|
|
|
}
|
|
|
|
|
2020-05-11 10:06:07 +00:00
|
|
|
// StopInfluxDB stops internal influxDBTicker which controls the pushing of metrics to InfluxDB Agent and resets it to `nil`.
|
2017-11-08 14:14:03 +00:00
|
|
|
func StopInfluxDB() {
|
|
|
|
if influxDBTicker != nil {
|
|
|
|
influxDBTicker.Stop()
|
|
|
|
}
|
|
|
|
influxDBTicker = nil
|
|
|
|
}
|
|
|
|
|
2018-05-29 20:58:03 +00:00
|
|
|
// Write creates a http or udp client and attempts to write BatchPoints.
|
|
|
|
// If a "database not found" error is encountered, a CREATE DATABASE
|
|
|
|
// query is attempted when using protocol http.
|
2017-11-08 14:14:03 +00:00
|
|
|
func (w *influxDBWriter) Write(bp influxdb.BatchPoints) error {
|
2018-05-29 20:58:03 +00:00
|
|
|
c, err := w.initWriteClient()
|
2017-11-08 14:14:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer c.Close()
|
|
|
|
|
2018-05-29 20:58:03 +00:00
|
|
|
if writeErr := c.Write(bp); writeErr != nil {
|
2018-11-14 09:18:03 +00:00
|
|
|
ctx := log.With(context.Background(), log.Str(log.MetricsProviderName, "influxdb"))
|
|
|
|
log.FromContext(ctx).Errorf("Error while writing to InfluxDB: %s", writeErr.Error())
|
|
|
|
|
|
|
|
if handleErr := w.handleWriteError(ctx, c, writeErr); handleErr != nil {
|
2018-05-29 20:58:03 +00:00
|
|
|
return handleErr
|
|
|
|
}
|
|
|
|
// Retry write after successful handling of writeErr
|
|
|
|
return c.Write(bp)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-07-03 08:02:03 +00:00
|
|
|
func (w *influxDBWriter) initWriteClient() (influxdb.Client, error) {
|
2018-05-29 20:58:03 +00:00
|
|
|
if w.config.Protocol == "http" {
|
2018-07-03 08:02:03 +00:00
|
|
|
return influxdb.NewHTTPClient(influxdb.HTTPConfig{
|
2018-07-11 15:50:03 +00:00
|
|
|
Addr: w.config.Address,
|
|
|
|
Username: w.config.Username,
|
|
|
|
Password: w.config.Password,
|
2018-05-29 20:58:03 +00:00
|
|
|
})
|
|
|
|
}
|
2018-07-03 08:02:03 +00:00
|
|
|
|
|
|
|
return influxdb.NewUDPClient(influxdb.UDPConfig{
|
|
|
|
Addr: w.config.Address,
|
|
|
|
})
|
2018-05-29 20:58:03 +00:00
|
|
|
}
|
|
|
|
|
2018-11-14 09:18:03 +00:00
|
|
|
func (w *influxDBWriter) handleWriteError(ctx context.Context, c influxdb.Client, writeErr error) error {
|
|
|
|
if w.config.Protocol != protocolHTTP {
|
2018-05-29 20:58:03 +00:00
|
|
|
return writeErr
|
|
|
|
}
|
|
|
|
|
|
|
|
match, matchErr := regexp.MatchString("database not found", writeErr.Error())
|
|
|
|
|
|
|
|
if matchErr != nil || !match {
|
|
|
|
return writeErr
|
|
|
|
}
|
|
|
|
|
|
|
|
qStr := fmt.Sprintf("CREATE DATABASE \"%s\"", w.config.Database)
|
|
|
|
if w.config.RetentionPolicy != "" {
|
|
|
|
qStr = fmt.Sprintf("%s WITH NAME \"%s\"", qStr, w.config.RetentionPolicy)
|
|
|
|
}
|
|
|
|
|
2018-11-14 09:18:03 +00:00
|
|
|
logger := log.FromContext(ctx)
|
|
|
|
|
|
|
|
logger.Debugf("InfluxDB database not found: attempting to create one with %s", qStr)
|
2018-05-29 20:58:03 +00:00
|
|
|
|
|
|
|
q := influxdb.NewQuery(qStr, "", "")
|
|
|
|
response, queryErr := c.Query(q)
|
|
|
|
if queryErr == nil && response.Error() != nil {
|
|
|
|
queryErr = response.Error()
|
|
|
|
}
|
|
|
|
if queryErr != nil {
|
2018-11-14 09:18:03 +00:00
|
|
|
logger.Errorf("Error while creating the InfluxDB database %s", queryErr)
|
2018-05-29 20:58:03 +00:00
|
|
|
return queryErr
|
|
|
|
}
|
|
|
|
|
2018-11-14 09:18:03 +00:00
|
|
|
logger.Debugf("Successfully created the InfluxDB database %s", w.config.Database)
|
2018-05-29 20:58:03 +00:00
|
|
|
return nil
|
2017-11-08 14:14:03 +00:00
|
|
|
}
|