swarm/metrics: introduce metrics export flag (#16177)

This commit is contained in:
Anton Evangelatov 2018-02-23 16:22:16 +01:00 committed by Balint Gabor
parent dcca613a0b
commit 114738982e

@ -27,6 +27,10 @@ import (
) )
var ( var (
metricsEnableInfluxDBExportFlag = cli.BoolFlag{
Name: "metrics.influxdb.export",
Usage: "Enable metrics export/push to an external InfluxDB database",
}
metricsInfluxDBEndpointFlag = cli.StringFlag{ metricsInfluxDBEndpointFlag = cli.StringFlag{
Name: "metrics.influxdb.endpoint", Name: "metrics.influxdb.endpoint",
Usage: "Metrics InfluxDB endpoint", Usage: "Metrics InfluxDB endpoint",
@ -34,17 +38,17 @@ var (
} }
metricsInfluxDBDatabaseFlag = cli.StringFlag{ metricsInfluxDBDatabaseFlag = cli.StringFlag{
Name: "metrics.influxdb.database", Name: "metrics.influxdb.database",
Usage: "metrics InfluxDB database", Usage: "Metrics InfluxDB database",
Value: "metrics", Value: "metrics",
} }
metricsInfluxDBUsernameFlag = cli.StringFlag{ metricsInfluxDBUsernameFlag = cli.StringFlag{
Name: "metrics.influxdb.username", Name: "metrics.influxdb.username",
Usage: "metrics InfluxDB username", Usage: "Metrics InfluxDB username",
Value: "", Value: "",
} }
metricsInfluxDBPasswordFlag = cli.StringFlag{ metricsInfluxDBPasswordFlag = cli.StringFlag{
Name: "metrics.influxdb.password", Name: "metrics.influxdb.password",
Usage: "metrics InfluxDB password", Usage: "Metrics InfluxDB password",
Value: "", Value: "",
} }
// The `host` tag is part of every measurement sent to InfluxDB. Queries on tags are faster in InfluxDB. // The `host` tag is part of every measurement sent to InfluxDB. Queries on tags are faster in InfluxDB.
@ -53,7 +57,7 @@ var (
// https://docs.influxdata.com/influxdb/v1.4/concepts/key_concepts/#tag-key // https://docs.influxdata.com/influxdb/v1.4/concepts/key_concepts/#tag-key
metricsInfluxDBHostTagFlag = cli.StringFlag{ metricsInfluxDBHostTagFlag = cli.StringFlag{
Name: "metrics.influxdb.host.tag", Name: "metrics.influxdb.host.tag",
Usage: "metrics InfluxDB `host` tag attached to all measurements", Usage: "Metrics InfluxDB `host` tag attached to all measurements",
Value: "localhost", Value: "localhost",
} }
) )
@ -61,22 +65,27 @@ var (
// Flags holds all command-line flags required for metrics collection. // Flags holds all command-line flags required for metrics collection.
var Flags = []cli.Flag{ var Flags = []cli.Flag{
utils.MetricsEnabledFlag, utils.MetricsEnabledFlag,
metricsEnableInfluxDBExportFlag,
metricsInfluxDBEndpointFlag, metricsInfluxDBDatabaseFlag, metricsInfluxDBUsernameFlag, metricsInfluxDBPasswordFlag, metricsInfluxDBHostTagFlag, metricsInfluxDBEndpointFlag, metricsInfluxDBDatabaseFlag, metricsInfluxDBUsernameFlag, metricsInfluxDBPasswordFlag, metricsInfluxDBHostTagFlag,
} }
func Setup(ctx *cli.Context) { func Setup(ctx *cli.Context) {
if gethmetrics.Enabled { if gethmetrics.Enabled {
log.Info("Enabling swarm metrics collection")
var ( var (
endpoint = ctx.GlobalString(metricsInfluxDBEndpointFlag.Name) enableExport = ctx.GlobalBool(metricsEnableInfluxDBExportFlag.Name)
database = ctx.GlobalString(metricsInfluxDBDatabaseFlag.Name) endpoint = ctx.GlobalString(metricsInfluxDBEndpointFlag.Name)
username = ctx.GlobalString(metricsInfluxDBUsernameFlag.Name) database = ctx.GlobalString(metricsInfluxDBDatabaseFlag.Name)
password = ctx.GlobalString(metricsInfluxDBPasswordFlag.Name) username = ctx.GlobalString(metricsInfluxDBUsernameFlag.Name)
hosttag = ctx.GlobalString(metricsInfluxDBHostTagFlag.Name) password = ctx.GlobalString(metricsInfluxDBPasswordFlag.Name)
hosttag = ctx.GlobalString(metricsInfluxDBHostTagFlag.Name)
) )
log.Info("Enabling swarm metrics collection and export") if enableExport {
go influxdb.InfluxDBWithTags(gethmetrics.DefaultRegistry, 10*time.Second, endpoint, database, username, password, "swarm.", map[string]string{ log.Info("Enabling swarm metrics export to InfluxDB")
"host": hosttag, go influxdb.InfluxDBWithTags(gethmetrics.DefaultRegistry, 10*time.Second, endpoint, database, username, password, "swarm.", map[string]string{
}) "host": hosttag,
})
}
} }
} }